Microsoft Office Tutorials and References
In Depth Information
Understanding VBA Syntax
Here are the most important things to know about syntax charts:
✦ In the Quick Info syntax chart, boldface indicates the part that you’re
about to type.
✦ Items in square brackets are optional and can be omitted from the
statement you’re typing.
✦ Never type the square brackets in your statement.
✦ If you skip an optional part, you must still type the comma.
Arguing with VBA
Each part that follows the keyword is generally referred to as an argument.
Most arguments are actually expressions, similar to expressions used in
calculated controls. A string expression can be a literal string enclosed in
quotation marks (such as “Smith” or “Jones”); the name of a field; or, for that
matter, the name of a variable that contains a string, such as [LastName]
(which refers to a field in a table).
We describe variables in Chapter 3 of this minibook.
A numeric expression can be anything that results in a valid number. 10 is a
valid numeric expression, as is 2*5 (two times five), as is the name of a field
or variable that contains a number.
Given all this information, take a look at some MsgBox statements that
follow the rules of syntax and are considered to be valid:
MsgBox(“Slow, children at play”)
The preceding statement is valid because the one-and-only required named
argument — the Prompt argument — is included. When executed, the
MsgBox() statement displays that prompt, as shown in Figure 2-4.
As you scroll through help for the MsgBox function, you’ll find many ways
to use that function. The following statement, for example, is perfectly valid
syntax for the MsgBox keyword. vbYesNo is the setting that tells the box to
display Yes and No buttons (and replaces the placeholder text buttons in
the MsgBox syntax chart), and “Question” is literal text that appears in the