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
551 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 ...
Displaying different buttons on a message box allows you to ask users important questions:
Many users in the UK will be curious as to why there is a No option on this message.
You don't need any files for this section.
You can click here to download a workbook containing the completed code.
Create a new workbook and insert a new module in the VBE.
By default, a message box will display only an OK button. You can choose from several other combinations of buttons using the Buttons parameter. Create a new subroutine called ChoosingDifferentButtons and add code to display a message box which displays different buttons:
After entering a prompt for the message, type a comma to see a list of button options. Choose the vbYesNo option.
If the list shown above disappears, you can press CTRL + SHIFT + J to display it.
Running the code will display the message box shown below. It doesn't matter which button you click as we haven't captured the result yet!
Yes is, of course, the correct response. Unfortunately, our code has no way to detect our response yet.
You can use named arguments to make the code easier to read. Add a second message box to the same subroutine using the code shown below:
Here we've written the parameter name in front of each argument. We've also used continuation characters (a space and underscore) to write the code on multiple lines.
The above code produces a message box resembling the one shown below:
Users from the US should note that a biscuit is equivalent to a cookie.
You can see the available button combinations in the table below:
Constant | Buttons |
---|---|
vbAbortRetryIgnore | |
vbOKCancel | |
vbOKOnly | |
vbRetryCancel | |
vbYesNo | |
vbYesNoCancel |
You can combine a choice of buttons with a choice of icon on the same message box:
You could replace Brexit with Trump or North Korea for a more international flavour.
To do this, create a subroutine called ButtonsAndIcons. Add code to display a message box as shown below:
You can add together a choice of buttons and icon using the + symbol.
Running the above code results in a message box which resembles the one shown below:
You can specify one set of buttons and one icon per message box.
You can include a title on the message box as well as a prompt, buttons and an icon. Add a second message box to the same subroutine using the code shown below:
Named arguments make it easier to see what's going on.
Running the code shown above produces a message box with a customised title, icon and buttons:
Not that anyone will read the title.
To practise choosing which buttons to display on a message box:
Sub AskYourselfOneQuestion()
MsgBox _
"Do I feel lucky?" & vbNewLine & _
"Well, do you, punk?"
End Sub
Sub AskYourselfOneQuestion()
MsgBox _
"Do I feel lucky?" & vbNewLine & _
"Well, do you, punk?", _
vbYesNo, _
"You've got to ask yourself one question:"
End Sub
Choose your answer carefully...
To practise displaying buttons and an icon on a message box:
Sub KnowMore()
MsgBox _
"Would you like to know more?"
End Sub
Sub KnowMore()
MsgBox _
"Would you like to know more?", _
vbQuestion + vbYesNo
End Sub
You should probably click Yes if you'd like to continue with the course.
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.