Read our blogs, tips and tutorials
Try our exercises or test your skills
Watch our tutorial videos or shorts
Take a self-paced course
Read our recent newsletters
License our courseware
Book expert consultancy
Buy our publications
Get help in using our site
547 attributed reviews in the last 3 years
Refreshingly small course sizes
Outstandingly good courseware
Whizzy online classrooms
Wise Owl trainers only (no freelancers)
Almost no cancellations
We have genuine integrity
We invoice after training
Review 30+ years of Wise Owl
View our top 100 clients
Search our website
We also send out useful tips in a monthly email newsletter ...
Once you've captured the user's response you'll need to test which button they clicked in order to work out what to do next.
It's vital to be able to test which button the user clicks.
Click here to download the file needed for this part of the lesson.
Click here to download the sample code used on this page.
Extract and open the workbook linked to in the Files Needed section above. In the VBE, find the subroutine called TestWhichButtonWasClicked in Module1.
After you've captured which button was clicked, all you need to do is test which one it is. Add an If statement at the bottom of the TestWhichButtonWasClicked subroutine, as shown in the diagram below:
If you've used the VbMsgBoxResult data type for your variable, you should receive some help when it comes to testing the result.
You can explicitly test for as many or as few buttons as you need. Complete the If statement according to the example shown below:
This simple example tests whether the user clicked Yes or No and displays an appropriate message in each case.
Run the subroutine a couple of times to check that you see the correct result depending on which button you click.
VbMsgBoxResult is an example of an enumeration: a collection of related constants. Each constant in this enumeration refers to one of the possible buttons that can be clicked on a message box. Each constant has both a name and an underlying numeric value, which you can see in the table below:
Constant | Value |
---|---|
vbOK | 1 |
vbCancel | 2 |
vbAbort | 3 |
vbRetry | 4 |
vbIgnore | 5 |
vbYes | 6 |
vbNo | 7 |
This means that you could also test for the result of a message box using a number.
This code works but is less easy to read than the previous example.
To practise testing the result of a message box:
Sub ControversialQuestion()
Dim Answer As VbMsgBoxResult
Answer = MsgBox( _
"Is it ever OK to put pineapple on pizza?", _
vbYesNo + vbQuestion, _
"Moral Quandary")
End Sub
You could ask a less controversial question, such as:
Sub ControversialQuestion()
Dim Answer As VbMsgBoxResult
Answer = MsgBox( _
"Is it ever OK to put pineapple on pizza?", _
vbYesNo + vbQuestion, _
"Moral Quandary")
If Answer = vbYes Then
MsgBox _
"Wrong - it is an affront to nature!", _
vbCritical
ElseIf Answer = vbNo Then
MsgBox _
"Correct - they should only be used for pina coladas"
End If
End Sub
This is the message you should see if you click Yes. I should confess that I actually like pineapple on pizza.
Sub ControversialQuestion()
Dim Answer As VbMsgBoxResult
Answer = MsgBox( _
"Is it ever OK to put pineapple on pizza?", _
vbYesNoCancel + vbQuestion, _
"Moral Quandary")
If Answer = vbYes Then
MsgBox _
"Wrong - it is an affront to nature!", _
vbCritical
ElseIf Answer = vbNo Then
MsgBox _
"Correct - they should only be used for pina coladas"
Else
MsgBox _
"You should really pick a side!", _
vbExclamation, _
"Taking the coward's way out"
End If
End Sub
Kingsmoor House
Railway Street
GLOSSOP
SK13 2AA
Landmark Offices
99 Bishopsgate
LONDON
EC2M 3XD
Holiday Inn
25 Aytoun Street
MANCHESTER
M1 3AE
© Wise Owl Business Solutions Ltd 2024. All Rights Reserved.