Pass Listbox by reference

Posted on 2006-06-02
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
    LVL 5

    Expert Comment

    what line does the type mismatch occur on?
    LVL 5

    Accepted Solution

    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)
    LVL 6

    Author Comment


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

    LVL 5

    Expert Comment

    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!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Today's users almost expect this to happen in all search boxes. After all, if their favourite search engine juggles with tens of thousand keywords while they type, and suggests matching phrases on the fly, why shouldn't they expect the same from you…
    Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
    Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
    What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.

    759 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

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now