Message Boxes & Input Boxes

In this section you will learn about Message boxes & Input boxes...

Visual Basic has intrinsic functions, which are functions that are built in VB. The "functions" job is really just to save you some time. Two basic functions that you will be learning now are the MsgBox() & the InputBox().

Message Boxes

A message box is a dialog box that your program can pop up to give the user information, and can be used to give your program feedback from the user.

error preview

Here's the code for a basic message box:

Private Sub Command1_Click()

    MsgBox "example's msg", vbCritical, "example's title"

End Sub

The first part in the code is the message you give the user, the second part is the icon shown in the msgbox, and the third part is the title. This is just a simple example though...

MsgBox icons and values

16 vbCritical
32 vbQuestion
48 vbExclamation
64 vbInformation

MsgBox Buttons and values

0 vbOKOnly
1 vbOKCancel
2 vbAbortRetryIgnore
3 vbYesNoCancel
4 vbYesNo
5 vbRetryCancel

Those are the buttons and icons that can be used with MsgBoxes, the values are really just short cuts...

MsgBox "example's msg", 16 + 4, "example's title"

     and

MsgBox "example's msg", vbCritical + vbYesNo, "example's title"

     are the same...

Return values

1 vbOk
2 vbCancel
3 vbAbord
4 vbRetry
5 vbIgnore
6 vbYes
7 vbNo

Note: Return values are not to be used in the MsgBox code, but to be used outside of it to check what the user pressed. For example if you asked the user "Do you want to show form2?", you would use the return value as shown below...

Private Sub Command1_Click()

  MyMsgBox = MsgBox("Do you want so see form2?", vbQuestion + vbYesNo, "Conform")

    If MyMsgBox = 6 Then '6 is the vbYes button, if it was clicked then form2 will be shown

     Form2.Show

  End If

End Sub

Tip: Notice the "+ vbYesNo" those are the buttons I wanted to use, and that's where the code goes to show what buttons you want to show

InputBoxes

On to InputBoxes we go... Well, InputBoxes are very simple to comprehend, they are basically like MsgBoxes, but easier.

Private Sub Command1_Click()

    label1.Caption = InputBox("Whats your name?", "Enter your name", "Jay")

End Sub

input box preview

The first part in the code is the message you say, ask, whatever, the second part is the title of the Inputbox, and the third part is what you want in the input boxes text...

That's basically it for that. Now you've learned Input Boxes, but more importantly MsgBoxes.


Comparison operators                                   Loops