Results 1 to 7 of 7
  1. #1
    Join Date
    Aug 2003
    Posts
    123

    Unanswered: Check for control type

    How do I check for the type of control again--i.e. if cycling through several controls, how do I check whether each one is a text box, check box, combo, etc.?

  2. #2
    Join Date
    May 2005
    Location
    Nevada, USA
    Posts
    2,888
    Provided Answers: 6
    Look at the ControlType property in VBA Help.
    Paul

  3. #3
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    This code will print out (in the Immediate Window) the control names and types for most of the common types of controls. You can adapt the code to do whatever you want, of course. I couldn't find a list of ControlType numbers, but the ones listed cover the vast majority of control types.

    Code:
    Private Sub PrintOutControlNameAndType_Click()
    Dim ctl As Control
    Dim CT As String
    For Each ctl In Me.Controls
      
        Select Case ctl.ControlType
     
        Case 100
          CT = "Label"
        Case 101
          CT = "Rectangle"
        Case 104
          CT = "Command Button"
        Case 106
          CT = "Check Box"
        Case 109
          CT = "Text Box"
        Case 110
          CT = "List Box"
        Case 111
          CT = "Combo Box"
        Case 123
          CT = "Tab control"
        Case 124
          CT = "Tabbed Page"
        End Select
    
        Debug.Print ctl.Name & "  " & CT
         
    Next ctl
    End Sub
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

  4. #4
    Join Date
    Jan 2007
    Location
    California, USA
    Posts
    520
    When you "Look at the ControlType property in VBA Help" you should find there the VBA constants that Microsoft has given us to refer to each type of control rather than using a number. Microsoft has already coded that number into the constants they provide, plus it makes for better self-documenting code.

  5. #5
    Join Date
    Aug 2003
    Posts
    123
    I guess what I'm looking for is a way to ignore certain controls when going through and clearing or setting default values. So for instance, I want to set all checkboxes to "checked", but ignore all text boxes.

    For Each cntl in Forms!frmThisForm
    If cntl.ControlType = CheckBox??
    CheckBox=0
    Else
    End If
    Next

  6. #6
    Join Date
    May 2005
    Posts
    1,191
    From VB help under AcControlType, the enumerated types for each are as shown in the attachment (as Vic was pointing to):

    So just do like:
    Code:
    Dim ctl As Control
    For Each ctl In Form.Controls
        If ctl.ControlType = acCheckBox Then
            ctl = False
        End If
    Next
    Attached Thumbnails Attached Thumbnails acControlTypes.JPG  
    Last edited by nckdryr; 05-30-07 at 12:55.
    Me.Geek = True

  7. #7
    Join Date
    Jun 2005
    Location
    Richmond, Virginia USA
    Posts
    2,763
    Provided Answers: 19
    Why didn't you say so, jrn0074? I could have posted the answer to you real question, which nckdryr did, yesterday, in about 30 seconds!

    BTW, if you ever want to do something like this, but you only want to change certain checkboxes (for instance) but not all checkboxes, do something like this:

    For the checkboxes you DON'T want affected, goto to their Property Box -- Other and in the Tag Property put skip.Then in code:

    Code:
    Private Sub YourButton_Click()
    
    Dim ctl As Control
    
    For Each ctl In Me.Controls
        If TypeOf ctl IsCheckBox Then
           If ctl.Tag <> "skip" Then
              ctl = False 
           End If
        End If
    Next
    
    End Sub
    Then you'll only uncheck the checkboxes you haven't tagged!

    Glad you got it figured out!
    Last edited by Missinglinq; 05-30-07 at 13:47.
    Hope this helps!

    The problem with making anything foolproof...is that fools are so darn ingenious!

    All posts/responses based on Access 2003/2007

Posting Permissions

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