1. VbScript | Message Box

Remember to try out the codes for yourself as an exercise. You lose what you don’t use, and the same can be said about knowledge.

Today we are going to talk about the MsgBox function in VbScript. It’s a useful command that allows you to communicate with the user.

Msgbox "Box Message",Type,"Title"

Breaking it Down: Msgbox is the dialog box. “Box Message” is the content that goes in the dialog box. Type is type of box shown. “Title” is what goes in the header of the dialog box.

Syntax Note: Capitalization & Spaces don’t matter; doesn’t affect the function of the script. Remember, separate lines do affect the script.

Example:

MsgBox "Box message",0,"title"

Output:

Capture2

Example2:

MsgBox "Bannana Smoothie",20,"Hello"

Output:

Capture3

Example3:

MsgBox "Shotgun",66

Output:

Capture4


MsgBox Button Feedback:

Have you noticed that the dialog boxes have yes, no, abort, etc. buttons? You can set your message boxes to give feedback depending on what buttons are pressed. You can do this by storing the chosen button value in a variable, and using if-then statements.

For example:

x = MsgBox "Would you like a Shotgun Smoothie?",36,"hello"
If x=6 then msgbox "Bam!!!"

Output:
Capture5

You get an error? What? Why, you may ask. That’s because when you are using a variable “x” you need to tell the program what the starting and ending point of the script are. You can do that by adding Parenthesis ()

Fixed Script:

x = MsgBox("Would you like a Shotgun Smoothie?",36,"hello")
If x=6 then msgbox "Bam!!!",64
If x=7 then msgbox "Too bad, I'll just have one myself",16

Output:

Notice that the Yes button has the value 6 & the No button has the value 7.


One thought to “1. VbScript | Message Box”

What's Your Opinion?