Pass Listbox by reference

Posted on 2006-06-02
Medium Priority
Last Modified: 2008-01-09


   I have a list box called "lst_AddIPP_AvailableParts" which I want to pass by reference to a subroutine when the listbox is clicked. The subroutine enables or disables a button if there are any items in the listbox that are selected. The listbox has the multiselect property set to True. The calling code is below:

Private Sub lst_AddIPP_AvailableParts_Click()    
    EvaluateListClick lst_AddIPP_AvailableParts, btn_AddIPP_PartNLeft
End Sub

The function that evaluates the listbox, "EvaluateListClick" is defined below:

Private Sub EvaluateListClick(ByRef mylist As Access.listbox, ByRef mybtn As Button)
    Dim bck As Boolean
    Dim varItm As Variant
    bck = False
    For Each varItm In mylist.ItemsSelected
        bck = True
        Exit For
    Next varItm
    If bck = True Then
        mybtn.Enabled = True
        mybtn.Enabled = False
    End If
End Sub

The form crashes when the user clicks the listbox and the error message says: "Type mismatch". The debugger says the listbox passed to "EvaluateListClick" has a value of null. Interestingly if I use the listbox on the line above the "EvaluateListClick" call, it works fine. Also fine works when I pass the listbox as a parameter to a subroutine when a button is clicked. The only place I get this error is when I try to pass the listbox as a parameter from within the listbox event.

Not sure how to get rid of this problem. Can someone help?

To replicate, place a listbox on a form and add an onClick event. Use the above code for the implementation.
The listbox has the multiselect property set to True.

Question by:maxy88
  • 3

Expert Comment

ID: 16819043
what line does the type mismatch occur on?

Accepted Solution

MageDribble earned 1000 total points
ID: 16819058
Private Sub EvaluateListClick(ByRef mylist As Access.listbox, ByRef mybtn As Button)

change it to:

Private Sub EvaluateListClick(ByRef mylist As Listbox, ByRef mybtn As as CommandButton)

Author Comment

ID: 16819100

Well done, MageDribble how did you know that was the culprit?


Expert Comment

ID: 16819186
Access doesn't have Buttons.  VB.NET does but Access does not.  Access only has CommandButtons and if you are in the Access environment you shouldn't need to prefix with Access.ListBox

I know that was the culprit because I've done it before :)

Good luck with your project!

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question