Often times, you may need to create a yes/no message box in MS Access. In this post, I’ll show you the steps to create a yes/no message box using a VBA.
To start, here is the general VBA structure that you can use to execute a yes/no message in MS Access:
If MsgBox("Type your message here", vbYesNo + vbQuestion) = vbYes Then VBA Command to be executed if Yes is selected Else VBA Command to be executed if No is selected End If
Example of a Yes/No Message Box
Next, I’m going to use the following example to illustrate how you can create a yes/no message box in MS Access:
- If a user presses a button on an Access Form, a pop-up message will appear with the following statement:
“Are you sure you want to exit the application?”
- The user will then have two options to choose from:
- “Yes” – if the user selects the “yes” option, then the MS Access application will be closed
- “No” – If the users chooses the “No” option, the user will return back to Access Form, and no further action will be executed
Let’s suppose that you already created a button on an Access Form. And so, here is the VBA syntax that you’ll need to add in order to include the yes/no message box:
If MsgBox("Are you sure you want to exit the application?", vbYesNo + vbQuestion) = vbYes Then DoCmd.Quit Else Exit Sub End If
And this is how the full VBA would look like in MS Access:
Let’s now see how this VBA is going to be executed once the user presses the button.
Here is the Access Form that I created. The Form contains a single button – the ‘Exit Application’ button:
Once the user presses that button, the following message box will appear on the screen:
Finally, if the user selects the ‘YES’ option, the MS Access application will be closed. While if the user chooses the ‘NO’ option, the user will return back to the Access Form.