IT tutorials
 
Technology
 

Windows Server 2008 : Creating Basic Visual Basic Scripts - Using if Statements, Checking for a Value with a Message Box

8/24/2013 9:25:01 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

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

if condition then action

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 LinesExplanation
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

This section provided only some basic information on the VB Scripting language. If you want to dig deeper, check out this TechNet page: http://msdn.microsoft.com/library/d1wf56tt.aspx.


Tip

Any script you create can be scheduled to run through Group Policy or with the Task Scheduler. 

 
Others
 
- Windows Server 2008 : Creating Basic Visual Basic Scripts - Displaying a Message Box with a Visual Basic Script
- Windows Server 2008 : Creating Basic Visual Basic Scripts - Working with filesystemobject
- Windows 7 : Installing and Upgrading Programs - Common Installation Prompts (part 2) - Type of Installation
- Windows 7 : Installing and Upgrading Programs - Common Installation Prompts (part 1) - Compliance check , The End User License Agreement
- Windows 7 : Installing and Upgrading Programs - Installing and Upgrading from a Disk
- Windows Server 2012 : Using Capacity-Analysis Tools - Other Microsoft Assessment and Planning Tools, Third-Party Toolset
- Windows Server 2012 : Using Capacity-Analysis Tools - Windows Performance Monitor
- Windows Server 2012 : Using Capacity-Analysis Tools - Network Monitor (part 2) - Capturing Network Traffic Between Computers , Parsing Captured Network Traffic Data
- Windows Server 2012 : Using Capacity-Analysis Tools - Network Monitor (part 1) - Using Network Monitor 3.4
- Windows Server 2012 : Using Capacity-Analysis Tools - Task Manager
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us