Introduction
Those of a certain age will remember a cartoon character called the Road Runner. This cartoon tracked the adventures of a really fast mischievous bird that would run rings around his nemesis, Wile E. Coyote. Every time he passed Wile E., he would give his signature sound: “Beep Beep!”
As you know, your computer, and Microsoft Access, also has the ability to beep. You probably use this beep when you want to alert the user that something is going on; and it is a good way to do this. However, over time, you might find that your users get so used to the beep sound that they start to filter it out.
In this month’s
Wizard, I will show you an alternative to the beep that will allow you to customize it; this way when you want to your user’s attention, you will be able to grab it.
Customizing the Beep Sound
The standardized beep sound in Access allows no customization. You can add some variety by putting pause statements into your code and setting up a loop so that it sounds more than once. Even with this type of change, after a while, your user might no longer tune into it. You can customize both the frequency and how long the beep sounds so the user hears something brand-new and, hopefully, pay attention to whatever it is you want him alert her to.
To do this, put the following line of code into a module or a code behind form in which you want to have a customized beep:
Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long
This particular instruction makes a call to code library in Window. It is a call to a function that takes two inputs, frequency (in hertz) and duration (in milliseconds). Once this is in place, you can write code snippets like the following:
Beep 1000, 500
When your code encounters this snippet, the computer will beep at the frequency of 1000 Hertz for a half second. The duration is expressed in milliseconds. So if you use 1000, the beep will last for one second. Both of these inputs can be manipulated programmatically, which leads to some interesting ways to use this library.
Examples
Let’s start with just a simple beep. You can put a loop in place that varies the frequency.
For instance, if you try the following code, you will hear something very strange.
For i = 100 To 1000 Step 10
Beep i, 20
Next i
This next routine will play quick four tones in a descending frequency.
Sub BeepMe()
Dim i As Integer
Dim intFrq As Integer
For i = 0 To 3
intFrq = 900 - (i * 100)
Beep intFrq, 100
Next i
End Sub
If you decide that you want to mystify your user, try to following:
Sub RandomBeeps()
Dim lngFreq As Long 'The frequency
Dim i As Integer
For i = 1 To 12
lngFreq = Int(15000 * rnd) + 1
Beep lngFreq, 500
Next i
End Sub
This particular code just randomizes the frequency so when it plays you get what sounds like communication from another planet. If you want to make it more annoying just increase the 12 in the for statement above to 120. Or if you really want to upset someone rather than using a for loop use a
do while loop, such as the following:
Do while 1 = 1
lngFreq = Int(15000 * rnd) + 1
Beep lngFreq, 500
Loop
If you use this code, the computer will just start sounding tones randomly and never stop until your user figures out how to break the code, or shuts down his or her machine.
If you are interested in playing a prank on a friend, add this code into the start-up routine of the computer and then try to be around when it comes on. That might be a better thing to do around April 1, rather than some other time of year.
Conclusion
This month I have shown you a technique that will allow you to customize tones within your application. This can be a very good way to capture your user’s attention because this will be something that he or she is not used to hearing. If you want to make sure that the user doesn't get used to a particular sound. You can randomize the frequency, duration, and number of tone sounds to keep your user tuned in.
Trap of the Month: Be Careful if you are Changing the Beeping Sound
The code snippet I started off with (
Private Declare Function Beep Lib "kernel32" (ByVal dwFreq As Long, ByVal dwDuration As Long) As Long) has a scope that is limited to the module in which it appears. Doing it this way ensures that if you already have a call to the beep sound in your application, it will not interfere with any existing code that you have written.
If you were to change the private function to a public function, then any instance of your code that has a beep will throw an error that you will have to fix. It is easy enough to isolate this type of library call and potential conflict just by putting this particular library call in any modules where it is specifically needed.