Datasheet
Designing Applications
41
In this case of course, we know exactly what caused the problem: the user, right? Wrong. Ask
the user: sloppy programming caused the problem!
Rule #1 of programming – the user is always right because they pay the bills.
While we cannot guard against absolutely every single incorrect action the user may take, we
really ought to try to do something against a common possibility like this. One way to solve the
problem would be to make the buttons intelligent so that they only allowed you to click them if
they represented a valid choice? In other words, if you were already at the first record, the
Previous button should appear grayed out or disabled.
Sure, you may say, but how? There doesn't appear to be any way to determine where you are in
the table when you're using macros. So how is it done? You have just come across one of the
shortcomings of macros. Macros are good at automating simple tasks, but they're less useful
when the task (or the logic behind the task) becomes more complex. The only way to make
these buttons intelligent is to use VBA, and we'll show you how to do this in the next chapter.
Macros Or VBA?
Obviously, there are some simple tasks that can be performed happily by macros, but the
example above should have highlighted one of their limitations. We could create navigation
using macros, but we could not disable or enable them according to where we were in the
records behind the form. That may not be a problem for some people, but if you want a slick
interface that will win over your end-users, you'll probably want to enable and disable buttons.
Our users will be sitting in front of this screen a lot, so we want to get it right.
Why You Should Use VBA
The advantages that VBA has over macros can be summarized as follows:
VBA enables you to provide complex functionality.
You'll remember that when we tried to move back to the previous record from the first record
we encountered an error and Access displayed an error message. What if we wanted to display
our own error message instead? This type of intelligence isn't possible with macros.
You can trap (intercept) and handle errors using VBA.
Handling errors is impossible with macros but simple enough with VBA. Also, in some
circumstances, you have to handle errors yourself. If you don't, your application could easily
crash! We look in detail at error handling in Chapter 12.
VBA is faster to execute than macros.
VBA code is executed faster than macros. Although you may not notice the difference in a one-
line macro, the difference in speed becomes more noticeable the longer and more complex the
macro you are creating. Since speed is normally a critical factor in impressing end-users, we
have another notch in favor of VBA.










