Using recordset from a query

G

Guest

I am unable to run code to export data from a query to excel using
Set rs = CurrentDb.OpenRecordSet("qryLocations"), _
dbOpenSnapshot

I am using a DAO connection in Access 2000.
My code works fine when I create a SQL string :

Set db = CurrentDb
Set rs = db.OpenRecordset(sSQL, dbOpenSnapshot)

I have data from several queries I need to export and do not want to have to
write a sql statement for each one. I would like to be able to just set the
connection to the query object itself if possible.
 
J

Jason Lepack

You ahve a small bracketing oops...

Set rs = CurrentDb.OpenRecordSet("qryLocations"), _
dbOpenSnapshot

this should be:
Set rs = CurrentDb.OpenRecordSet("qryLocations", _
dbOpenSnapshot)

Cheers,
Jason Lepack
 
G

Guest

Thanks Jason. Now I need to pass two parameters to the query. I have a form
with txtLocation and txtMonYr

The declaration of the function is:
Public Function ExportDetail(Locn As String, MnYr As String) As String

The click event on the form is:
Private Sub cmdExport_Click()
Call ExportDetail(txtLoc, txtPeriod)
End Sub

Can I still use the OpenRecordSet Method using parameters?
 
J

Jason Lepack

The key is to use a querydef to set the parameters then open the
recordset from that.

I created a query called "Query2" with two parameters "val1" and
"val2" and two fields, "field1" and "field2"

Query2:
SELECT field1, field2
FROM Table1
WHERE field1 Between [val1] And [val2];

' This code demonstrates use of parameters with a querydef
Public Sub testthis()
Dim db As Database
Dim qdf As QueryDef
Dim rs As Recordset

' reference to current database
Set db = CurrentDb

' load the query to the querydef
Set qdf = db.QueryDefs("Query2")

' set the parameters
qdf.Parameters("val1") = 4
qdf.Parameters("val2") = 8

' open the query in the recordset
Set rs = qdf.OpenRecordset(dbOpenDynaset)

' print the results of the query to the immediate window
Do While Not rs.EOF
Debug.Print rs!field1 & " " & rs!field2
rs.MoveNext
Loop

' clean up
Set rs = Nothing
Set qdf = Nothing
Set db = Nothing
End Sub


Cheers,
Jason Lepack
 
G

Guest

It seems when I try to use querydef, I get a 3265 error "item not found in
thiscollection". I created the table and the query then I pasted your code
and still got the error.
at qdf.Parameters("val1") = 4


Jason Lepack said:
The key is to use a querydef to set the parameters then open the
recordset from that.

I created a query called "Query2" with two parameters "val1" and
"val2" and two fields, "field1" and "field2"

Query2:
SELECT field1, field2
FROM Table1
WHERE field1 Between [val1] And [val2];

' This code demonstrates use of parameters with a querydef
Public Sub testthis()
Dim db As Database
Dim qdf As QueryDef
Dim rs As Recordset

' reference to current database
Set db = CurrentDb

' load the query to the querydef
Set qdf = db.QueryDefs("Query2")

' set the parameters
qdf.Parameters("val1") = 4
qdf.Parameters("val2") = 8

' open the query in the recordset
Set rs = qdf.OpenRecordset(dbOpenDynaset)

' print the results of the query to the immediate window
Do While Not rs.EOF
Debug.Print rs!field1 & " " & rs!field2
rs.MoveNext
Loop

' clean up
Set rs = Nothing
Set qdf = Nothing
Set db = Nothing
End Sub


Cheers,
Jason Lepack

Thanks Jason. Now I need to pass two parameters to the query. I have a form
with txtLocation and txtMonYr

The declaration of the function is:
Public Function ExportDetail(Locn As String, MnYr As String) As String

The click event on the form is:
Private Sub cmdExport_Click()
Call ExportDetail(txtLoc, txtPeriod)
End Sub

Can I still use the OpenRecordSet Method using parameters?












- Show quoted text -
 
J

Jason Lepack

Do you have a parameter called "val1" in your query?

Post your SQL.

Cheers,
Jason Lepack

It seems when I try to use querydef, I get a 3265 error "item not found in
thiscollection". I created the table and the query then I pasted your code
and still got the error.
at qdf.Parameters("val1") = 4



Jason Lepack said:
The key is to use a querydef to set the parameters then open the
recordset from that.
I created a query called "Query2" with two parameters "val1" and
"val2" and two fields, "field1" and "field2"
Query2:
SELECT field1, field2
FROM Table1
WHERE field1 Between [val1] And [val2];
' This code demonstrates use of parameters with a querydef
Public Sub testthis()
Dim db As Database
Dim qdf As QueryDef
Dim rs As Recordset
' reference to current database
Set db = CurrentDb
' load the query to the querydef
Set qdf = db.QueryDefs("Query2")
' set the parameters
qdf.Parameters("val1") = 4
qdf.Parameters("val2") = 8
' open the query in the recordset
Set rs = qdf.OpenRecordset(dbOpenDynaset)
' print the results of the query to the immediate window
Do While Not rs.EOF
Debug.Print rs!field1 & " " & rs!field2
rs.MoveNext
Loop
' clean up
Set rs = Nothing
Set qdf = Nothing
Set db = Nothing
End Sub
Cheers,
Jason Lepack

- Show quoted text -
 
G

Guest

Sorry, I had a table1 in my database. When I resolved that I no longer got
the error.
Now I have a new error
When it got to
Set rs = qdf.OpenRecordset(dbOpenDynaset)
I got a run time error 13 type mismatch.
Could these be caused but the data types in the table?


Jason Lepack said:
Do you have a parameter called "val1" in your query?

Post your SQL.

Cheers,
Jason Lepack

