R
ryguy7272
I am trying to export the results of a query, with more than 65,536 rows, to
an Excel sheet. I found this code on this DG, from a while back. Yesterday
it (sort of) worked for me, but today, after changing a few fields in my
query, it is not working at all. It stops at row 65,536, and this is the
line that errors out:
MinID = rst.Fields("Employee")
Entire Macro:
Option Compare Database
Public MinID As Long
Sub Export_Query()
Dim db As DAO.Database
Dim rst As DAO.Recordset
MinID = 0
TotalRec = DCount("[Employee]", "MyQuery")
If TotalRec = 0 Then
MsgBox "No records found", vbExclamation, "Export Aborted"
Exit Sub
End If
SheetCount = TotalRec \ 65536
If TotalRec / 65536 > SheetCount Then
SheetCount = SheetCount + 1
End If
Set db = CurrentDb
For i = 1 To SheetCount
strSQL = "SELECT TOP 65536 * FROM MyQuery"
strSQL = strSQL & " ORDER BY Employee"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
"MyQuery", "C:\MyWorkBook.xls", False, "Export" & i
Set rst = db.OpenRecordset(strSQL)
rst.MoveLast
MinID = rst.Fields("Employee")
rst.Close
Next
Set rst = Nothing
Set db = Nothing
End Sub
Function GetMinID()
GetMinID = MinID
End Function
My query fields are: Employee, Unit Price, Order Date, Company Name, Item,
and Units.
I spent a while fiddling with it this AM but couldn’t get it working; I am
probably missing something simple, just don’t know what.
If anyone could offer some help I would greatly appreciate it.
Regards,
Ryan---
an Excel sheet. I found this code on this DG, from a while back. Yesterday
it (sort of) worked for me, but today, after changing a few fields in my
query, it is not working at all. It stops at row 65,536, and this is the
line that errors out:
MinID = rst.Fields("Employee")
Entire Macro:
Option Compare Database
Public MinID As Long
Sub Export_Query()
Dim db As DAO.Database
Dim rst As DAO.Recordset
MinID = 0
TotalRec = DCount("[Employee]", "MyQuery")
If TotalRec = 0 Then
MsgBox "No records found", vbExclamation, "Export Aborted"
Exit Sub
End If
SheetCount = TotalRec \ 65536
If TotalRec / 65536 > SheetCount Then
SheetCount = SheetCount + 1
End If
Set db = CurrentDb
For i = 1 To SheetCount
strSQL = "SELECT TOP 65536 * FROM MyQuery"
strSQL = strSQL & " ORDER BY Employee"
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _
"MyQuery", "C:\MyWorkBook.xls", False, "Export" & i
Set rst = db.OpenRecordset(strSQL)
rst.MoveLast
MinID = rst.Fields("Employee")
rst.Close
Next
Set rst = Nothing
Set db = Nothing
End Sub
Function GetMinID()
GetMinID = MinID
End Function
My query fields are: Employee, Unit Price, Order Date, Company Name, Item,
and Units.
I spent a while fiddling with it this AM but couldn’t get it working; I am
probably missing something simple, just don’t know what.
If anyone could offer some help I would greatly appreciate it.
Regards,
Ryan---