How to use the MsgBox function to customize user messages

Who I am
Philippe Gloaguen

Author and references

One of the most used functions within VBA is the message window or better known as MsgBox. These messages are used for send information to the user final, just to indicate something or to invite him to take an action. Use the MsjBox feature to personalize all messages you send to end users of Office applications.

Generally the MsgBox is used when a simple answer by the user as Accept or Cancel. They are easy to make and totally editable, make a different one for each action.

How to use the MsgBox function to customize user messages

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 fairly straightforward and no additional programming knowledge is required.

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 helpful to provide the user with a reference indicating the relationship of the message.
  • Buttons: it is also optional and if it is 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 path to 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.
Read Galaxy Buds + vs Airpods comparison Which are the best?

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?"
Button types 'message to user style = vbYesNo + vbCritical + vbDefaultButton2'

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 Answer = vbYes Then 'If the user selects Yes
lbltexto.Caption = "Excellent" 'The action being performed
text message "If user does not select lbltexto.Caption =" Nothing happens "" which is displayed if the user says no
End If

End Sub

The previous code assigns the MsgBox function to a button on the form . When the user clicks the button, a message will appear asking if he wants 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 displayed message is "Nothing is happening".

Another way to show the topic of a MsgBox is place the value of each constant or add the value of the arguments. The previous way is simpler because it allows you to interpret the code with the naked eye.

Returns values ​​from MsgBox

The important thing is to determine what type of value is associated with the user's response, in addition to the constant name. 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.

Read How to Easily Customize Instagram Featured Stories Covers

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 subtitles 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 displayed by default.

2. Send a message to the user with the buttons

Private subtitles 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, "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 customize user messages
Comment sent successfully! We will review it in the next few hours.