Consulting

Results 1 to 4 of 4

Thread: Solved: How to Access Objects/Tools (ie ListBoxes) of a UserForm?

  1. #1
    VBAX Regular
    Joined
    Jun 2006
    Posts
    26
    Location

    Solved: How to Access Objects/Tools (ie ListBoxes) of a UserForm?

    I want to be able to put various ToolBox Controls (ie TextBoxes and ListBoxes) of a UserForm into a collection, but I am not sure how to access/call specific controls.

    For example, if I have three textboxes named Box1, Box2, and Box3, how can I put these into a collection so I can selectively turn off Box3 if it is not required, without creating explicit select/case logic.

    Below is an example sub that I thought would work, VBComp is filtered to only examine UserForms, but I don't know how to go deeper to find the Controls on the userform. I thought VBComp.Collection would work, but it did not (for me)

    [vba]Sub ListAllFormComponents()
    Dim VBComp As VBComponent
    Dim Msg As String
    Dim CompItem As Object

    For Each VBComp In ThisWorkbook.VBProject.VBComponents
    If (VBComp.Type = vbext_ct_MSForm) Then
    Msg = Msg & VBComp.name & vbCrLf
    For Each CompItem In VBComp.Collection
    Msg = Msg & vbTab & CompItem.name & vbCrLf
    Next CompItem
    End If
    Next VBComp
    MsgBox Prompt:=Msg
    End Sub[/vba]
    Last edited by nitt1995; 06-21-2006 at 09:29 AM. Reason: clarification

  2. #2
    Knowledge Base Approver
    The King of Overkill!
    VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Hi nitt,
    Those items are referred to as controls in vba, so you would use:[vba]'For Each CompItem In VBComp.Collection
    For Each CompItem In VBComp.Controls[/vba]Matt

  3. #3
    VBAX Regular
    Joined
    Jun 2006
    Posts
    26
    Location
    Thanks!

  4. #4
    Knowledge Base Approver
    The King of Overkill! VBAX Master
    Joined
    Jul 2004
    Location
    Rochester, NY
    Posts
    1,727
    Location
    Glad to help! Gotta love the VBA syntax

Posting Permissions

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