I have successfully used this type of code to create permanent queries that are much faster to access from vba code. (It seems to be much faster than using normal DAO RecordSet code with queries or tables).
It also lets you use parameters.
I first define a query in the normal query design mode and save it. Then I add PARAMETERS to the top of the SQL.
I can then access it as a QueryDef object from the code and create a recordset from it.
I am using Access 2003
My problem is that when I perform a change in both the code and in the SQL PARAMETERS section to remove a parameter, it gives the error
"Too Few Parameters. Expected 3" for example.
SQL for the query:
I created a permanent query that appears in the list of queries
Code in the module utilizing the QueryDef:
It runs when I first define it and then when I go back, for example and remove parameter and code for strOther, prmOTHER it gives me the error I mentioned.
Seems like the only way I can get rid of it is to reconstruct the query again from scratch, ending up with the same code that gave me the error.
Is it compiling it interally and needs to be removed somehow and then re-defined?
It also lets you use parameters.
I first define a query in the normal query design mode and save it. Then I add PARAMETERS to the top of the SQL.
I can then access it as a QueryDef object from the code and create a recordset from it.
I am using Access 2003
My problem is that when I perform a change in both the code and in the SQL PARAMETERS section to remove a parameter, it gives the error
"Too Few Parameters. Expected 3" for example.
SQL for the query:
Code:
PARAMETERS lngTeamID Long, lngShiftID Long, strOther Text ( 255 ); SELECT DISTINCTROW Log.ID, Log.ID2, Log.[Other] FROM Log WHERE Log.ID=[lngID]) AND Log.ID2=[lngID2] AND Log.[Other]=[strOther]
Code in the module utilizing the QueryDef:
Code:
Sub Test1 (ID as Strong, ID2 As String)
Dim qdf As QueryDef, prmID As Parameter, prmID2 As Parameter, prmOther As Parameter
' Use pre-defined (compiled) QueryDefs for better performance than OpenRecordSet(strSQL)
Dim rst As Recordset, strOther_text As String
Set qdf = CurrentDb.QueryDefs("qry_def_Log")
Set prmID = qdf.Parameters!lngID
Set prmID2 = qdf.Parameters!lngID2
Set prmOTHER = qdf.Parameters("strOTHER")
prmID = ID ' From function parameters
prmID2 = ID2
prmOTHER = "1"
Set rst = qdf.OpenRecordset(dbOpenSnapshot)
If rst.RecordCount > 0 Then
rst.MoveFirst
Other statements…
End If
Exit Sub
Seems like the only way I can get rid of it is to reconstruct the query again from scratch, ending up with the same code that gave me the error.
Is it compiling it interally and needs to be removed somehow and then re-defined?
Comment