Skip to content
Auscultby NBIT
EN
Free diagnostic

Access error reference

Access error 13: Type mismatch in VBA and queries (how to fix)

“Type mismatch”: VBA received a value it cannot convert to the type required. Text where the code expects a number, a badly written date, or an object from a different library than the one intended.

The message

Microsoft Visual Basic box: “Run-time error '13': Type mismatch”
The real error box, captured in Access for Microsoft 365, in English: a customer's name (text) is stored in a Long variable.

Type mismatch

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

On a French Access: « Incompatibilité de type ». In a query, the equivalent has another number (3464, “Data type mismatch in criteria expression”).

Where it shows up

In VBA code: an assignment (lngQty = Me.txtQty), a calculation, a comparison, a function called with an argument of the wrong type, or Set rs = CurrentDb.OpenRecordset(…).

Common causes

  • text in a numeric variable: “abc”, “12 kg” or an empty string "" put into a Long, Integer or Double;
  • a date written as text that VBA does not recognise as a date under the PC's regional settings;
  • Dim rs As Recordset with both the DAO and ADO references ticked: if ADO comes before DAO, the variable becomes an ADO Recordset, while CurrentDb.OpenRecordset returns a DAO one.

How to reproduce and check it

  1. Click “Debug” and read the yellow line: the variable on the left gives the type expected.
  2. In the Immediate window (Ctrl+G), type ? TypeName(Me.txtQty) and ? Me.txtQty to see what actually arrives.
  3. Try again with the control empty, then with malformed input (letters, a comma or a decimal point).

How to fix it

Check the input before converting: If Not IsNumeric(Me.txtQty) Then MsgBox "Invalid quantity.": Exit Sub, then lngQty = CLng(Me.txtQty). For a date: If IsDate(Me.txtDate) Then dtStart = CDate(Me.txtDate).

For Recordsets, name the library: Dim rs As DAO.Recordset, then Set rs = CurrentDb.OpenRecordset("Customers", dbOpenDynaset). Also give controls a format (number, date) and a validation rule: bad input is refused before it reaches the code.

What Auscult reports

The diagnostic records error 13 with the form, report or button that raises it, like the related type errors in queries. The report explains it as “A value is not of the kind expected: text instead of a number or a date, for example” and suggests fixing the value involved.

The Access error reference, code by code

Frequently asked questions

Why does the same code work on another PC?

Date and number conversions follow Windows regional settings: decimal separator, day-month order. Text converted without error on one PC can fail on another.

Should I untick the ADO reference?

Only if nothing uses it. The safest is to write DAO.Recordset or ADODB.Recordset in every declaration: the code no longer depends on the order of the references.

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.