How to pass listbox name as parameter in a function access vba

svuyyuru

Registered User.
Local time
Today, 12:57
Joined
Feb 10, 2014
Messages
33
I've a method **querylistboxitems** and i want to call this method in several click events, only difference is listbox,dropdown values change based on the event i call.
Code:
Public Sub querylistboxitems(lstbox As listbox, dropdown As ComboBox)
 
     Dim drpdwnvalue As String
     drpdwnvalue = dropdown.Value
     With lstbox
 
     //do something
     End with
    End Sub
And i'm calling this in the buttion click event by passing the listbox names as **List_Compare** and **Select_CM_Compare**
Code:
     Private Sub Command_compare_Click()
     Call querylistboxitems_1(List_Compare, Select_CM_Compare)
     End Sub
But the values passing to the function are not control names, control values i.e corresponding control selected values.
I want to use listbox name in **lstbox**, not the value.
 
The problem is you have put parentheses around the parameters in the Call. They cause the value to be passed. Just leave them out.

BTW Your Sub is expecting the objects, not their names.

You should also refer to their context with Me.
 
Do you use Option Explicit ?
I ask because this:
Public Sub querylistboxitems(lstbox As listbox, dropdown As ComboBox)
don't look good.
Also note that "dropdown" is a reserved word (hope I remember well).
 
Last edited:
I read the symptoms but didn't look closely enough at your question so I was off the mark about the parentheses before.

However you are calling a different Sub from the one described.

You also stated you are passing the names. The Sub is expecting objects and they should be passed as Me.List_Compare etc.

You need to show more of your code.

BTW Using underscores in your object names makes your code harder to read. I know some developers use and it is legal but in my view it is a very poor choice.

The underscore is already used to separate the object from its Event so it is not an appropriate character in a name.
 
BTW Dropdown is a Method of a combobox but it isn't actually a reserved word as such. Still a good name to avoid.
 

Users who are viewing this thread

Back
Top Bottom