Custom MsgBox 10
By Richard Rost
3 years ago
Custom MsgBox in Access. Part 10: Play Sounds In this Microsoft Access tutorial series, we focus on creating a custom message box using VBA, enhancing the standard MsgBox function.
In Part 10, we will learn how to play sounds when our MsgBox opens. You can have it play any number of beeps. We'll learn a trick using the form's Timer event to play them properly. MembersThere 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!
PrerequisitesLinksRecommended CoursesComing 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, Beep, play sound, make noise, tempvars, form timer event, me.timerinterval, sleep
Intro In this video, I will show you how to add multiple beeps to your custom message box in Microsoft Access, giving you the option to control the number of beeps as your form opens. We'll cover how to use the Sleep function to space out the beeps, explain why beeps might overlap, and demonstrate how to trigger the sound using a form's Timer event. You'll also learn how to pass options like the beep count to your form using TempVars. This is part 10.Transcript Welcome to another TechHelp video brought to you by AccessLearningZone.com. I'm your instructor, Richard Rost.
This is part 10 of my message box series. Today we're going to make your message box go beep. If you don't want just one beep, we're going to make it go beep beep beep - as many beeps as you want! I know it sounds silly because you could just put a beep command in there, but if you want multiple beeps, there's a trick I'm going to show you, so stay tuned. Here we go.
This is part 10. If you haven't watched parts one through nine, go watch parts one through nine first, then come on back.
All right, our message box is getting pretty good. We can move it around, we can change colors, we can change the file, and we can put these pretty little boxes on the icons, whatever we did in the last part.
What if you want this thing to beep when it opens up and you want that to be an option, not just always beeping? Well, it should be second nature now to add options to our boxes, so let's go to the global module. Let's find the call to the function right here.
We're going to add another option in here: Optional. Let's call it beeps as Long, and the default will be zero - no beep. Add it down here at the bottom, got to send it to the arguments, right? Copy, paste, and then we'll grab beeps and put beeps down here and here. All right.
We've done this, what, a dozen times so far? Well, not a dozen, we've done this ten times overall. Save it.
Let's go to our function call, so it's going to be in here where it comes in. We're going to say we'll do it after the icon here. We'll say: case beeps. Now, if we send it one beep, no problem. If we send it three beeps, we need a loop counter, right? We need to loop.
So let's come up top here and add another variable. We get x as Long, and then we're going to put in here y as Long as well. x, y, z, they're your typical loop counter variables. That goes way back to my TRS-80 Coco BASIC programming days.
So, for y equals one to name value one, and then in here we're going to beep, and then we're going to say next.
All right, let's save it. Then debug, compile, come back out here, and let's update our main menu call - main menu F - and come down here and right after this we'll put one for our beeps. Save it and click the button. There's our beep.
Okay, pretty cool. Let's do three beeps. Save it, hit the button.
Still, I only heard one beep. What's going on here? Let's try ten beeps and see what happens.
See, the problem with beep is they all get sent almost immediately to the "beep machine" - whatever in the computer makes the beep - and without any spacing between them, it all sounds like one beep. Sometimes you might hear like five of them real fast.
So we need to put a pause in our code.
So back to here. Right in here, we've got to sleep a little bit. There is a function we can use called Sleep. I have a video that explains exactly how to use this and it is on this page. This is one of those items in my Code Vault that is usually reserved for the members.
But this one is free for everyone so you can actually go in the Code Vault and get this one. Go to this page down here, scan the QR code, or just type in "sleep" after my domain. That should take you to my sleep page. Come right down here, free source code.
Yeah, it's free source code. It's free source code day. Everyone gets some free source code, and you get some free source code. Just click on that.
Now, this is the whole thing. All you really need is this one line right here. To use the Sleep function, I wrote a little helper function that goes with it called SleepSeconds, where you can specify a number of seconds.
If you say, like, 10 seconds, it'll sleep and it does it in one second intervals because these are milliseconds. So this just pauses for 10 seconds, but it puts breaks in the middle in case the user hits cancel or something, and it allows other code to run. Watch the other video, it'll explain how this works.
But all we really need is this right here, just this one line of code. Just copy that. Copy back over here to your database, you're going to go in your global module - and here, it's already in here.
If you are using my TechHelp free template from my website and you've downloaded it recently, the Sleep function will already be in here because I use it somewhere else. But if not, just paste it right in here at the top of your global module or any global module. In fact, my SleepSeconds is in here as well. We're just going to use Sleep.
Now, come back down here to your function. Not this thing - we need to go to the message box, right down here where we're looping. We're going to beep, and then we're going to sleep.
All right, beeping and sleep. However long you sleep is up to you. I'm going to say probably at least half a second. That's 500 milliseconds. Remember, a thousand milliseconds is a second. Save that. Let's see how that sounds. I don't want 10 though. Let's go back to three.
All right, save it. Let's see how half a second does.
That's better, right? You could do full seconds if you want to. I like the half seconds.
Now, we're working, it looks good, everything's great, but here's the problem: you click. You're hearing beeps, but you're not seeing the message box. The message box isn't displaying because the code that opens it up, right, if you go into your MyMessageBox, this code here that's actually doing this loop, this loop, is in the form load event.
Because it's in the form load event, anything in this basically stops; the form will not display on the screen until this event is done running.
So we're not going to put this here. We're going to put it somewhere else. I'll show you where in just a second. For now, cut this out. Put it in your clipboard so you don't lose it. Drop it in your Notepad. There's Notepad. Where are you? Come here. Boop, just stick it in Notepad.
Yeah, I know the tabs are off. Don't worry about that.
Come back up here now. We don't need this y in here anymore. You can get rid of the y. Why? Because I don't need it.
We're going to run this code somewhere else, but we have to tell it somehow that you need to do some beeps. So we're going to say in here tempVars!doBeeps equals how many beeps, name value one.
Now, who's going to run that beep situation? Well, it's got to be an event that runs after the form is finished loading.
I like to use the timer event, and there's a trick you can do. You set it so that the timer interval is zero so it doesn't run after this goes off. So it'll basically run it - you set the timer interval to something small, like again, half a second. So, half a second after the form loads, it runs the timer event. The timer event will do the three beeps and then shut the timer off.
I've covered this in a couple of other videos, including the one where I show you how to do flashing text, flashing backgrounds, that kind of stuff. It's the same kind of technique.
So, after this guy goes, we're going to now say Me.TimerInterval = 500. That's going to turn on the timer event.
When the form loads, it doesn't matter what you put in design time because it's going to get set to 500 right here.
Now, we go over to the form timer event. You can either go back here and go to the properties and find On Timer over here, or you can go in here. We're already in the form proper, in the form load. Drop this down and find Form_Timer. Now we're in the form's Timer event.
Now, this is what's going to happen after the form loads, half a second later, which for a computer is an eternity, right?
So, we're going to say we need that y back, right? Dim y As Long, we got rid of it before.
We're going to take that code that we copied to our clipboard, we're going to put it back in here. Boop, right there. There you go. Now, watch this. I'm just going to select this stuff and go Shift+Tab, Shift+Tab, Shift+Tab, Shift+Tab, Shift+Tab - keep it all nice and lined up like that.
But before this runs, we don't want it running a second time, all right? We're going to say Me.TimerInterval = 0 to turn off the timer.
Right, so: form loads, we need some beeps. Set it to 500, so that way half a second later the form timer event is going to run. It's going to immediately turn the timer off so it doesn't run a second time, and then we're going to get our loop with our beeps. The problem is, this guy doesn't know what name value one is.
So we're going to grab the tempVar that we had up here and put that there. That's a quick, easy way to pass values around. You could use a global variable, you could use a hidden form field, you could use a public variable in this form, whatever. TempVars is great. I love tempVars.
Pretty sure I mentioned this because we used it before in one of the previous videos, so I'm sure you've probably watched this one by now, but it's tempVars, so it gets a second mention. I know I've covered the form timer event in a couple of different places. Here's Timer Event - this one is to pop up a notice. It just runs in the background on one of your forms, and it looks for an event.
This is the one I mentioned earlier with the flashing text - this guy will just flash. Anyway, let's see if it works. Ready? Here we go.
Debug, compile, everything looks good, come back out here, save it, close it, close it, save it, close it, open it.
There you go! See, you saw the form first, then you heard your beeps.
Let's slow those beeps down a little bit. Let's just slow them down. Let's make them a full second pause, and ready, go.
There you go. That's quite obvious now - the form appears first, then you hear the beeps.
Now, of course, at this point, I know a lot of you are going to ask me, "Can we play a sound in there?" Yeah, you could play a wave file. You could play any kind of sound - you could play a Star Trek klaxon, red alert, whatever you want. I know the default message box has different sounds it plays, so yeah, you can just play whatever. You know, "I didn't say the magic word." You can do whatever you want.
In the extended cut for this video, I show you how to play wave files in VBA. But that's going to do it for part 10 of your TechHelp video for today. As of right now, I have one more part coming up, but of course, this is, let's see, today is actually November 30th. You're not going to be watching this until like December 8th, so next week. So if I get some cool feedback in the next couple of days, I might do part 12.
One more thing coming up: we're going to do a configuration form where you can actually have a form where you put in the options and then you hit the button and it generates the VB code for you to copy and paste into your VB code. It's going to be cool. You're going to like it.
But that concludes part 10. That's your TechHelp video for today. I hope you learned something. Live long and prosper. I'll see you next time.Quiz Q1. What is the main purpose of the technique demonstrated in this video? A. To show how to play custom wave files when a message box opens B. To create a message box that can beep multiple times with spacing between beeps C. To add customized icons to message boxes D. To display multiple message boxes at once
Q2. Why do multiple beep commands in a row typically sound like a single beep in VBA? A. The computer ignores repeated beep commands B. The beep command is disabled after the first beep C. All beep commands are executed almost simultaneously without delays between them D. The sound card can only process one beep at a time
Q3. Which function is introduced in this video to create a pause between beeps? A. Wait B. Pause C. Stop D. Sleep
Q4. Why is it necessary to use a timer event instead of the form load event for running the beep sequence? A. The timer event allows for more precise control of the beep volume B. The timer event helps the beeps to play before the message box displays C. The form will not display on the screen until the load event is finished, so the beeps would play before the form appears D. The timer event is easier to debug than the load event
Q5. If you want your beeps to occur half a second apart, what value should you use for the Sleep function? A. 50 milliseconds B. 100 milliseconds C. 500 milliseconds D. 1000 milliseconds
Q6. What variable or mechanism does Richard recommend for passing the desired number of beeps to the timer event? A. A hidden text box on the form B. A global variable in the module C. TempVars D. A public constant
Q7. What does the statement Me.TimerInterval = 0 accomplish in the timer event? A. It disables further timer events after the beeps are done B. It causes the timer to repeat with no delay C. It resets the form to its initial state D. It pauses the code for zero seconds
Q8. In the context of the video, what is an advantage of using TempVars for passing information? A. TempVars permanently store data between sessions B. TempVars can be used to pass data between events and forms without relying on global variables C. TempVars are visible to users on the form D. TempVars require complicated setup and initialization
Q9. According to the video, why might the code in the form load event prevent the form from being seen before the beeps? A. The form's caption property needs to be set first B. The load event code blocks the form from rendering until it finishes executing C. The beeps pause the system, freezing the application D. The beeps modify the form's visibility property
Q10. How can you adjust the spacing between beeps in the final message box implementation? A. By sending a different icon type to the message box B. By changing the Sleep function's parameter value C. By editing the color settings of the form D. By resizing the message box window
Q11. According to the video, what is one limitation of the message box beep approach compared to playing custom sounds? A. Beeps cannot be looped B. Only one beep can play at a time C. Beeps are limited to the default system sound and cannot play .wav files D. Beeps require additional installation of external libraries
Q12. What is a potential next step suggested at the end of the video series for further customizing the message box experience? A. Allow messages to be displayed in multiple languages B. Add animation to the message box C. Create a configuration form to generate VBA code for message box options D. Add support for sending email notifications
Answers: 1-B; 2-C; 3-D; 4-C; 5-C; 6-C; 7-A; 8-B; 9-B; 10-B; 11-C; 12-C
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 is part 10 in my ongoing message box series for Microsoft Access. Today, I am going to show you how to make your message box produce a beep sound when it opens, and, if you'd like, make it beep multiple times. This might seem like a small detail, but adding customizable beeps to your message boxes can be a useful feature. There are, however, some obstacles to overcome if you want more than just a single beep, and I am going to show you the best method for making this happen.
If you have not watched parts one through nine in this series yet, I recommend going back and watching those first, since we build on lessons from previous videos.
Up to this point, our custom message box can already be moved, the colors are configurable, you can set different file values, and we've handled icons and other customizations. The next enhancement is controlling whether or not the message box will beep when it opens, and allowing the user to pick the number of beeps.
To add this feature, we'll start by introducing a new optional parameter in the global function that handles our custom message boxes. The parameter will be called "beeps," it will be a Long type, and I'll set the default to zero so the message box does not beep unless we ask it to. Just as with the other parameters we have added, you'll want to update all references to this function to include the new argument throughout your code.
The logic for making the beeps themselves is pretty straightforward. If you want one beep, a simple command suffices. If you want multiple beeps, you need to use a loop, so you'll want a counter variable to run the beep command as many times as needed.
At this point, you might expect that simply looping through multiple beep commands should give you a series of beeps. The issue is that without a pause between each beep, they get sent to Windows so quickly that they merge into a single drawn-out beep rather than the distinct, individual beeps you are likely trying to achieve. If you try ten rapid beeps in a row, you might only hear a handful as Windows tries to process them all at once.
To solve this, you need to insert a brief pause between each beep. The Windows API provides a Sleep function that pauses code execution for a specified number of milliseconds. I have another video on my site, and a free code example in my Code Vault, that demonstrates how to use Sleep and a little helper function I wrote called SleepSeconds. You are welcome to visit my site and download the free example.
To make your message box beep the way you want, add a call to Sleep (for example, around half a second, or 500 milliseconds) after each beep within your loop. This will separate each beep clearly. Try different values to get the spacing that sounds best for your needs.
However, there is a further complication. When you place the beep loop in the form's load event, the form does not actually appear on screen until the event completes, so your users will hear the beeps before seeing the message box. This is a quirk in Access: code in the form's load event will execute before the form is displayed. This is not what you want if you want the visual and audio cues to match up.
To address this, you need a way to trigger the beeps only after the form has loaded and is visible. The best way is to use the form's Timer event. First, store the number of beeps in a TempVar. Then, right at the end of the form's load event, set the TimerInterval property to the desired delay (for example, 500 milliseconds). This will cause the form's Timer event to fire once after the interval. In the Timer event, place your beep loop code, retrieve the number of beeps from TempVars, and then immediately set TimerInterval back to zero so it does not fire again.
I have covered using the Timer event in depth in previous tutorials, such as those showing how to flash text or backgrounds, so if you want to learn more about this technique, check those out on my website.
Once this is implemented, your message box will appear first, and then you will hear the series of beeps, separated by delays as you specify. You can experiment with different delay values to get the effect you want.
A question I get frequently is whether you can replace the beep with a different sound. Yes, it is possible to play wave files in VBA, including custom audio clips like sound effects. In the Extended Cut for this video, I show how to play wave files, so if that is something you are interested in, be sure to check that out.
Looking ahead, I am planning one more video in this series, where we will build a configuration form. This form will let you choose the message box options you want, and then generate the VBA code for you, which you can then copy and paste into your project. It is a handy tool for anyone building custom message boxes repeatedly.
That wraps up part 10 in this message box TechHelp series. You can find a complete video tutorial with step-by-step instructions for everything discussed here on my website at the link below.
Live long and prosper, my friends.Topic List Adding an optional beeps parameter to a custom message box function Passing arguments through to subroutines in VBA Implementing a loop to allow multiple beeps Using the Beep command in VBA Inserting pauses between beeps using the Sleep function Integrating the Sleep API function into your Access project Setting timer intervals on Access forms Using the form Timer event to trigger code after form load Passing values between events using TempVars Ensuring the form is visible before executing beeps Turning off the Timer event after it fires Debugging issues with code execution timing in form events Adjusting sleep duration for different beep speeds
|