Message and Input Boxes

[ Using VB ]

    Message Boxes and Input Boxes are a common thing to see in Visual Basic programs.  You may see a message box every time you get an error, or some other helpful information.  Input boxes are great for getting user input.  This page will show you how each works and and example.

Message Boxes
Input Boxes


Message Boxes

    The Message Box has these parts:

MsgBox(prompt[, buttons] [, title] [, helpfile, context])

    Prompt is the message of the Message Box

    Title is the title of the message box.

    Helpfile and Context is where at in a helpfile the user can find help on that message box.

    Buttons is the only tricky part of the message box. 

First, to understand what the buttons is asking for, you must understand what a message box can look like.  It can have 4 icons, Critical, Question, Exclamation, and Information.  It can have a variety of buttons for the user to click on which include: OK, OK Cancel, Abort Retry Ignore, Yes No Cancel, Yes No, and Retry Cancel.  You can also select which of the buttons is the default. 

All these options are selected by putting one number into the buttons are of the message box.  To get that number, add the number that goes with each option you want.

OK 0
OK and Cancel 1
Abort, Retry, and Ignore 2
Yes, No, and Cancel 3
Yes and No 4
Retry and Cancel 5
Critical Critical.gif (955 bytes) 16
Question Question.gif (987 bytes) 32
Exclamation Exclamation.gif (978 bytes) 48
Information Information.gif (979 bytes) 64
Button 1 as default 0
Button 2 as default 256
Button 3 as default 512
Button 4 as default 768

For example, if you want a Yes or No answer (value of 4) and a question mark (value of 32), then you would just add 4 and 32 (32 + 4 = 36) to get 36 and put that in the Buttons field.  You can put the following code behind your Exit button or mnuFileExit command:

                    Dim intUserResponse As Integer
                    intUserResponse = MsgBox("Do you want to exit?", 36, "Exit?")
                    Select Case intUserResponse
                    Case 6    'If the user clicks "Yes" then...
                        End     'Quit the program
                    Case 7    'If the user click on No....
                        Exit Sub    'Then Exit the sub, not the program.
                    End Select

wpe3.jpg (4689 bytes)

How do you know that 6 is the value for if the user clicked Yes?   Just use the values in this chart:

OK

1
Cancel 2
Abort 3
Retry 4
Ignore 5
Yes 6
No 7

Input Boxes

    Input Boxes are great for getting user input.  If you need a password, name, or other info., this is a very easy way of getting it.  The syntax for an InputBox is:

InputBox(prompt[, title] [, default] [, xpos] [, ypos] [, helpfile, context])

    The prompt is the message the input box has.   This tells the user what the program wants them to input.  (ex:   "Please enter your password."). 

    The title is the title of the input box, which is the part that appears in the blue.  (ex:  "Password").

   Default is an option you may want to use.  It puts text in the text box part of the InputBox as a default.  (ex:  "Anonymous" is a good default if you're asking for the user's name). 

    Xpos and ypos are the horizontal and vertical positions of the input box.  If you leave these out, the default puts the input box horizontally centered and about one third of the way down the screen.

    Helpfile and context tell where in a help file the user can get help on that Input Box.

                    Dim strUserPassword As String

                    strUserPassword = InputBox("Please enter your password", "Password")
                    If strUserPassword = "Jessica" Then
                        ..............

wpe6.jpg (7089 bytes)