QueryDefs and Recordsets

J

Jim Pockmire

I would like to create a querydef (to extract records from a table) and then
use the querydef as a recordset (so as to Movenext) through the records Can
I do this without saving the querydef as a query object first?
 
P

PC Datasheet

Dim Db As DAO.Database
Dim Rst As DAO.Recordset
Dim QDF As DAO.QueryDef
Dim Prm As Parameter
Dim SQLStr As String
SQLStr = "....."
Set Db = CurrentDb
Set QDF = Db.QuerfDefs("MyQuery")
QDF.SQL = SQLStr
For Each Prm In QDF.Parameters
Prm.Value = EVAL(Prm.Name)
Next Prm
Set Rst = Db.OpenRecordset("MyQuery")
Rst.MoveNext
 
K

Ken Snell [MVP]

Any reason why you cannot just open a recordset to extract the records,
without the need to create another query object?

Dim dbs As DAO.Database
Dim rst As DAO.Recordset
Dim strSQL As String

Set dbs = CurrentDb

strSQL = "SELECT * FROM Table_Name " & _
"WHERE [FieldName] = 4;"
Set rst = dbs.OpenRecordset(strSQL, dbOpenDynaset, dbReadOnly)
' loop through the recordset as you wish with code
'
rst.Close
Set rst = Nothing
dbs.Close
Set dbs = Nothing
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top