I have a database that uses filters to display data on forms and reports. Now I need to send the filtered data to an Excel Spreadsheet. I managed to do this two ways:
1. Using a recordset and sending the data to an Excel object; but this method is SLOW and I've tried to optimize the method as best I could. Below is the function that performs the operation.
Public Function procExpQry(strQry As String, strCriteria As String) As Boolean
Dim db As Database
Dim qryPass As QueryDef
Dim qryLocal As QueryDef
Dim rs As Recordset
Dim strSaveAs As String
Dim strSQL As String
Dim introwas Integer
Dim intcol As Integer
Dim objXL As New Excel.Application
Set objXL = CreateObject("Excel.Application"
objXL.Workbooks.Add
Set db = CurrentDb()
Set qryPass = db.QueryDefs(strQry)
strSQL = qryPass.SQL
strSQL = fucnRefineSQL(strSQL) 'will change to proper SQL
qryPass.Close
Set qryPass = Nothing
Set qryLocal = db.CreateQueryDef(""
qryLocal.SQL = strSQL
Set rs = qryLocal.OpenRecordset()
introw = 1
For intcol = 1 To rs.Fields.count
objXL.ActiveSheet.Cells(introw, intcol).Value = rs.Fields.Item(intcol - 1).Name
Next intcol
introw = introw + 1
Do While Not rs.EOF
For intcol = 1 To rs.Fields.count
objXL.ActiveSheet.Cells(introw, intcol).Value = rs.Fields.Item(intcol - 1).Value
Next intcol
rs.MoveNext
Loop
Set objXL = Nothing
Set rs = Nothing
Set qryPass = Nothing
Set db = Nothing
End Function
2. Combining the SQL portion of my saved query with the Criteria set by the user to Make a table (make-table query) and then using the TransferSpreadsheet command. This works...! The first time, but bombs after 3-4 iterations of the same process! The Make-Table query runs fine; its the TransferSpreadsheet that crashes almost all the time!
Public Function procMSExpQry(strQry As String, strCriteria As String) As Boolean
Dim db As Database
Dim qryPass As QueryDef
Dim rs As Recordset
Dim strSaveAs As String
Dim strSQL As String
Dim objXL As New Excel.Application
DoCmd.SetWarnings False
strSaveAs = "c:\my documents\" & strQry & "_" & Format(Date, "yyyy_mm_dd"
& ".xls"
Set db = CurrentDb()
Set qryPass = db.QueryDefs(strQry)
strSQL = qryPass.SQL
qryPass.Close
Set qryPass = Nothing
strSQL = funcFixSQL(strSQL, strCriteria)
strSQL = Left(strSQL, InStr(1, strSQL, "FROM"
- 1) & " INTO tblDummy " & Right(strSQL, Len(strSQL) - InStr(1, strSQL, "FROM"
+ 1)
DoCmd.RunSQL strSQL
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, "tblDummy", strSaveAs
db.Close
Set db = Nothing
DoCmd.SetWarnings True
End Function
In both instances, I've notice that Method#2 is faster and cleaner, but it crashes Access more than method 1! Of the two main tables I query/filter, the maximum record count is about 1000!!! I can't understand why this is happening. If there is someone that can help me, I'd appreicate it. I'd eventually like to open the Excel spreadsheet and perform formatting tasks to the spreadsheet!
Thank you in advance for any assistance/suggestions!
Lawrence
1. Using a recordset and sending the data to an Excel object; but this method is SLOW and I've tried to optimize the method as best I could. Below is the function that performs the operation.
Public Function procExpQry(strQry As String, strCriteria As String) As Boolean
Dim db As Database
Dim qryPass As QueryDef
Dim qryLocal As QueryDef
Dim rs As Recordset
Dim strSaveAs As String
Dim strSQL As String
Dim introwas Integer
Dim intcol As Integer
Dim objXL As New Excel.Application
Set objXL = CreateObject("Excel.Application"
objXL.Workbooks.Add
Set db = CurrentDb()
Set qryPass = db.QueryDefs(strQry)
strSQL = qryPass.SQL
strSQL = fucnRefineSQL(strSQL) 'will change to proper SQL
qryPass.Close
Set qryPass = Nothing
Set qryLocal = db.CreateQueryDef(""
qryLocal.SQL = strSQL
Set rs = qryLocal.OpenRecordset()
introw = 1
For intcol = 1 To rs.Fields.count
objXL.ActiveSheet.Cells(introw, intcol).Value = rs.Fields.Item(intcol - 1).Name
Next intcol
introw = introw + 1
Do While Not rs.EOF
For intcol = 1 To rs.Fields.count
objXL.ActiveSheet.Cells(introw, intcol).Value = rs.Fields.Item(intcol - 1).Value
Next intcol
rs.MoveNext
Loop
Set objXL = Nothing
Set rs = Nothing
Set qryPass = Nothing
Set db = Nothing
End Function
2. Combining the SQL portion of my saved query with the Criteria set by the user to Make a table (make-table query) and then using the TransferSpreadsheet command. This works...! The first time, but bombs after 3-4 iterations of the same process! The Make-Table query runs fine; its the TransferSpreadsheet that crashes almost all the time!
Public Function procMSExpQry(strQry As String, strCriteria As String) As Boolean
Dim db As Database
Dim qryPass As QueryDef
Dim rs As Recordset
Dim strSaveAs As String
Dim strSQL As String
Dim objXL As New Excel.Application
DoCmd.SetWarnings False
strSaveAs = "c:\my documents\" & strQry & "_" & Format(Date, "yyyy_mm_dd"
Set db = CurrentDb()
Set qryPass = db.QueryDefs(strQry)
strSQL = qryPass.SQL
qryPass.Close
Set qryPass = Nothing
strSQL = funcFixSQL(strSQL, strCriteria)
strSQL = Left(strSQL, InStr(1, strSQL, "FROM"
DoCmd.RunSQL strSQL
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel97, "tblDummy", strSaveAs
db.Close
Set db = Nothing
DoCmd.SetWarnings True
End Function
In both instances, I've notice that Method#2 is faster and cleaner, but it crashes Access more than method 1! Of the two main tables I query/filter, the maximum record count is about 1000!!! I can't understand why this is happening. If there is someone that can help me, I'd appreicate it. I'd eventually like to open the Excel spreadsheet and perform formatting tasks to the spreadsheet!
Thank you in advance for any assistance/suggestions!
Lawrence