Performing Multiple Actions on an Object

You often need to perform several different actions on the same object. For example, you might need to set several properties for the same object. One way to do this is to use several statements.

Private Sub Form_Load()
   Command1.Caption = "OK"
   Command1.Visible = True
   Command1.Top = 200
   Command1.Left = 5000
   Command1.Enabled = True
End Sub

Notice that all these statements use the same object variable, Command1. You can make this code easier to write, easier to read, and more efficient to run by using the With...End With statement.

Private Sub Form_Load()
   With Command1
      .Caption = "OK"
      .Visible = True
      .Top = 200
      .Left = 5000
      .Enabled = True
   End With
End Sub

You can also nest With statements by placing one With...End With statement inside another With...End With statement.