OK, looking back over the previous posts in this thread, I understand the
query includes references to form controls, yes? Something like "SELECT *
FROM SomeTable WHERE SomeField = Forms!SomeForm!SomeControl"? To open a
recordset on a parameter query like this, you must assign values to the
parameters before attempting to open the recordset. DAO and ADO examples
below, or alternativly, it might be simpler not to use the saved query at
all and just build the complete SQL string in code, e.g. "SELECT Count(*) AS
TheCount FROM SomeTable WHERE SomeField = " & Forms!SomeForm!SomeControl".
In other words, build the same criteria into the SQL statement as in the
saved query. (Remember that text values in criteria need to be delmited with
single quotes, and dates need to be delimited with '#' characters and in US
mm/dd/yyyy format). In fact, the more I think about it, the more I think
that's probably the simplest solution. But I'll include the DAO and ADO
examples anyway for completeness ...
Public Sub TestDAO()
Dim db As DAO.Database
Dim qdf As DAO.QueryDef
Dim rst As DAO.Recordset
Dim boolFound As Boolean
Set db = CurrentDb
Set qdf = db.QueryDefs("qryTest")
qdf.Parameters("Forms!frmTest!txtTest") = Forms!frmTest!txtTest
Set rst = qdf.OpenRecordset
If Not (rst.BOF And rst.EOF) Then
boolFound = True
End If
rst.Close
End Sub
Public Sub TestADO()
Dim cmd As ADODB.Command
Dim prm As ADODB.Parameter
Dim rst As ADODB.Recordset
Dim boolFound As Boolean
Set cmd = New ADODB.Command
With cmd
.ActiveConnection = CurrentProject.Connection
.CommandText = "SELECT Count(*) AS TheCount FROM qryTest"
.CommandType = adCmdText
Set prm = .CreateParameter("Forms!frmTest!txtTest", adVarChar,
adParamInput, 50, Forms!frmTest!txtTest)
.Parameters.Append prm
End With
Set rst = cmd.Execute
boolFound = rst.Fields("TheCount") <> 0
End Sub