How to use the MsgBox function to personalize messages to the user

Who I am
Shane Conder
Author and references

How to use the MsgBox function to customize messages for the user

The first thing you should know about MsgBox is that they are created using the project code. The code is placed inside an object and has a specific syntax. Fortunately, the syntax is pretty straightforward, and you don't need any additional programming skills.

Syntax of a MsgBox

MsgBox (text [, button (s)] [, title] [, help file, context])

  • Message (prompt): is the text that contains the message you want to communicate to the end user.
  • Title: is optional and is the text for the name of the message. It may be useful to give a reference to the user indicating the relationship of the message.
  • Buttons: it is also optional and if not configured, the Accept button will be displayed by default. Some of the constants that can be configured for buttons are:

  • Help file (HelpFile):  is the text string that contains the location of the help file for the message. It is optional, but in case it is placed, the Context must be specified.
  • Context: the numeric value assigned to the help topic. It is related to the help file.

MsgBox topic

There are several ways to assign values ​​to a MsgBox argument. An easy way to do this is to use the constant name:

Private Sub CommandButton1_Click()
text = "Do you want to log out of the system?" 'message to user
style = vbYesNo + vbCritical + vbDefaultButton2 ′ types of buttons
title = "MsgBox Test message" 'message title
help = «DEMO.HLP» 'define help file
Ctxt = 1000 'Defines the context argument.
Response = MsgBox (text, style, title, help, Ctxt) 'MsgBox sent to the user
If Response = vbYes Then 'If user selects Yes
lbltexto.Caption = «Excellent» 'The action being performed
Otherwise 'If the user
selects no lbltexto.Caption = "Nothing happens" 'text message that is displayed if the user says no
End If

End subtitle

The previous code assigns the MsgBox function to a button on the form. When the user clicks the button, they will see a message asking if they want to exit the system with two action buttons (Yes and No), a critical message icon.

If the user replies that the word "Excellent" will be displayed on a label and if the answer is No, the message that is displayed is "Nothing is happening".

Another way to show the topic of a MsgBox is enter the value of each constant or add the value of the arguments. The previous form is simpler because it allows the code to be interpreted with the naked eye.

Return values ​​from MsgBox

The important thing is to determine what type of value is associated with the user's response, as well as the name of the constant. Depending on the value obtained, it can be assigned a specific action. The following image shows the values ​​obtained after pressing a button on MsgBox.

Examples of MsgBox

Only the first argument (the message to the user) is required, the rest is editable and optional.

1. Send a simple message to the user 

Private Sub btnbienvenida_Click()
MsgBox "Hello user, welcome to the system"
End Sub

When the button action is performed, to the user a welcome message with the Accept button which is displayed by default.

2. Send a message to the user with the buttons

Private Sub btncontinuar_Click()
MsgBox "Do you want to continue?", VbYesNo + vbExclamation, "Continue system"
End Sub

This code is more specific because the message is displayed to the user, the "Yes" and "No" buttons are assigned, the exclamation mark icon and the title "Continue System".

add a comment of How to use the MsgBox function to personalize messages to the user
Comment sent successfully! We will review it in the next few hours.