Hi, I would like to export several queries into one Excel file, with each query on a separate worksheet. I've gotten a bit of advice from the Excel board about doing this from Excel as opposed to from Access. Ideally, though, I would like to run this from my Access DB. Any suggestions on this would be appreciated. Currently my system works as follows:
1. From a form, the user selects the record ID she would like to query.
2. When the user clicks "go", the query is run, Excel opens, and the query is placed in the Excel file that is created in the same location that the Access DB exists on the user's computer (this location is variable, depending on who is using the DB.)
I have this working for 1 query, but ideally, I would like to run multiple queries at once, and then put each of these in a different worksheet. The code I have so far is below:
Private Sub runquery(qry As String)
Dim FileName As String
Dim FilePath As String
Dim CurrDate As String
'The purpose of this query is to open the selected query, and then to export
'these data to an excel file.
'qry is the passed argument, specifiying the name of the particular query I want to export.
DoCmd.OpenQuery qry, acViewNormal, acEdit
'This specifies the name and location of the created Excel file.
FileName = "Query " & CurrDate & Me!UNQID
FilePath = ObtainDir(Application.CurrentDb.name) & FileName & ".xls"
'Exports data to the Excel file.
DoCmd.OutputTo acOutputQuery, , acFormatXLS, FilePath, -1
1. From a form, the user selects the record ID she would like to query.
2. When the user clicks "go", the query is run, Excel opens, and the query is placed in the Excel file that is created in the same location that the Access DB exists on the user's computer (this location is variable, depending on who is using the DB.)
I have this working for 1 query, but ideally, I would like to run multiple queries at once, and then put each of these in a different worksheet. The code I have so far is below:
Private Sub runquery(qry As String)
Dim FileName As String
Dim FilePath As String
Dim CurrDate As String
'The purpose of this query is to open the selected query, and then to export
'these data to an excel file.
'qry is the passed argument, specifiying the name of the particular query I want to export.
DoCmd.OpenQuery qry, acViewNormal, acEdit
'This specifies the name and location of the created Excel file.
FileName = "Query " & CurrDate & Me!UNQID
FilePath = ObtainDir(Application.CurrentDb.name) & FileName & ".xls"
'Exports data to the Excel file.
DoCmd.OutputTo acOutputQuery, , acFormatXLS, FilePath, -1