Executing Parameterized Queries From VBA
When running Parameterized queries from the Access interface, the user is asked to enter the parameter. To accomplish this from VBA, parameter values need to be supplied before the query is executed. There are two distinctly different methods to accomplish this:
- The first method is for Append, Make Table, Update and Delete queries
- The second method is for Select queries with recordsets
A parameterized query appears as follows:

The code to demonstrate how to supply [Param1] from VBA (For Both Types) follows, and it uses the QueryDefs object.
Program Code
Option Compare Database
Option Explicit
Public Sub TestParamQuery()
' ***********************************************************************
' Using Parameters in an Append, Update, Delete or Make Table Query
' ***********************************************************************
Dim qd As DAO.QueryDef
Dim rs As DAO.Recordset
' ***********************************************************************
' Set qd To The Value of an Existing QueryDef
' ***********************************************************************
Set qd = CurrentDb.QueryDefs("qryTestParams")
' ***********************************************************************
' Assign A Value to the Query's Parameter Param1
' ***********************************************************************
qd!Param1 = "A"
' ***********************************************************************
' Execute The Query With a Parameter Value
' ***********************************************************************
qd.Execute dbFailOnError
End Sub
Public Function TestSelectQuery()
' ***********************************************************************
' Using Parameters in a SELECT Query
' ***********************************************************************
Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim qdf As DAO.QueryDef
' ***********************************************************************
' Set Up DB and Query Definitions
' ***********************************************************************
Set db = CurrentDb()
Set qdf = db.QueryDefs("qryTestSelectWithParams")
qdf!Param1 = "A"
' ***********************************************************************
'Now we'll convert the querydef to a recordset and run it
' ***********************************************************************
Set rst = qdf.OpenRecordset
If rst.EOF Then
rst.Close
Exit Function
End If
' ***********************************************************************
' Loop Through All The Records Matching Param1
' ***********************************************************************
Do
MsgBox ("Value is " & rst!Name)
rst.MoveNext
Loop Until rst.EOF
rst.Close
qdf.Close
Set rst = Nothing
Set db = Nothing
Set qdf = Nothing
End Function
