The Problem (Q-score 6, ranked #27th of 67 in the Access VBA archive)
The scenario as originally posted in 2010
I am accessing an MS Access 2007 database through C#, and I keep getting an exception whenever I try to read an empty cell.
Specifically, I am trying to read a “Date/Time” cell that may or may not be empty.
I am using OLE DB, and have filled a DataSet. None of these conditions work:
DataSet dataSet = GetDataSet();
DataRow row = dataSet.Tables[0].Rows[0];
DateTime time = new DateTime();
time = (DateTime)row[5]; // Exception thrown
How to check if the cell is empty before trying to assign it? None of these work:
if(row[5] == null) ;
if(row[5] == DBNull) ;
if(row[5] == (String)"") ;
Edit: I should have mentioned: When I debug, it says that row[5] equals “System.DBNull,”, but I get an error when I try “if(row[5] == DBNULL)”. The error says “DBNULL is a type, which is not valid in the given context”.
Why community consensus is tight on this one
Across 67 Access VBA entries in the archive, the accepted answer here holds niche answer (below median) status — meaning voters are unusually aligned on the right fix.
The Verified Solution — niche answer (below median) (+8)
13-line Access VBA pattern (copy-ready)
You can check it like the following.
if (row[5] == DBNull.Value)
{
// value is null
}
else if (String.IsNullOfEmpty(Convert.ToString(row[5]))
{
// value is still null
}
else
{
// value is not null here
}
When to Use It — vintage (14+ years old, pre-2013)
Ranked #27th in its category — specialized fit
This pattern sits in the 74% tail relative to the top answer. Reach for it when your scenario closely matches the question title; otherwise browse the Access VBA archive for a higher-consensus alternative.
What changed between 2010 and 2026
The answer is 16 years old. The Access VBA object model has been stable across Office 2013, 2016, 2019, 2021, 365, and 2024/2026 LTSC, so the pattern still compiles. Changes that might affect you: 64-bit API declarations (use PtrSafe), blocked macros in downloaded files (Mark-of-the-Web), and the shift toward Office Scripts for web-first workflows.