Basics of VB Programming
Message Boxes are used to display information to the user. You see them all the time in Windows and other programs.
Message boxes can be called like this:
MsgBox Prompt, Buttons/Icon, Title
The prompt is basically what you want it to say, the buttons/icon is what buttons you want and which icon you want and title is really self explanatory. e.g.
Msgbox "This is a test.",vbCritical,"Title"
You can use the following constants for buttons:
- vbOkOnly
- vbOkCancel
- vbYesNo
- vbYesNoCancel
- vbAbortRetryIgnore
- vbRetryCancel
and these constants for icons:
- vbQuestion
- vbInformation
- vbExclamation
- vbCritical
An important thing that you need to be able to do is to translate what the user has done. e.g.
Dim iRes As Integer iRes=MsgBox("Test Question?",vbYesNo+vbQuestion,"Title" If iRes=vbYes Then Msgbox "The Yes button was clicked." Else Msgbox "The No button was clicked." End If
To include the contents of a control, such as a text box, you can use the ampersand character (&) to concentate the prompt string (see No Strings Attached). e.g.
Msgbox "The contents is:" & Text1.Text
Page 2 of 6
This article was originally published on November 20, 2002