Pass Listbox by reference
Posted on 2006-06-02
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
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
If bck = True Then
mybtn.Enabled = True
mybtn.Enabled = False
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.