Tuesday, October 20, 2026

Can You Spot a VBA SQL INSERT Field Order Bug in Microsoft Access? Video Quiz D2.4

Think you have your Access VBA and SQL INSERT statements under control? Good. Now let's see if your code survives a few very common traps: inserting records without overwriting old ones, using the correct destination fields, handling Null values, and making sure your field order does not quietly scramble your data. Mars colony records are optional. Bad data is not.

This is a developer-level Access quiz with five questions. Give yourself a few seconds to answer each one before reading the explanation. If you want the full quiz experience, watch the embedded video and pause it before each answer.

Question 1: Your VBA button needs to add one new log record to a table without changing any existing records. Which type of SQL statement should you send to DoCmd.RunSQL?

The correct answer is INSERT INTO ... VALUES. An INSERT query adds a brand-new row to a table. An UPDATE query changes data in rows that already exist, which is definitely not what you want if the job is simply to add another record. SELECT queries retrieve or create data based on a query, but they are not the normal choice for adding one record from a form button.

Question 2: Suppose your INSERT statement includes a field named Commission, but the destination table does not actually have a field by that name. The form may have a text box named Commission, but that does not help the table. What fixes the problem?

The answer is to add the expected Commission field to the destination table, assuming Commission is data you really intend to store. The field list in an INSERT statement refers to fields in the table, not controls on your form. Access does not care that you have a nicely labeled text box on screen if there is nowhere in the table to put its value. Your form controls and table fields often have similar names, but they are not the same thing.

Question 3: Long SQL strings are often split across multiple VBA lines. You will usually see an ampersand followed by a space and underscore. What are those characters doing?

The ampersand concatenates, or joins, pieces of text together. The underscore is VBA's line-continuation character. It tells VBA, "I'm not done with this statement yet. Keep reading on the next line." This makes long SQL statements much easier to read and maintain. Without it, you end up with one giant ugly line of SQL that eventually wraps itself around the moon.

Question 4: An unbound text box can contain either a Null value or an empty string. If your validation code stops when IsNull(Me.Notes) is true, which reset value will make that guard work again on the next click?

The answer is Null, not an empty string. Setting the control to Me.Notes = Null gives it an actual Null value, so IsNull will return True later. Setting it to "" gives it a zero-length string instead. It may look blank on the screen either way, but VBA sees those as different values. This is one of those tiny details that can make validation seem broken when it is actually doing exactly what you told it to do.

Question 5: This is the field-order bug. EmployeeID starts with a value of 7 and Commission starts with a value of 50. The SQL statement is built using those values, and then EmployeeID is changed to 9 afterward. The INSERT statement's field list is ordered as Commission, EmployeeID, but the VALUES portion supplies EmployeeID first and Commission second. What gets inserted?

The result is Commission = 7 and EmployeeID = 50. SQL matches values to fields by position, not by the variable names you used while building the string. If your field list says (Commission, EmployeeID), then the first supplied value goes into Commission and the second goes into EmployeeID.

Also remember that changing EmployeeID after the SQL string has already been built does not change the SQL string retroactively. Once VBA has assembled that text, it is just text. The later value of 9 is irrelevant to the statement that was already created.

The safest habit is simple: when you write an INSERT statement, read the field list and the VALUES list side by side. Make sure each value is in the exact same position as its destination field. Your query can run perfectly without errors while putting the wrong information in the wrong columns, which is much worse than an error message. At least an error message has the decency to complain.

How did you do? If you missed one or two, no worries. These are exactly the kinds of details that separate SQL that merely runs from SQL that stores the right data. Watch the embedded video for the quiz walkthrough, and if you want more practice adding records with VBA and resetting forms for the next entry, check out the Microsoft Access Developer Level 2 material.

Live long and prosper,
RR

No comments:

Post a Comment