Time for a quick VBA quiz. These questions cover a few small but important ideas that come up constantly when you start writing code behind Access forms: procedure scope, passing parameters, choosing between Select Case and If statements, and keeping validation where it belongs. See how many you get before checking the answers.
Grab a piece of paper if you want to keep score. There are five questions, and although none of them are especially evil, VBA has plenty of ways to make a tiny misunderstanding turn into an afternoon of staring at the screen wondering why nothing works.
Question 1: What does declaring a procedure as Private Sub mean in a form's VBA module?
A. Any form or module in the database can call it.
B. Only code in that form's module can call it.
C. It can run only from a command button's Click event.
D. It can be called only from a query.
Answer: B. Only code in that form's module can call it.
A Private procedure is local to the module where it is declared. If you create a helper routine inside a form module and mark it Private, code in that same form module can use it, but other forms and standard modules cannot call it directly. This is useful when the routine exists only to support that particular form. It keeps your code organized and prevents other parts of the database from depending on something that was never meant to be shared.
Question 2: Given a procedure declared like this: Private Sub Calculate(Operation As String), what does the call Calculate "Add" do?
A. It passes the text "Add" into the Operation parameter and runs the procedure.
B. It declares Operation as a new String variable.
C. It assigns the procedure's return value to Operation.
D. It passes both "Add" and "Subtract" to the procedure.
Answer: A. It passes the text "Add" into the Operation parameter and runs the procedure.
The word Operation is a parameter. Think of it as a placeholder the procedure uses to receive information from whatever code calls it. When you call the procedure with "Add", the parameter receives that text, and the procedure can decide to perform addition. The same routine could be called with "Subtract", "Multiply", or "Divide" instead. That is a lot cleaner than creating separate, nearly identical procedures for every calculator button.
Question 3: Why is Select Case often a good choice when one variable can contain several operation names?
A. It automatically converts every operation name into a number.
B. It runs every Case block so all operations are checked.
C. It organizes several alternatives around one expression and runs the matching Case.
D. It can be used only when the variable is numeric.
Answer: C. It organizes several alternatives around one expression and runs the matching Case.
If you have one variable, such as an operation name, with several possible values, Select Case is usually easy to read and maintain. You evaluate the expression once, then give each supported value its own Case section. VBA runs the matching Case and skips the rest. It is especially handy when your choices are mutually exclusive, which is usually the situation with calculator operations.
Question 4: What is the main control-flow advantage of an If...ElseIf chain over several separate If blocks when only one operation should run?
A. ElseIf repeats the first condition until it becomes false.
B. ElseIf causes all conditions to run at the same time.
C. ElseIf automatically returns a value from the procedure.
D. After a true condition is found, later ElseIf conditions are skipped.
Answer: D. After a true condition is found, later ElseIf conditions are skipped.
With a series of separate If statements, VBA evaluates every condition, even if an earlier one already matched. With an If, ElseIf chain, once VBA finds a true condition, it skips the remaining ElseIf tests. That makes it a better fit when only one choice should be performed.
One related gotcha: do not assume that putting multiple tests on one line automatically means VBA will stop evaluating as soon as it finds a true condition. Be careful with expressions joined by logical operators, particularly when later tests could cause an error or call a function you did not intend to run. Write the logic clearly rather than trusting your code to take the scenic route through Mordor safely.
Question 5: Inside a Select Case Operation block, where should a divide-by-zero test go if it applies only to division?
A. Before Select Case, so every operation must pass the division test.
B. Inside Case "Divide", before performing the division.
C. Inside Case Else, after all known operations fail.
D. After End Select, after the division has already occurred.
Answer: B. Inside Case "Divide", before performing the division.
Put validation as close as possible to the operation that needs it. Addition, subtraction, and multiplication do not care whether the second number is zero. Division definitely does. So the zero check belongs inside the division Case, immediately before the calculation happens.
This keeps the code easier to follow because anyone reading it can see the division rule right alongside the division logic. It also avoids forcing unrelated operations through checks that have nothing to do with them. That may seem like a small detail, but small details are where clean VBA code separates itself from a tangled pile of "why is this here?" conditions.
If you got all five, congratulations. You rode into the final battle with a trumpet and a plan. If you missed a couple, no worries. These concepts are covered in more depth in Access Developer Level 2, Lesson 2, where we continue building the calculator application and put this logic to work in a real project.
Watch the embedded video to take the quiz along with the slides, then check out the full class if you want the complete walkthrough and implementation details.
Live long and prosper,
RR