PDA

View Full Version : [SOLVED:] ListBox Control is Inexplicably Null



Mavila
12-08-2019, 07:59 PM
I have a UserForm that I'm trying to use a generic control object to fill ListBox controls.

I use "dim cntrl as Control" in the declarations of my UserForm.

I call the UserForm with, "Call frmPrograms.Initialize" as the sub routine that sets the controls before displaying the UserForm (UserForm.Show).

Then, I use Set cntrl = myListBox.

But, myListBox is "Null".

So, it doesn't work. I've read about this and it seems there might be an ambiguity issues between ListBox control and Active X ListBox control. I've tried to eliminate the ambiguity by using frmPrograms.myListBox but that doesn't work. I've tried adding an item to the ListBox control to SetFocus and that doesn't work.

If I set cntrl to another control, such as command button, it recognizes it (i.e., not Null). This leads me to believe that there might be something to the ambiguity issue, but I'm lost at this point.

Anyone have any ideas what might be going on here?

gmayor
12-08-2019, 09:47 PM
Try the following


Private Sub UserForm_Initialize()Dim oCtrl As Control
For Each oCtrl In Controls
If oCtrl.Name = "myListBox" Then
'do something with octrl e,g,
oCtrl.AddItem "This is an item"
Exit For
End If
Next oCtrl
End Sub

Mavila
12-09-2019, 10:21 AM
That does work. Weird.

Not exactly what I was looking for but I think I can make that work fairly well.

Thanks.