CumbrianCanary
Registered User.
- Local time
- Today, 13:06
- Joined
- Sep 13, 2004
- Messages
- 22
Exporting query results to Excel
Hi,
I am writing some code to export results from a query to Excel. I have managed to get the correct Excel file open and can write test to it no problem. The issue comes when I want to use data from a query rather than set text that is hard coded.
I have replaced the set text section in the code with
But it is causing an 424 Object required error. I think it may be due to the
Does this work in Access97? It comes up with some square boxes within the string. It seemed the easy way to convert the SQL into VBA, but I expect I need to learn how to do this. Am I barking up the wrong tree with this one?
Thanks in advance.
CC
Hi,
I am writing some code to export results from a query to Excel. I have managed to get the correct Excel file open and can write test to it no problem. The issue comes when I want to use data from a query rather than set text that is hard coded.
I have replaced the set text section in the code with
strSQL = CurrentDb.QueryDefs("qryMyQuery").SQL
Set dbs = CurrentDb
Set rst = dbs.OpenRecordset(strSQL, dbOpenSnapshot)
If Not rst.BOF Then
rst.MoveFirst
'For this template, the data must be placed in the appropriate cells of the spreadsheet
Do While Not rst.EOF
With strWorkbookName
.Sheets("Sheet1").Range("A3") = rst.Fields("FieldName1")
.Sheets("Sheet1").Range("B3") = rst.Fields("FieldName2")
.Sheets("Sheet1").Range("C3") = rst.Fields("FieldName3")
.Sheets("Sheet1").Range("D3") = rst.Fields("FieldName4")
.Sheets("Sheet1").Range("E3") = rst.Fields("FieldName5")
End With
rst.MoveNext
Loop
rst.Close
End If
But it is causing an 424 Object required error. I think it may be due to the
strSQL = CurrentDb.QueryDefs("qryMyQuery").SQL
Does this work in Access97? It comes up with some square boxes within the string. It seemed the easy way to convert the SQL into VBA, but I expect I need to learn how to do this. Am I barking up the wrong tree with this one?
Thanks in advance.
CC