Custom MsgBox 2
By Richard Rost
3 years ago
Custom MsgBox in Access. Part 2: OpenArgs
In this Microsoft Access tutorial series, we focus on creating a custom message box using VBA, enhancing the standard MsgBox function.
In Part 2, we will learn how to pass values to a dialog form by using the OpenArgs parameter.
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
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, OpenArgs, passing parameters to a form, form parameters, form open arguments
Intro
In this video, you will learn how to pass information into a custom message box in Microsoft Access using the OpenArgs property. I will show you how to set properties like the title and prompt text by sending arguments when calling your message box form, and explain why this method is preferred over using global variables or temp vars. You will see practical examples of sending data into a dialog box and using that information within your form. This is part 2.
Transcript
Today is part two of my custom message box series where we're making our own cool little message box to replace the built-in one.
Today we're going to learn how to pass options to this box. We're going to learn about something called open args, which sounds like something an ogre would say if you lock the door. Open args. Anyways, this is part two. So if you haven't, go watch part one. There's the link; scan the QR code. I'll put a link down below. Go watch that, then come on back.
Here we are back in our database. We got this working. We hit OK and it sends a value back. We did all that in part one.
At this point, we need to learn how to send information to this box to set some settings, to set the title, to put a prompt here, or to specify the actual text that goes in the message box.
What buttons do you want, what color do you want, what size do you want, all that stuff. We're going to get to all that eventually. For today, we need to learn how to pass information into this box.
Now, we could use a global variable or we could use temp vars like we did to get the value out of it, but there's a better way. We can actually pass arguments into the box when we call it.
Now let me show you why this is useful.
Let's say I wanted to open up the customer form. From the main menu, let's say I want to open the customer form and I want to set some value in here. Let's say I want to open the customer form and just set the first name equal to Jean-Luc or whatever.
In a button in your code, if I turn the project explorer on, and in yesterday's extended cut... Well, I'm going to leave it open. It's up here. View project explorer. Let's just bounce around in your code modules and in your forms modules without having to close this thing.
Go back here.
So let's say that in my button, I want to do DoCmd.OpenForm "CustomerF". Once the form is open, I want to set the first name. So I'm going to say Forms!CustomerF!FirstName = "Jean-Luc". I'll just put an Exit Sub here because I do not want the rest of the stuff to run.
Save it. Let's come back over here. I'm going to close this and reopen it. I'll click the button. It opened the form. It set the value of the first name to Jean-Luc, and you can see now the record is dirty. It's in the middle of being edited.
I'm going to hit escape a few times and cancel out of that. Let's come back into my message box stuff.
So S = MyMessageBox. Let's go find that. Right-click, Define or Definition.
It's right here. So MyMessageBox. Right here is the command: DoCmd.OpenForm "MyMessageBoxF". After this, let's try setting a property or something on that form.
Let's say Forms!MyMessageBoxF.Caption, the caption property (the title bar across the top), equals "Hi there". We'll continue on. Save that. Let's go back over here, click the button.
The form opened up, but the caption didn't change. Why is that? Let me hit cancel. Oh, what happened? It says, "TechOut Free Template: Can't find the referenced form 'MyMessageBoxF'". Let's hit Debug, and it brings me to this line.
What's wrong with that? That is a valid command; you can do that. Why doesn't it work? Why is it stopping there with an error?
Let me explain why. It's important that you understand this concept before we get into how to get around it.
What happens here with this command, and I mentioned this in part one: this right here, setting the window mode to acDialog. What does that do? It pauses execution of the code right there. No further lines of this function will run until this form closes, and the command to close that is in the buttons, the OK and the Cancel buttons. It sets the temp var on the exit out, closes the form.
So by the time this line then runs after that, the form no longer exists. It's gone. So you can't set its caption. That's the same thing that happens if you try to reference a value on a form that's closed. So we can't do that. Get rid of that line.
But what we can do is we can send arguments. What are arguments? Arguments are basically like parameters. These are arguments. Allow events. Arguments are things you send into a function or a subroutine.
You can also send arguments to a form. You can actually send arguments at the command line in Access itself if you want to run it from a DOS batch file or something.
You can see these. Let me slide this over again. Let me look at the big long list of parameters here. That's still got to move to the left more. There it is. See, there's OpenArgs way on the end. OpenArgs.
Almost never use these, but you can use it to send a value into that form.
So right here I'm going to say OpenArgs:= "Hi there". This is saying open this form in dialog box mode and send to it "Hi there". It's always sending it "Hi there".
Now how do we get that? Well, let's go to the forms module. Here's MyMessageBoxF. Here's the code in there.
Now, when this form was... By the way, this MoveForm thing is something for the members; I covered it in the extended cut. It's in this member module. If you want to see what that does, then you'll have to join. Remember, we had our code before to just move the form down 100 pixels and across 100 pixels, so it's in the center close to the top left of the Access window. Well, this is the code that we did in the extended cut to center it. You do not have to worry about that right now. You can leave your Move code in there that you had before.
But what we're going to do is we're going to come down here and say:
Dim args As String
Now, args is our variable.
args = Me.OpenArgs
There it is. It's a property of the form.
Now we'll say:
Me.Caption = args
I know you could just say Me.Caption = Me.OpenArgs. I get it. But I'm trying to show you here how we can take the OpenArgs and put them in a variable.
Let's see what happens. Save it. Yes. Give it a good Debug-Compile once in a while.
Let's go back over here, close it and open it, and look at that: "Hi there". What happened is we called it from here. We sent it OpenArgs, which is "Hi there". "Hi there" gets read by the form because it's in Me.OpenArgs now. We put that in our own variable called args, and then that gets set to the caption, which is the title across the top of the form.
Voila, that's how it works. It's how you send data into a dialog box. You could set it in temp vars, but then you can't treat it like a function. I want to treat this like a function, and I try to use temp vars sparingly. You don't want to overuse temp vars. You don't want to have 50,000 temp vars flying around your database. Use them when absolutely necessary. But I still do prefer temp vars over global variables for lots of reasons. The number one reason is that while you're developing, if your database throws up a syntax error or something else, all your global variables get reset, whereas temp vars keep the values. You can use temp vars in queries and in all kinds of other stuff.
Now, this is just one value that we sent in. In the next lesson, in part three, I'm going to show you how to send in name-value pairs, also sometimes called key-value pairs, where you can say, for example, Caption=Hi there; Buttons=3, or something like that – a bunch of parameters inside that OpenArgs string. We're going to do that in part three.
Now, while I've got you here, I also wanted to mention that if you want to learn more about this kind of stuff, I've got tons of developer lessons. Obviously, and I forgot to mention this in part one, I create this thing called a universal dialog box in my Access Developer Level 11 class. That's part one; part two is in Level 12. It's something very similar to what we're building in today's class, where there are tons of options in it. If you want to learn this stuff now, without waiting for this entire series to finish, you can check this out now. It's available on my website. I'll put a link down below. Plus, the one in the Developer class goes over tons more options than I'm going to cover in this series, so if you really want to learn about this stuff, check it out.
We go over all this stuff, change it around, some form positioning options, and all kinds of stuff.
That's going to do it for part two. Today is Friday the 24th. I hope you guys all had a nice Thanksgiving yesterday if you're in the States. I'm actually recording this on the 22nd, so I still get to look forward to my Thanksgiving dinner tomorrow.
Tune in on Monday the 27th for part three. We'll see you then. Go check out my TechHelp video for today. I hope you learned something.
Live long and prosper, my friends. I'll see you Monday.
Quiz
Q1. What is the main feature being added to the custom message box in this lesson? A. The ability to pass options or arguments into the box B. The ability to display images in the box C. Creating a password-protected message box D. Adding animations to message box buttons
Q2. Which technique is described as the best way to pass data into an Access form used as a dialog box? A. Using a global variable B. Using temp vars only C. Passing data through the OpenArgs property D. Writing the data to a hidden table
Q3. Why is it not recommended to use global variables in Access forms for passing information? A. They cannot store numbers B. They are reset if the database encounters an error during development C. They make the database run much slower D. They are not supported in Access 2016 and later
Q4. What does setting the WindowMode to acDialog do when opening a form? A. Opens the form as a report B. Hides the main Access window C. Pauses code execution until the form is closed D. Forces the form to open in read-only mode
Q5. What happens if you try to reference or change properties of a modal dialog form after it has been closed? A. The properties are updated, but the changes are lost on reopen B. An error occurs because the form no longer exists C. The changes are saved to the database D. The form automatically reopens
Q6. What is the purpose of the OpenArgs property when opening a form? A. To control the maximum number of open forms B. To specify arguments or data to pass into the form C. To define user permissions D. To set the default font for the form
Q7. How do you retrieve the value sent with OpenArgs inside the form module? A. By calling Application.OpenArgs B. By directly referencing the OpenArgs property of the form (Me.OpenArgs) C. By looking it up in a temp variable D. By using a SQL SELECT statement
Q8. In the example, what property was changed on the message box form using the value from OpenArgs? A. The form's width B. The form's color C. The form's Caption property (title bar text) D. The text of the OK button
Q9. According to the lesson, what is a key advantage of temp vars over global variables? A. Temp vars allow larger amounts of data B. Temp vars persist their values even after certain errors during development C. Temp vars cannot be accidentally deleted D. Temp vars automatically sync across networked databases
Q10. In this lesson, how many values are passed into the message box using OpenArgs? A. None B. One C. Two D. Multiple using name-value pairs
Q11. What future improvement does the instructor mention for passing multiple options to the form? A. Using global variables again B. Using a hidden form for parameters C. Packing multiple name-value pairs into the OpenArgs string D. Saving settings in a configuration file
Q12. Why should temp vars be used sparingly according to the instructor? A. They require administrator permissions B. Too many temp vars can clutter the database and create confusion C. They are slower than global variables D. They only work in query contexts
Answers: 1-A; 2-C; 3-B; 4-C; 5-B; 6-B; 7-B; 8-C; 9-B; 10-B; 11-C; 12-B
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 my custom message box series. In this second part, I will show you how to pass options to our custom message box, making it more versatile than the built-in version. Specifically, we will work with the OpenArgs property, which allows us to send data into a form when it opens. If you have not already viewed part one, I recommend checking that out first before continuing with this lesson.
Previously, we set up the custom message box so it could return a value when the user clicks OK. Now, it is time to learn how to send information to the message box itself. This will let us set properties such as the window title, the message prompt, button choices, colors, size, and more. Although we will eventually get to all of those settings, today we will focus on the basics of passing data into the form.
There are several ways you might consider for this purpose, such as using global variables or TempVars. TempVars are helpful for receiving data back from the form, but they are not the ideal way to send information into a form. Instead, you can use arguments, just like you pass parameters into functions or subroutines. Access provides this ability through the OpenArgs property when opening forms.
Let me explain why using OpenArgs is useful. Imagine you want to open a customer form from your main menu and automatically set a field value, such as the first name. Typically, you might open the form in your code and set a field by referencing it directly. However, if you open the form using the acDialog window mode, the execution of your code pauses until the form closes. This means any lines of code that try to change properties or fields on the form after it opens will fail, since the form will have already closed by that point. This is why trying to set the caption or field value after opening the form as a dialog results in an error—the object no longer exists.
To solve this, OpenArgs is the solution. When you open a form, you can specify a value to be passed into that form using the OpenArgs parameter. Once the form loads, the value is available as the OpenArgs property of that form, and you can use it however you need.
For example, you might write your code to open the custom message box form with OpenArgs set to "Hi there." Then, in the form's module, you would refer to Me.OpenArgs in your code. This allows you to dynamically set properties based on the argument passed in. For instance, you can assign Me.OpenArgs to Me.Caption so the form's title bar displays "Hi there" when it opens. This demonstrates how you can send information into the dialog box, improving its reusability and flexibility.
While you could use TempVars or global variables for this purpose, I prefer treating forms like functions and avoid relying on TempVars whenever possible. Overusing TempVars can clutter your database. I still prefer TempVars over global variables because TempVars preserve their values if your database crashes or encounters a syntax error during development, whereas global variables get reset. TempVars are also available in queries and other places throughout Access.
In this lesson, we covered sending a single value into the message box using OpenArgs. In part three, I will show you how to send in multiple parameters at once, structured as name-value pairs, like Caption=Hi there;Buttons=3, so you can configure many different options in one go.
If you are interested in learning more about these kinds of techniques, I offer a comprehensive Access Developer class on my website that covers building a universal dialog box with far more features and configuration options than we will discuss in this series. This is covered in Developer Level 11 and 12. If you want to get ahead or explore advanced dialog box techniques, I encourage you to check out these courses.
That concludes part two of the series. Thank you for following along. You can find a complete video tutorial with step-by-step instructions on everything discussed here on my website at the link below.
Live long and prosper, my friends.
Topic List
Passing arguments to a custom message box form
Understanding the OpenArgs parameter in DoCmd.OpenForm
Using OpenArgs to send data into an Access form
Setting form properties using data from OpenArgs
Extracting OpenArgs inside the form module
Setting the form caption based on OpenArgs
Differences between global variables temp vars and OpenArgs
Why acDialog mode pauses VBA code execution
Limitations of setting form values after dialog forms close
Choosing between temp vars and global variables for inter-form data passing
|