Consulting

Results 1 to 3 of 3

Thread: ListBox Control is Inexplicably Null

  1. #1
    VBAX Contributor
    Joined
    Aug 2012
    Posts
    120
    Location

    ListBox Control is Inexplicably Null

    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?

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    VBAX Contributor
    Joined
    Aug 2012
    Posts
    120
    Location
    That does work. Weird.

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

    Thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •