Link to home
Start Free TrialLog in
Avatar of maxy88
maxy88

asked on

Pass Listbox by reference


Hello,

   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
    Else
        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.

Cheers,
Max.
Avatar of MageDribble
MageDribble

what line does the type mismatch occur on?
ASKER CERTIFIED SOLUTION
Avatar of MageDribble
MageDribble

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of maxy88

ASKER


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

Max.
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!