FrankParsosns911
New member
- Local time
- Tomorrow, 01:26
- Joined
- Sep 30, 2019
- Messages
- 6
What I have done so far:
Researched many sites including this one.
Tried a number of suggestions, copied code, modified it. Not successful.
What I don't need:
No DoCmd Transfers please as my Excel SS has 4 rows for headings, some cells merged. I have been down that path for weeks and my research says no go.
Success so far:
The code below successfully exports the data to my designated Excel spreadsheet, but ALL the data from the recordset is exported to the named sheets. In other words, too successful.
What I need help with:
Export a query to excel, but filter the query within the database.
I have named spreadsheets that I have to export data to.
I have tried WHERE statements to no avail, probably because I don't know the syntax.
I appreciate any suggestions. You will notice I have included some 'notes in my code. Please note that eventually I will loop from H1 to H60. That is why I have defined HR as a variable, but not yet used it.
Here is my code so far.
Private Sub Command137_Click()
'Export query results to a specific location in a specific spreadsheet
Dim dbs As Database
Dim HR As String
Set dbs = CurrentDb
'The target Excel SS is say (C:\Continuum\H42.xlsx). Set HR to H42
HR = H42
Set rsQuery = dbs.OpenRecordset("QExportCount")
' "QExportCount" contains 1300 records each with a HR field that ranges from H1 to H60. In this example I have targetted H42.
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.workbooks.Open("C:\Continuum\H42.xlsx")
targetWorkbook.Worksheets("Counting").Range("A5").CopyFromRecordset rsQuery
'This is where my difficulty lies; How to filter?
' I need to FILTER out say only the records that have Field HR = 42
'That will allow me to export only those records, say 25 of them.
'Do.cmd does not work for me as my Excel SS has 4 rows of headings, some meged cells.
End Sub
Researched many sites including this one.
Tried a number of suggestions, copied code, modified it. Not successful.
What I don't need:
No DoCmd Transfers please as my Excel SS has 4 rows for headings, some cells merged. I have been down that path for weeks and my research says no go.
Success so far:
The code below successfully exports the data to my designated Excel spreadsheet, but ALL the data from the recordset is exported to the named sheets. In other words, too successful.
What I need help with:
Export a query to excel, but filter the query within the database.
I have named spreadsheets that I have to export data to.
I have tried WHERE statements to no avail, probably because I don't know the syntax.
I appreciate any suggestions. You will notice I have included some 'notes in my code. Please note that eventually I will loop from H1 to H60. That is why I have defined HR as a variable, but not yet used it.
Here is my code so far.
Private Sub Command137_Click()
'Export query results to a specific location in a specific spreadsheet
Dim dbs As Database
Dim HR As String
Set dbs = CurrentDb
'The target Excel SS is say (C:\Continuum\H42.xlsx). Set HR to H42
HR = H42
Set rsQuery = dbs.OpenRecordset("QExportCount")
' "QExportCount" contains 1300 records each with a HR field that ranges from H1 to H60. In this example I have targetted H42.
Set excelApp = CreateObject("Excel.application", "")
excelApp.Visible = True
Set targetWorkbook = excelApp.workbooks.Open("C:\Continuum\H42.xlsx")
targetWorkbook.Worksheets("Counting").Range("A5").CopyFromRecordset rsQuery
'This is where my difficulty lies; How to filter?
' I need to FILTER out say only the records that have Field HR = 42
'That will allow me to export only those records, say 25 of them.
'Do.cmd does not work for me as my Excel SS has 4 rows of headings, some meged cells.
End Sub