My code so far is:
Dim strSQL As String
Dim DB As Database
Set DB = CurrentDb()
Dim RS As Recordset
Dim objXL As Excel.Application
Dim objWS As Excel.worksheet
Dim fld As Field
Dim intCol As Integer
Dim intRow As Integer
strSQL = "SELECT ...blah blah blah..."
Set RS = DB.OpenRecordset(strSQL) 'create recordset
'create and name worksheet
Set objWS = objXL.Worksheets.Add 'opens a new sheet in the workbook
objWS.Name = "BLAH" 'names the new sheet
'copy to worksheet
'first the field names
For intCol = 0 To RS.Fields.Count - 1
Set fld = RS.Fields(intCol)
objWS.Cells(1, intCol + 1) = fld.Name
Next intCol
'now the actual data
intRow = 2
Do Until RS.EOF
For intCol = 0 To RS.Fields.Count - 1
objWS.Cells(intRow, intCol + 1) = RS.Fields(intCol).Value
Next intCol
RS.MoveNext
intRow = intRow + 1
Loop
Thanks in advance for any advice!
That might get a little tricky when you get to your second question,
however, as I don't believe you can use CopyFromRecordset to copy only part
of a recordset...it's all or nothing. You might want to look at splitting
the recordset up at the source by using a SELECT TOP query or filtering the
recordset in whatever way is logical for your app.
Rob
Rob
intSheetNumber = 1
Do Until RS.EOF
'adds a new sheet and name it
Set objWS = objXL.Worksheets.Add
strSheetName = "GPS" & intSheetNumber
objWS.Name = strSheetName
'add the field names
For intCol = 0 To RS.Fields.Count - 1
Set fld = RS.Fields(intCol)
objWS.Cells(1, intCol + 1) = fld.Name
Next intCol
'this will copy the data into the current page
'luckily, it only copies however much can fit on the page, and then sits
at the next record!
objWS.Range(objWS.Cells(1, 1), objWS.Cells(1,
RS.Fields.Count)).Font.Bold = True 'this just makes the field names bold!
objWS.Range("A2").CopyFromRecordset RS
'set the next sheet number
intSheetNumber = intSheetNumber + 1
Loop
Thanks again for the help!
Jey
Rob
Public Sub ExportToWorksheet(objXL As Excel.Application, RS As Recordset,
strName As String)
'takes an open Excel workbook, a populated recordset, and a name stub
'exports the recordset to one or more new (named and numbered) worksheets
On Error GoTo Err_Handler
Dim intSheetNumber As Integer
Dim objWS As Excel.worksheet
Dim strSheetName As String
Dim fld As Field
Dim intCol As Integer
RS.MoveLast
If RS.RecordCount > 65000 Then
RS.MoveFirst
intSheetNumber = 1
Do Until RS.EOF
'adds a new sheet and name it
Set objWS = objXL.Worksheets.Add
strSheetName = strName & intSheetNumber
objWS.Name = strSheetName
'add the field names
For intCol = 0 To RS.Fields.Count - 1
Set fld = RS.Fields(intCol)
objWS.Cells(1, intCol + 1) = fld.Name
Next intCol
'this will copy the data into the current page
'luckily, it only copies however much can fit on the page, and then
sits at the next record!
objWS.Range(objWS.Cells(1, 1), objWS.Cells(1,
RS.Fields.Count)).Font.Bold = True
objWS.Range("A2").CopyFromRecordset RS
'set the next sheet number
intSheetNumber = intSheetNumber + 1
Loop
Else
RS.MoveFirst
'create and name worksheet
Set objWS = objXL.Worksheets.Add
objWS.Name = strName
'copy to worksheet
'first the field names
For intCol = 0 To RS.Fields.Count - 1
Set fld = RS.Fields(intCol)
objWS.Cells(1, intCol + 1) = fld.Name
Next intCol
'now the actual data
objWS.Range(objWS.Cells(1, 1), objWS.Cells(1,
RS.Fields.Count)).Font.Bold = True
objWS.Range("A2").CopyFromRecordset RS
End If
Err_Handler_Exit:
Exit Sub
Err_Handler:
MsgBox Err.Number & " - " & Err.Description & " - Sub ExportToWorksheet()"
Resume Err_Handler_Exit
End Sub