Data Import & Export

Fix Null Values in an Access VBA SELECT DISTINCT Recordset

Camila MilosovichCamila Milosovich Nov 22, 2026 868 views

Question details

Determine why a SELECT DISTINCT query returns Null when its first field is read from an Access VBA recordset.

Fix Null Values in an Access VBA SELECT DISTINCT Recordset
Product
Microsoft Access
Device & OS
not provided
Scenario
An Access VBA procedure reads distinct values from a table imported from an HTML-formatted file.
Observed behavior
The query works in the Access query designer, but the VBA recordset encounters a Null value in its first field. The imported table contains a mostly blank record with an ID.
Before you start

Identify the imported table and the field being read from the recordset. Open the table in Access Datasheet View and check whether a record has an ID but no value in that field.

Solution 1Recommended

Filter Null and blank values from the DISTINCT query

Use this approach when records without a value in the selected field should not appear in the VBA results.

DISTINCT removes duplicate results; it does not exclude Null. A record with an assigned ID can still have a Null value in another field.

1
Check the imported field

Open the imported table in Datasheet View and locate the field your VBA code reads. Confirm that records without a value in that field should be excluded.

2
Add a query filter

In the SQL used by OpenRecordset, replace the example names with your own: SELECT DISTINCT [FieldName] FROM [WeeklyReport] WHERE [FieldName] Is Not Null AND [FieldName] <> '';

3
Run the recordset loop

Run the VBA procedure again and check the returned values. Use an If Not rs.EOF check before reading rs.Fields(0), and call rs.MoveNext inside the loop.

Filter Null and blank values from the DISTINCT query
Whitespace-only values: The example excludes Null and zero-length strings, but not values containing only spaces. If those are invalid too, use a suitable Trim expression for the text field.
Free Microsoft Office alternative

Review Imported Data with WPS Spreadsheets

WPS Office is a free, lightweight alternative with a familiar interface for reviewing spreadsheet data. Its Microsoft Office format compatibility makes it easier to move existing files into your workflow, though it does not fix Access VBA or database imports.

Review spreadsheet rows and filter values when checking imported dataCompatible with Microsoft Excel formats, including XLSX and XLSFamiliar spreadsheet interface for an easier transitionFree and lightweight Office alternative
microsoft office alternative - wps office

Frequently Asked Questions

Why does SELECT DISTINCT return Null in Access?

DISTINCT removes duplicate values but can return Null when the selected field contains it. Add WHERE [FieldName] Is Not Null to exclude those results.

How can I exclude both Null and empty strings?

For a text field, add WHERE [FieldName] Is Not Null AND [FieldName] <> '' to the query. Use an additional Trim expression if whitespace-only values should also be excluded.

How do I check for Null in a VBA recordset?

Check that the recordset is not at EOF, then use IsNull(rs.Fields(0).Value) before processing the first field. This lets you handle Null without treating it as an ordinary string.

Why does an imported record have an ID but no other values?

An ID can be assigned even when other imported fields are blank. Inspect the imported table in Datasheet View and review the HTML source to determine which records are invalid.