How to Update an Access Date Field Using VBA When Value is Null
Question details
The user needs to update a Microsoft Access date field via a VBA SQL query, specifically handling scenarios where the input date control is Null without defaulting to a zero-time value.

- Product
- Microsoft Access
- Device & OS
- not provided
- Scenario
- Building and executing an UPDATE SQL query in Access VBA when a date control on a form might be empty.
- Observed behavior
- The UPDATE query fails due to invalid syntax, or replacing Null with zero stores an unintended default time value instead of leaving the database field empty.
Ensure you have a backup of your Access database and familiarize yourself with the VBA Editor before modifying your SQL query construction logic.
Conditionally Format Date Literals or Assign Null in VBA
Explicitly check for a Null value using an If statement before building the SQL string, ensuring the query receives either a valid date literal or the SQL keyword Null.
When concatenating form controls directly into an SQL string, a Null value can break the syntax and cause the query to fail. By checking for Null first, you can safely pass the string 'Null' for empty fields, or a properly formatted date literal for populated fields.
Use the IsNull() function in VBA to check the value of your form's date control. For example: If IsNull(Forms!YourForm!txtDateControl) Then...
If the control evaluates to Null, assign the string "Null" to a holding variable (e.g., strDate = "Null"). Do not wrap this in date delimiters.
If the control is not Null, format the date to an internationally safe literal and wrap it in hash marks: strDate = "#" & Format(Forms!YourForm!txtDateControl, "yyyy-mm-dd hh:nn:ss") & "#"
Concatenate the strDate variable into your UPDATE statement and execute it using CurrentDb.Execute "UPDATE YourTable SET DateField = " & strDate, dbFailOnError.

Looking for a Lightweight Office Suite with VBA Support?
While Microsoft Access handles complex relational databases, much of your daily data analysis and VBA automation can be done efficiently in powerful spreadsheet software. WPS Office provides robust VBA macro support, high compatibility with standard Office formats, and a familiar interface—all for free.
- 1. Download the software: Visit the official WPS website to download the free WPS Office installer.
- 2. Install WPS Office: Run the installation wizard and complete the setup process in just a few minutes.
- 3. Open your macro-enabled files: Launch WPS Spreadsheets, open your existing Excel (.xlsm) files, and seamlessly run your VBA code.

Frequently Asked Questions
Why does my Access VBA date update fail when the field is empty?
When a date control is empty, it evaluates to Null. If this is concatenated directly into an SQL string without explicit handling, it either creates an invalid SQL syntax or inserts a zero-value, which Microsoft Access translates to an arbitrary time value like 12:00 AM.
What is the best date format to use in Access VBA SQL queries?
The internationally safe format 'yyyy-mm-dd hh:nn:ss' wrapped in '#' delimiters is highly recommended. This prevents the database engine from misinterpreting days and months if the user's regional settings differ.
How does dbFailOnError help in executing VBA queries?
Using the 'dbFailOnError' argument when executing a query through 'CurrentDb.Execute' forces VBA to throw a descriptive run-time error if the SQL statement fails, allowing you to catch and debug syntax or constraint issues rather than having the query fail silently.
Can I use the 'Nz' function to handle Null dates in VBA?
While the 'Nz' function can replace a Null with an alternative value, substituting a Null date with 0 results in an unwanted default time being saved in the database. Explicitly passing the 'Null' keyword as a text string into the SQL statement is a much safer approach for date fields.




