Problems using controls within VBA Code

KjWhal

Registered User.
Local time
Today, 15:08
Joined
Jan 5, 2007
Messages
60
Hi,

I have a listbox on my form named participantsListBox

I want to do the following

dim selectedControl as Control

selectedControl = Me.Controls!participantsListBox

However I cannot do this because participantsListBox is null.

What is the error in my logic / how do I do what I'm aiming for?
 
I'm guessing that the listbox is multiselect. If so, it will always be null. If it doesn't need to be multiselect, changing that is your easiest fix.
 
pbaldy said:
I'm guessing that the listbox is multiselect. If so, it will always be null. If it doesn't need to be multiselect, changing that is your easiest fix.

It does need to be multiselect.

What I'm not following is, if a control is an object, and the listbox is a control, why can I not declare an object and set it equal to (by reference or value) to a control on the form?
 
Maybe because the syntax is incorrect?
Try this instead:
Code:
Dim selectedControl as Control

selectedControl = Me.Controls ("participantsListBox")
 
boblarson said:
Maybe because the syntax is incorrect?
Try this instead:
Code:
Dim selectedControl as Control

selectedControl = Me.Controls ("participantsListBox")

The syntax is correct. There are two methods to explicitly refer to a control, yours being the latter.

I tried it with yours though and it has the same result. The listbox is null and thus the Control will be set to null as well.
 
Like I said, a multiselect listbox will ALWAYS be null. Search here on multiselect and you should turn up the code to loop through the selected items.
 

Users who are viewing this thread

Back
Top Bottom