Msgbox Example

This demonstrates displaying some message boxes using the MsgBox function. Add three command buttons called cmdExample1, cmdExample2 and cmdExample3. Add the code below, and then run your project

Option Explicit

Private Sub cmdExample1_Click()
Start:
    Dim Msg As String
    Dim Icon As Integer
    Dim Title As String
    Dim Buttons As Integer
    Dim Ans As VbMsgBoxResult
    ' Fill Message string
    Msg = "Select Abort to Cancel, Retry to try again and Ignore to ignore this problem" & Chr(vbKeyReturn)
    Msg = Msg & "This is the second line" & Chr(vbKeyReturn)
    Msg = Msg & "You can do this by using Char code 13"
    ' Set dialog Title
    Title = "Msgbox Example - Abort Retry Ignore"
    ' Set icon to critical (X)
    Icon = vbCritical
    ' Set buttons to abort, retry and ingnore
    Buttons = vbAbortRetryIgnore
    Buttons = Buttons + vbDefaultButton2 'set default button to retry
   
    Ans = MsgBox(Msg, Buttons + Icon, Title)
    'alternatively, simply
    'Ans = MsgBox(Msg, vbAbortRetryIgnore + vbDefaultButton2 + vbCritical, "Msgbox Example - Abort Retry Ignore")
   
    ' If the user clicked Retry, try again
    If Ans = vbRetry Then
        GoTo Start
    ' If the user clicked abort exit procedure
    ElseIf Ans = vbAbort Then
        Exit Sub
    End If
End Sub

Private Sub cmdExample2_Click()
    Dim Msg As String
    Dim Ans As VbMsgBoxResult
    ' Fill Message string
    Msg = "This may take a while. Click OK to continue" & Chr(vbKeyReturn)
    Msg = Msg & "If you click Cancel, to abort"
    ' Set icon to information (question mark)
    ' Set buttons to OK and Cancel
    ' Show Message Box
    Ans = MsgBox(Msg, vbOKCancel + vbInformation, "Msgbox Example - OK Cancel")
   
    ' If the user clicked cancel...
    If Ans = vbCancel Then
        MsgBox "Cancelled"
        Exit Sub
    End If
End Sub

Private Sub cmdExample3_Click()
    Dim Ans As VbMsgBoxResult

    ' Set icon to exclamation mark
    ' Set buttons to OK and Cancel
    ' Show Message Box
    Ans = MsgBox("Are you sure you want to continue?", vbYesNo + vbExclamation, "Msgbox Example - Yes No")
    ' If the user clicked no exit sub
    If Ans = vbNo Then
        Exit Sub
    End If
End Sub

You might also like...

Comments

James Crowley James first started this website when learning Visual Basic back in 1999 whilst studying his GCSEs. The site grew steadily over the years while being run as a hobby - to a regular monthly audience ...

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.” - Rich Cook