Results 1 to 2 of 2
  1. #1
    Join Date
    May 2004
    Posts
    19

    Unanswered: Looping through form objects

    Hi folks,

    If I have several objects on a form and I wish to make them visible/invisible how best can I do this using a For Next loop?

    E.g. lblCustomer1, lblCustomer2 I deliberately placed numbers on the end in order to set that using the loop controller, but access doesn't like that and prefers me to type in the hard name, which would mean doing the same line of code 40 times or so.

    Any tips or suggestions on this would be gratefully accepted.

  2. #2
    Join Date
    Sep 2004
    Location
    Reston, VA
    Posts
    86
    You can try something like this, which will loop through all the objects on your form and perform an action depending on the control type and/or a control tag value you've entered for certain controls (i.e., entering 1 as the tag for a group of controls of different types that you want to hide).

    Code:
    Dim ctl as Control
    
    For Each ctl in Me.Controls
    
       'check the control type and do some action
       Select Case ctl.ControlType
          Case acTextBox
             code...
          Case acLabel
             code...
          Case acComboBox
             code...
       End Select
    
       'or check the control tag and do some action
       Select case ctl.Tag
          Case 1
             code...
          Case 2
             code...
       End Select
    
    Next ctl
    
    'Destroy the ctl object
    Set ctl = Nothing
    If you're not using it in a form module, just replace Me.Controls with Forms!formname!Controls.

    You can get a list of the acControlType enumeration constants in the VBA object browser (press F2 while in the VBA editor and search for "acControlType")

Posting Permissions

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