Skip to content
Auscultby NBIT
EN
Free diagnostic

Access error reference

Access error 91: Object variable or With block variable not set

The code uses an object (Recordset, form, class, Excel) that was never created, or was released in the meantime. VBA then stops on “Object variable or With block variable not set”.

The message

Microsoft Visual Basic box: “Run-time error '91': Object variable or With block variable not set”
The real error box, captured in Access for Microsoft 365, in English: the code reads RecordCount from a Recordset variable never assigned with Set.

Object variable or With block variable not set

English Access, VBA run-time error box (error 91)

On a French Access: « Variable objet ou variable de bloc With non définie ». The variable is Nothing when the code reads one of its properties or calls one of its methods.

Where it shows up

On a line that uses an object: rs.MoveNext, frm.Requery, xlApp.Workbooks.Open, gUser.Name, or inside a With … End With block.

Common causes

  • a missing Set: rs = CurrentDb.OpenRecordset("Customers") instead of Set rs = …;
  • an object never created on one code path: the Set sits inside an If that was not taken;
  • an object already released by Set x = Nothing, or closed before being reused;
  • a global variable reset: after an unhandled error and a click on “End”, the VBA project is reset and global objects are Nothing.

How to reproduce and check it

  1. Click “Debug” and find the object variable on the yellow line.
  2. In the Immediate window (Ctrl+G), type ? rs Is Nothing: True confirms the cause.
  3. Trace the code back to that variable's Set, and look for the path that skips it.

How to fix it

Create the object with Set before using it: Set rs = CurrentDb.OpenRecordset("Customers", dbOpenDynaset). For an object that may be missing, test it: If rs Is Nothing Then Exit Sub.

For a global object, go through a function that recreates it when needed rather than relying on start-up initialisation: If gUser Is Nothing Then Set gUser = New clsUser. Add error handling to procedures called by buttons: the project is no longer reset at the first error.

What Auscult reports

The diagnostic records error 91 with the form or button that raises it. The report explains it as “The code uses an object that was not set up beforehand: a form that is not open, a link that was not established” and suggests fixing how that object is used.

The Access error reference, code by code

Frequently asked questions

Why does error 91 appear after another error?

An unhandled error followed by a click on “End” resets the VBA project: every global variable is emptied. The next click that uses one hits Nothing.

The message mentions a With block: I have none.

The message is the same for any empty object variable. The With block is only one case: most often it is a missing Set or a released object.

Where are the other errors in your application hiding?

The diagnostic runs on your computer, on a copy of the application, without sending the data in your tables. The health grade and the summary are free.