Nils
Nils

Reputation: 67

Passing a ListBox control object to a function requiring a ListBox as argument

I am going over all control elemtens in my form and want to passt all ListBoxes to another function. The Code looks like this:

Dim ctl As Control
For Each ctl In Me.Controls
    If ctl.ControlType = acListBox Then

        Save (ctl)

    End If
Next ctl

Private Sub Save(list As ListBox)
    'Do something with list
End Sub

It gives me 424 Runtime Error, telling me that an object is needed.

When I try to use

Save (ctl.Object)

I'll get an 2455 Runtime Error.

EDIT: I also tryed casting the ctl Control Object to a ListBox Object with

Dim list As ListBox
Set list = ctl

Then it tells me the same as in my first example (Error 424)

Upvotes: 1

Views: 1338

Answers (1)

Fionnuala
Fionnuala

Reputation: 91356

Get rid of the brackets, so:

Dim ctl As Control
For Each ctl In Me.Controls
    If ctl.ControlType = acListBox Then

        Save ctl

    End If
Next ctl

VBA is very fussy about brackets.

I strongly recommend that you use something other than Save as the name of your function, it is very easy to disrupt VBA.

Upvotes: 1

Related Questions