Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set a variable to one of my form's listboxes in vba(access)?

Tags:

vba

ms-access

I have a function that I want to return different Listboxes based on a string argument. Here is the function:

    Here is the function:
Private Function returnList(name As String) As AccessObject
If name = "app" Then
    returnList = Me.Controls("List61")
    'I have also tried the following: 
    'returnList = Me.List61, returnList = Forms![Daily Reports]![List61]
ElseIf name = "lpar" Then
'..several more cases
End If
End Function

Whenever I try to call it, I get a "Run-time error '91': Object variable or With block variable not set." And when I use the debugger, it tells me that the reference to list61(Me.list61, Me.Controls("List61")) is null.

Anyone have any idea how to fix this? Any help would me much appreciated.

like image 911
James Liu Avatar asked Dec 07 '25 06:12

James Liu


1 Answers

The most important thing to note is; as you are now handling "Objects" instead of "variables" you have to put the word "Set" in front of the object variable. Also change the AccessObject type to ListBox.

Private Function returnList(name As String) As ListBox
If name = "app" Then
    Set returnList = Me.Controls("List61")
    'I have also tried the following:
    'returnList = Me.List61, returnList = Forms![Daily Reports]![List61]
ElseIf name = "lpar" Then
'..several more cases
End If
End Function
like image 113
TØny Hine Avatar answered Dec 08 '25 18:12

TØny Hine