Accessing Controls Through the Controls Collection Example

The following example accesses individual controls from the Controls collection using a For Each...Next loop. When the user presses CommandButton1, the other controls are placed in a column along the left edge of the form using the Move method.

To use this example, copy this sample code to the Script Editor of a form. To run the code you need to open the form so the Open event will activate. Make sure that the form contains a CommandButton named CommandButton1 and several other controls.

Dim CtrlHeight 
Dim CtrlTop 
Dim CtrlGap
Dim CommandButton1 

Sub Item_Open()
  Set CommandButton1 = Item.GetInspector.ModifiedFormPages("P.2").CommandButton1

  CtrlHeight = 20
  CtrlGap = 5
    
  CommandButton1.Caption = "Click to move controls"
  CommandButton1.AutoSize = True
  CommandButton1.Left = 120
  CommandButton1.Top = CtrlTop
End Sub

Sub CommandButton1_Click()
  Dim MyControl

  Set AllControls = Item.GetInspector.ModifiedFormPages("P.2").Controls

  CtrlTop = 5

  For i = 0 to AllControls.Count - 1
    Set MyControl = AllControls(i)
    If MyControl.Name = "CommandButton1" Then
    'Don't move or resize this control.
    Else
      'Move method using unnamed arguments (left, top, width, height)
      MyControl.Move 5, CtrlTop, ,CtrlHeight
            
      'Calculate top coordinate for next control
      CtrlTop = CtrlTop + CtrlHeight + CtrlGap
    End If
  Next

End Sub