如何在 vba(access) 中将变量设置为我的表单列表框之一?

Posted

技术标签:

【中文标题】如何在 vba(access) 中将变量设置为我的表单列表框之一?【英文标题】:How do I set a variable to one of my form's listboxes in vba(access)? 【发布时间】:2013-06-28 13:27:52 【问题描述】:

我有一个函数,我想根据字符串参数返回不同的列表框。 这是函数:

    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

每当我尝试调用它时,都会收到“运行时错误 '91':对象变量或未设置块变量”。当我使用调试器时,它告诉我对 list61(Me.list61, Me.Controls("List61")) 的引用为空。

有人知道如何解决这个问题吗?任何帮助我都会非常感激。

【问题讨论】:

【参考方案1】:

最重要的是要注意;由于您现在处理的是“对象”而不是“变量”,因此您必须将“设置”一词放在对象变量的前面。还要将 AccessObject 类型更改为 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

【讨论】:

以上是关于如何在 vba(access) 中将变量设置为我的表单列表框之一?的主要内容,如果未能解决你的问题,请参考以下文章