It seems when I try to use querydef, I get a 3265 error "item not found in
thiscollection". I created the table and the query then I pasted your code
and still got the error.
at qdf.Parameters("val1") = 4



Jason Lepack said:
The key is to use a querydef to set the parameters then open the
recordset from that.
I created a query called "Query2" with two parameters "val1" and
"val2" and two fields, "field1" and "field2"
Query2:
SELECT field1, field2
FROM Table1
WHERE field1 Between [val1] And [val2];
' This code demonstrates use of parameters with a querydef
Public Sub testthis()
Dim db As Database
Dim qdf As QueryDef
Dim rs As Recordset
' reference to current database
Set db = CurrentDb
' load the query to the querydef
Set qdf = db.QueryDefs("Query2")
' set the parameters
qdf.Parameters("val1") = 4
qdf.Parameters("val2") = 8
' open the query in the recordset
Set rs = qdf.OpenRecordset(dbOpenDynaset)
' print the results of the query to the immediate window
Do While Not rs.EOF
Debug.Print rs!field1 & " " & rs!field2
rs.MoveNext
Loop
' clean up
Set rs = Nothing
Set qdf = Nothing
Set db = Nothing
End Sub
Cheers,
Jason Lepack
On Mar 13, 8:58 am, JoeA2006 <[email protected]>
wrote:
Thanks Jason. Now I need to pass two parameters to the query. I have a form
with txtLocation and txtMonYr
The declaration of the function is:
Public Function ExportDetail(Locn As String, MnYr As String) As String
The click event on the form is:
Private Sub cmdExport_Click()
Call ExportDetail(txtLoc, txtPeriod)
End Sub
Can I still use the OpenRecordSet Method using parameters?
:
You ahve a small bracketing oops...
Set rs = CurrentDb.OpenRecordSet("qryLocations"), _
dbOpenSnapshot
this should be:
Set rs = CurrentDb.OpenRecordSet("qryLocations", _
dbOpenSnapshot)
Cheers,
Jason Lepack
On Mar 12, 5:04 pm, JoeA2006 <[email protected]>
wrote:
I am unable to run code to export data from a query to excel using
Set rs = CurrentDb.OpenRecordSet("qryLocations"), _
dbOpenSnapshot
I am using a DAO connection in Access 2000.
My code works fine when I create a SQL string :
Set db = CurrentDb
Set rs = db.OpenRecordset(sSQL, dbOpenSnapshot)
I have data from several queries I need to export and do not want to have to
write a sql statement for each one. I would like to be able to just set the
connection to the query object itself if possible.- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 
J

Jason Lepack

Yes, if you're using strings then you need ' ' and if you're using
dates you need # # around your variables.

Cheers,
Jason Lepack

Sorry, I had a table1 in my database. When I resolved that I no longer got
the error.
Now I have a new error
When it got to
Set rs = qdf.OpenRecordset(dbOpenDynaset)
I got a run time error 13 type mismatch.
Could these be caused but the data types in the table?



Jason Lepack said:
Do you have a parameter called "val1" in your query?
Post your SQL.
Cheers,
Jason Lepack
It seems when I try to use querydef, I get a 3265 error "item not found in
thiscollection". I created the table and the query then I pasted your code
and still got the error.
at qdf.Parameters("val1") = 4
:
The key is to use a querydef to set the parameters then open the
recordset from that.
I created a query called "Query2" with two parameters "val1" and
"val2" and two fields, "field1" and "field2"
Query2:
SELECT field1, field2
FROM Table1
WHERE field1 Between [val1] And [val2];
' This code demonstrates use of parameters with a querydef
Public Sub testthis()
Dim db As Database
Dim qdf As QueryDef
Dim rs As Recordset
' reference to current database
Set db = CurrentDb
' load the query to the querydef
Set qdf = db.QueryDefs("Query2")
' set the parameters
qdf.Parameters("val1") = 4
qdf.Parameters("val2") = 8
' open the query in the recordset
Set rs = qdf.OpenRecordset(dbOpenDynaset)
' print the results of the query to the immediate window
Do While Not rs.EOF
Debug.Print rs!field1 & " " & rs!field2
rs.MoveNext
Loop
' clean up
Set rs = Nothing
Set qdf = Nothing
Set db = Nothing
End Sub
Cheers,
Jason Lepack
On Mar 13, 8:58 am, JoeA2006 <[email protected]>
wrote:
Thanks Jason. Now I need to pass two parameters to the query. I have a form
with txtLocation and txtMonYr
The declaration of the function is:
Public Function ExportDetail(Locn As String, MnYr As String) As String
The click event on the form is:
Private Sub cmdExport_Click()
Call ExportDetail(txtLoc, txtPeriod)
End Sub
Can I still use the OpenRecordSet Method using parameters?
:
You ahve a small bracketing oops...
Set rs = CurrentDb.OpenRecordSet("qryLocations"), _
dbOpenSnapshot
this should be:
Set rs = CurrentDb.OpenRecordSet("qryLocations", _
dbOpenSnapshot)
Cheers,
Jason Lepack
On Mar 12, 5:04 pm, JoeA2006 <[email protected]>
wrote:
I am unable to run code to export data from a query to excel using
Set rs = CurrentDb.OpenRecordSet("qryLocations"), _
dbOpenSnapshot
I am using a DAO connection in Access 2000.
My code works fine when I create a SQL string :
Set db = CurrentDb
Set rs = db.OpenRecordset(sSQL, dbOpenSnapshot)
I have data from several queries I need to export and do not want to have to
write a sql statement for each one. I would like to be able to just set the
connection to the query object itself if possible.- Hide quoted text -
- Show quoted text -- Hide quoted text -
- Show quoted text -- Hide quoted text -

- Show quoted text -
 

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