Send a suggestion!

We're building a brand new version of the site, and we'd love to hear your ideas

Members

Technology Zones

IBM Learning Center

Articles

Hosted By

MaximumASP

Info

Rated
Read 46,382 times

Related Categories

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

James first started writing tutorials on Visual Basic in 1999 whilst starting this website (then known as VB Web). Since then, the site has grown rapidly, and James has written numerous tutorials, articles and reviews on VB, PHP, ASP and C#. In October 2003, James formed the company Developer Fusion Ltd, which owns this website, and also offers various development services. In his spare time, he's a 3rd year undergraduate studying Computer Science in the UK. He's also a Visual Basic MVP.

Comments