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
Comments