Module 5 - Messages and User Inputs
Lesson 5.2 - Capturing the User Response
Topic 5.2.4 - Setting a Default Button

When a message box is displayed you can press Enter on the keyboard to click its default button.  The default button will usually be the left-most one of the set:

Default button

Connoisseurs of 1980s movies will appreciate the importance of not accidentally choosing  Yes when asked this question by a computer...

 

Files Needed

You don't need any files for this part of the lesson.

Completed Code

You can click here to download a workbook containing the sample code.

Setting a Default Button

You can set a different button as the default by adding a constant in the Buttons parameter of a message box.  In a new workbook, insert a module and create a subroutine called DefaultButton.  Add code to display a message box as shown below:

Choose default button

You can add a default button option to the Buttons parameter by adding it to the choice of buttons with the + symbol.

 

Including an Icon

You can combine a choice of default button with a choice of icon:

Button default and icon

Specify your choice of buttons, default button and icon in the Buttons parameter.

 

Running the above code displays a message box which resembles the one shown below:

Final message box

The only winning move is not to play.

 

To practise setting a default button on a message box:

  1. Using any workbook, add a new subroutine called IfAtFirst.
  2. Add code which displays a message box with a customised prompt, icon and buttons:

Sub IfAtFirst()

 

MsgBox _

"If at first you don't succeed...", _

vbQuestion + vbAbortRetryIgnore

 

End Sub

  1. Extend the code so that the message box defaults to the Retry button:

Sub IfAtFirst()

 

MsgBox _

"If at first you don't succeed...", _

vbQuestion + vbAbortRetryIgnore + vbDefaultButton2

 

End Sub

  1. Run the subroutine to check that your message appears as expected:
Default retry

Feel free to set one of the other buttons as the default if you have a different attitude to failure.

 
  1. Save and close the workbook.
This page has 0 threads Add post