I added:
Code:
If rsClone.RecordCount > 0 Then
rsClone.MoveFirst
End If
That did it.
This is what I have so far:
Code:
Private Sub cmdExportToExcel_Click()
If Me.Dirty Then Me.Dirty = False
Dim rsClone As DAO.Recordset
Set rsClone = Me.[Packaging information charts - Copy Of subform].Form.RecordsetClone
If rsClone.RecordCount > 0 Then
rsClone.MoveFirst
End If
If rsClone.EOF Then
MsgBox "No records found."
Set rsClone = Nothing
Exit Sub
End If
Dim xlApp As Object
Set xlApp = CreateObject("Excel.Application")
With xlApp
.Visible = True
.Workbooks.Add
.Sheets("Sheet1").Select
.ActiveSheet.Range("A2").CopyFromRecordset rsClone
For i = 1 To rsClone.Fields.Count
xlApp.ActiveSheet.Cells(1, i).Value = rsClone.Fields(i - 1).Name
Next i
xlApp.Cells.EntireColumn.AutoFit
End With
End Sub
What do you mean by this:
It is also good practice to destroy what you create and close what you open. You do this to prevent memory leaks as well as ensuring that your are not using old data/objects.
Should I add the last bit of code that you have? What does it do?
Code:
'tidy up
rsClone.Close
Set rsClone = Nothing
'It would be proper to save and close the spreadsheet.
'For demonstration purposes we will leave the .Visible = true
'and the file open
Set xlApp = Nothing