1. Using if Statements
You can use if statements to add choices to your script. The if statement tests for a condition, and when the condition is true, it takes the specified action. The basic syntax is
You can also add else and elseif statements. If you add else and elseif statements, you must end with an end if statement. The extended syntax is
if condition then action
elseif condition then
action
else condition then
action
endif
Tip
The spaces before the elseif, else, and action statements are not necessary; however, they do make the code easier to read.
The following section shows how to use the if elseif statement block to check which button a user pressed from a message box.
2. Checking for a Value with a Message Box
You can identify which button a user presses by
checking for a value after the message box is dismissed. The following
code shows a basic check:
intbutton = msgbox ("Do you want to continue", vbquestion +
vbyesno,"Question")
If intbutton = 6 then
msgbox "User pressed Yes",,"Answer"
elseif intbutton = 7 then
msgbox "User pressed No",,"Answer"
end if
Code Lines | Explanation |
---|
intbutton = msgbox ("Do you want to continue", vbquestion + vbyesno, "Question")
| The first line assigns a value to the intbutton variable based on which button the user presses.
Tip
When using the msgbox in an assignment statement (assigning the result to a variable), you must enclose it in parentheses. However, if you use the msgbox statement by itself, you must omit the parentheses.
|
If intbutton = 6 then msgbox "User pressed Yes",vbokonly, "Answer" elseif intbutton = 7 then msgbox "User pressed No",,"Answer" end if
| The second line is an if elseif statement. It checks for the two possible values (6 for yes and 7 for no).
Although
this code simply displays another message box to indicate which button
the user pressed, you can put in any other code here desired.
Note
The msgbox style (between the message and the title text) is omitted in the last example with two commas. The default is vbokonly to display an OK button.
|
Tip
Any script you create can be scheduled to run through Group Policy or with the Task Scheduler.