RVF
RVF

Reputation: 13

Pass ListBox Name as Parameter to use in a Function - ACCESS VBA

I am trying to create a function which delete all the Items listed in a ListBox. However, when I Try to pass the name of the ListBox, it returns an error: Inavlid Parameters.

Here is the code:

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

Here is how I am calling the function:

Call CleanList(List_List1.Name)

The name of the ListBox I am using is List_List1

If I don't type the Method .Name, it shows the first Item of the List. I have tried without the Parentheses, without success.

Could you help me?

Thanks and Regards!

Upvotes: 0

Views: 421

Answers (1)

Andre
Andre

Reputation: 27634

Your function doesn't accept a string (listbox name), but a Listbox object.

Which is the best way for a function like that.

So when calling the function, you only pass the object, not its name:

Call CleanList(Me!List_List1)

Upvotes: 1

Related Questions