Custom MsgBox 5
By Richard Rost
3 years ago
Custom MsgBox in Access. Part 5: Third Button
In this Microsoft Access tutorial series, we focus on creating a custom message box using VBA, enhancing the standard MsgBox function.
In Part 5, we will learn how to add a third button to the form, and hide buttons 2 and 3 if they're not needed. For example, an "OK" only form would not need buttons 2 and 3. "OK/Cancel" wouldn't need button 3. So we'll hide them.
Members
There is no extended cut, but here is the database download:
Silver Members and up get access to view Extended Cut videos, when available. Gold Members can download the files from class plus get access to the Code Vault. If you're not a member, Join Today!
Prerequisites
Links
Recommended Courses
Coming Up
Keywords
TechHelp Access 2016, Access 2019, Access 2021, Access 365, Microsoft Access, MS Access, MS Access Tutorial, #msaccess, #microsoftaccess, #help, #howto, #tutorial, #learn, #lesson, #training, #database, Custom Dynamic MsgBox, Custom MsgBox in Access, Adding a 3rd Button, Hiding Buttons you don't need
Intro In this video, we continue building a custom message box in Microsoft Access by adding a third button, allowing for Yes, No, and Cancel options. I will show you how to modify your form and VBA code to support up to three customizable buttons, handle button captions and visibility, use the IIf function for cleaner code, and ensure sensible defaults in your message box routine. We also discuss different ways to call your custom message box, whether you need one, two, or three buttons for your prompts. This is part 5.Transcript Welcome to another TechHelp video brought to you by accesslearningzone.com. I'm your instructor Richard Rost.
In today's video, we are continuing with our custom message box. This is part five, and today we're going to give you that third button. So far, our box only had yes and no. Well, now we're going to add cancel so we can have one, two, or three buttons possible.
Of course, this is part five. So what does that mean? You guessed it. Go watch parts one through four if you haven't watched those yet. Go watch them, then come on back. You'll find links down below.
All right, you ready to add button three? We have two buttons, yes and no. My favorite message box combination is yes, no, cancel. As I mentioned before, sometimes people get a prompt and don't know what to do, so they cancel just to get out of dodge.
Let's go into design view and make button three. I'm just going to slide you over here and copy/paste, and this guy will slide over here now. This will be button three. What are the other ones? Button one, button two.
Let's make sure the space is about the same. There we go. Name it button three, and I'm going to change the captions too. We're just going to put B3 on there, put B2 on this one, because you're going to send the captions as properties. If you don't send them, the defaults kick in.
Time to put the code behind it. Right-click, build event, same as we got up here. Copy that, paste it down here. Button is three.
I've gotten some emails from a few of you asking if we can set up our own codes like VBS VB now. Sure, you can. You can set up your own constants if you want. I prefer using one, two, and three to represent the buttons. That's my preference. You do whatever you want there. It's your Legos. You put them together however you want them to be.
Let's go over to the message box function, which is in our global module. Let's add button three in here: optional button three as a string, and this one can be - let's leave the default for that one as the empty string. You don't have to put anything after the declaration there. Most of the time, I think most people are going to use two buttons. That third button is an option.
Also, while I'm thinking about it, let's make it so that button one has to be something. We'll say if button one equals blank, then button one equals OK. In case they accidentally send in an empty string - because you could force an empty string in as that parameter - if they do that, they're getting an OK button. You can give them an error message if you want, but I think this is better.
If that's the case, the args down here always has to have button one. So we can get rid of this and say right here: args equals args and button one, because button one can't be an empty string. Now we're going to add three to the end of this. Three goes there, three goes there, three goes there, and now those get sent into the form.
Let's save this and go over to the form. The form code is right here: myMessageBoxF.
While I'm thinking about it, I'm going to rem out the move form code because you can actually use auto center with pop-up and dialog forms now. They did not used to work well. I think I talked about this briefly before. I stopped using auto center with pop-up forms and dialog forms because they were buggy, and I explained why in this video. I always relied on positioning the forms myself because on a multi-monitor setup, pop-up forms would not pop up where you would expect them to. Go watch this video for all the details.
We're going to rem this out for now and come over here. We're going to take this guy and make auto center set to yes. That should do what we want it to do there.
Back in the code, we have to add button three right here. Button three, save it.
Now, in case they don't send in captions for buttons two and three, we need a way to hide them. If it's just an OK box, hide buttons two and three. Here we're going to write this a little bit differently. We're not going to do it with just a standard If...Then statement. We're going to use the IIf function.
We're going to be a little cool with this. Button two.Visible = IIf(button two.Caption = "", False, True).
Take a look at that. Pause the video if you have to and read in your brain what that actually says.
It basically says it's the same as writing: if button two.Caption equals an empty string then button two.Visible equals False else button two.Visible equals True. That's what the IIf function does.
If you're not familiar with the IIf function, go watch this. I'm always trying to teach you something new even if you haven't seen that before.
Let's copy and paste that for button three. There you go, get in there. It's just a little more compact. I know I'm usually about readability over making things compact, but it's nice to do something new once in a while too.
If you're going to do something like this down there, it's also nice up here to initialize them to make sure. I'm going to put button one.Caption equals OK. These are the defaults: button two.Caption equals blank, button three.Caption equals blank. If no arguments are sent in, we're making sure that we force these arguments there.
Let's save this, go back to our main menu now, and redo our prompt.
This time, since we're going to have three possible entries, we're going to put the response in a variable. So: Dim L As Long. Let's call it that. L is going to be: MyMessageBox("Is Picard the greatest captain", "Yes", "No", "Cancel").
Now we're displaying that, and we're getting the value that's returned and putting it into L. Let's just see what that looks like here. I'm just going to put: If L = 1, let's see what this gives us. It should give us Yes, No, Cancel, but only work with one button. Let's just give it a quick test.
All right, there's our Yes, No, and Cancel.
Let's get rid of cancel real quick just to make sure that hiding stuff is working. Let me delete that, save it, and click. All right, yes and no, notice that it's centered. We will deal with that later.
Okay, let's put cancel back in here.
What happened? I never closed that, that's why.
All right, cancel.
Now we're just going to change this to: If L = 1 Then Welcome Aboard, Else If L = 2 Then Show This Man to the Airlock, Else Come Back Later When You Decide. You had Cancel, so you know.
Click Yes. Welcome aboard. Show this man to the airlock. Three, come back later when you decide.
If you want just an OK button, let me do something up here. You don't have to use MyMessageBox as a function. You can call it like you normally would message box something, as a subroutine. You could just say: MyMessageBox("Picard rules"), like that and that's it.
What do you get? Picard rules, OK.
If you want it just to be an OK button, then you can change these defaults in here so instead of the defaults being OK, Cancel, leave this one blank. So now the only defaulted value is OK.
Now, if I come back here and hit the button, that's just like a standard, typical OK message box.
That's all you have to do to pop it up.
Now, we are going to talk a little bit more about positioning these things later on. For now, let's say you just want Yes and No, and you don't want that big space to the right there. You'll have this big empty space here. Well, you can just use buttons one and three if you want to. This is a simple, cheesy thing you could do. You could leave this guy blank and make this guy No, and then just check for buttons one and three, like that.
Now you get Yes and No, and this is also good if you've got one of the situations where you want to make sure people don't accidentally click a button because they're spaced far apart.
Or you could make OK. I guess we could make it so that button one doesn't necessarily have to be valid; you can make it button two, and then just do a check to make sure that any one of these buttons has something. There's got to be at least one button to click to close the box.
That's up to you - your Legos.
So there you go. There's your third button.
That's going to do it for today, folks. That's your TechHelp video for today. Hope you learned something. Live long and prosper, my friends. I'll see you next time. Lots more to come, so don't go anywhere.Quiz Q1. What was the main addition to the custom message box discussed in this video? A. Adding an icon to the box B. Adding a third button (Cancel) C. Adding a sound alert D. Adding a help link
Q2. Why did the instructor recommend using numbers (1, 2, 3) for button return values? A. They are easier to remember than text labels B. They make the code more difficult to read C. They correspond to error codes in VB D. They force users to remember button order
Q3. What is the purpose of making Button One's caption required in the function? A. To ensure there is always at least one visible button B. To block the use of the function unless all buttons are labeled C. To allow any caption to be used D. To prevent syntax errors
Q4. What does the IIf function accomplish in the code for the message box buttons? A. Determines which button was clicked B. Hides a button if its caption is blank C. Moves the message box to the center D. Applies default captions to all buttons
Q5. What is the default caption for Button One if an empty string is passed? A. Cancel B. Yes C. OK D. Close
Q6. How does the function handle the scenario where no captions are set for Button Two and Button Three? A. All buttons will be invisible B. Only Button One will be shown C. Buttons Two and Three will display as blank D. The message box will not appear
Q7. What programming practice was highlighted when using the IIf function to set button visibility? A. Making code longer for clarity B. Using compact code for readability C. Adding unnecessary complexity D. Ignoring the importance of readability
Q8. What does auto center do for pop-up and dialog forms? A. Makes them always stay on top B. Changes the button labels automatically C. Centers the form on the screen D. Maximizes the form window
Q9. How can you display only Yes and No buttons with more space between them according to the video? A. Use all three buttons but label two of them as Yes and No and hide the third B. Use only button one and button three with button two left blank C. Always use all three buttons D. Use only button one by default
Q10. What is the return value L used for after displaying the message box ("Is Picard the greatest captain", "Yes", "No", "Cancel")? A. To determine which caption was used for Button One B. To identify which button the user clicked C. To hold the error message string D. To change the form title
Q11. What was the purpose of demonstrating message box use as both a function and subroutine? A. To show how to return a value from a subroutine B. To illustrate flexibility in how the custom message box can be called C. To force the user to pick a button D. To prevent errors with default arguments
Q12. If you want an OK-only message box, what should you do based on the video? A. Set captions for all three buttons B. Omit the captions for buttons two and three C. Set a default caption of Cancel D. Use only buttons two and three
Q13. According to the instructor, what is essential for the custom message box to function properly? A. At least one button must have a caption B. All three buttons must always be displayed C. Button visibility must be set to false by default D. All captions must be blank
Answers: 1-B; 2-A; 3-A; 4-B; 5-C; 6-B; 7-B; 8-C; 9-B; 10-B; 11-B; 12-B; 13-A
DISCLAIMER: Quiz questions are AI generated. If you find any that are wrong, don't make sense, or aren't related to the video topic at hand, then please post a comment and let me know. Thanks.Summary Today's TechHelp tutorial from Access Learning Zone continues our project of building a custom message box in Microsoft Access. In this fifth installment, we are adding a third button to the message box, providing the flexibility to display up to three buttons: Yes, No, and Cancel.
If you have not yet followed along with the previous parts of this series, I recommend watching parts one through four first. There are links on my website where you can find those lessons to catch up.
Now, our message box so far only featured Yes and No buttons. My personal favorite setup for message boxes is to offer Yes, No, and Cancel. This is particularly helpful because sometimes users simply want an option to back out rather than commit to a choice they are unsure about. That is where the Cancel button comes in handy.
To add the third button, I work in Design View, duplicating the previous button controls so that we now have Button1, Button2, and Button3. I arrange them so the spacing is even, then rename the new one to Button3 for clarity. While the actual button captions will be provided as properties when the form is called, I temporarily use B1, B2, and B3 to keep things organized while editing.
Next, I add the necessary code for Button3, mirroring the event procedures that were already in place for the first two buttons. The logic is straightforward: pressing any button returns a corresponding value (1, 2, or 3). As some of you have asked, it is possible to assign custom constants here if you prefer, but I find sticking with 1, 2, and 3 keeps things simple. As always, you can adjust this to fit your specific needs.
The next step is to modify our global module where the MyMessageBox function is coded. I add an optional argument for Button3, defaulting to an empty string. Since most message boxes only use two buttons, Button3 remains optional unless specifically included. I also make sure Button1 always has a caption. If the user passes in an empty string, Button1 will default back to "OK" so we never have an unusable button.
The code then assembles the parameters, ensuring they are passed correctly to the form. Button captions are sent through as arguments. The goal is to allow total flexibility depending on how many buttons you wish to show.
Switching over to the form's code, I make a small improvement by discontinuing the manual form positioning that was previously necessary with popup and dialog forms. Thanks to recent improvements in Access, using the Auto Center property now works more reliably on these forms, so I re-enable that feature.
Back to the code, I make sure that the visibility of Button2 and Button3 on the form reflects whether they have captions. Using the IIf function, each button is only shown if its caption is not blank, so you do not end up with empty buttons cluttering the interface. This helps keep the form clean and professional.
I also set default captions for all three buttons at the form level. Button1 defaults to OK, while Button2 and Button3 start as blank. This ensures that if no arguments are given, the user will at least have an OK button to close the box.
Now, to put our message box to the test, I modify the main menu's code to capture the user's selection in a variable. When prompting the user with the question "Is Picard the greatest captain?", for example, I specify Yes, No, and Cancel as the three possible button captions. Depending on the returned value, I handle each case—welcoming the user aboard if they choose Yes, showing them to the airlock if they choose No, and inviting them to come back later if they cancel.
The system is flexible. If you want just a simple OK message box, you can call MyMessageBox with only a single caption or no caption arguments at all. The defaults take over and display the classic OK button box.
I also point out an approach for controlling how your buttons appear and are spaced. If you want Yes and No stretched further apart for clarity, you can assign those captions to Button1 and Button3, leaving Button2 blank. This can help prevent accidental button clicks by making the choices more distinct.
Ultimately, the way you use and customize the buttons is entirely up to you. This project is much like playing with Legos; you get to assemble things however you see fit for your particular application.
With these updates, you now have a fully functional custom message box that can display one, two, or three buttons in Access, with the flexibility to label and control them as needed.
If you want to see every step I described here in detail, complete with hands-on demonstrations, you can find the full video tutorial on my website at the link below.
Live long and prosper, my friends.Topic List Adding a third button to a custom message box Configuring button captions dynamically Setting default button captions Adding an optional third button parameter Hiding buttons based on empty captions Using the IIf function to set button visibility Initializing button captions with default values Handling multiple button responses in code Storing the message box result in a variable Returning custom values for different buttons Creating OK, Yes No, and Yes No Cancel boxes Calling the custom message box as a subroutine Adjusting button spacing and visibility Ensuring at least one button is present
|