将 ListBox 名称作为参数传递以在函数中使用 - ACCESS VBA

问题描述

我正在尝试创建一个删除列表框中列出的所有项目的函数。但是,当我尝试传递 ListBox名称时,它返回错误:Inavlid Parameters。

代码如下:

Public Function CleanList(ListName As ListBox)
Dim intItemsInList As Integer
Dim intCounter As Integer

intItemsInList = ListName.ListCount

For intCounter = 0 To intItemsInList - 1
    ListName.RemoveItem 0
Next

End Function

这是我调用函数的方式:

Call CleanList(List_List1.Name)

我使用的 ListBox名称List_List1

如果我不输入方法 .Name,它会显示列表的第一项。我试过没有括号,但没有成功。

你能帮我吗?

谢谢和问候!

解决方法

您的函数不接受字符串(列表框名称),而是一个列表框对象。

对于这样的函数来说,这是最好的方法。

所以在调用函数时,你只传递对象,而不是它的名字:

Call CleanList(Me!List_List1)