With Statement
Language Items List
Definition:
Begins a block of statements that can use a shorthand notation for accessing
members of a particular object.
Syntax:
With object
[ statement-block ]
End With
Syntax Description
object Name of an object
statement-block Typical statement block
Details:
The With statement allows a block of code to refer to members of a specified object
without requalifying the name of the object each time. For example, to assign the
values of a set of properties on a certain object, it is easier (and more
efficient) to use a With statement block as shown:
With Button1
.Left = 100
If .Width > 1000 Then .Height = 1000
End With
Each time the “dot-notation” is used as a prefix to an identifier, it is equivalent to substituting the ‘object’ in front of the dot. The dot prefix acts as an implied object reference
variable, so the previous example above could also be written as:
Button1.Left = 100
If Button1.Width > 1000 Then Button1.Height = 1000
Note: It is illegal for the With dot-notation to be used outside of a With-block.
With statements can be nested. The object of the nested With statement can use the With dot-notation when specifying its object:
With Form1
With .Image1
With .Picture
.FileName = “bitmap.bmp”
End With
End With
End With
The inner statement is equivalent to:
Form1.Image1.Picture.FileName = “bitmap.bmp”