I have a procedure below to export data to an excel worksheet.
If i run the result of the debug.Print strSQL in the query designer , i get all the records but the copyfromrecordset returns not all the records.
e.g it may return like 10 rows yet the rows are 100.
Here is the code
CODE
Private Sub cmdSummary_Click()
Dim rst As DAO.Recordset
Dim db As DAO.Database
Dim objApp As Excel.Application
Dim objBook As Excel.Workbook
Dim objSheet As Excel.Worksheet
Dim strSQL As String
Dim i As Integer
On Error GoTo ErrorHandle
If Me.lstPeriods.ItemsSelected.Count = 0 Then
MsgBox " No Period Selected. Select a period to proceed", vbExclamation
Exit Sub
End If
strSQL = "SELECT qryTotalPaid.Contractor," & _
" qryTotalPaid.SectionNumber, " & _
" qryTotalPaid.SectionLength," & _
" qryTotalPaid.RoadName," & _
" qryTotalPaid.PackageRef, " & _
" qryTotalPaid.PlannedAmount," & _
" qryTotalPaid.ActualAmount," & _
"Format( Nz([qryTotalPaid]![ActualAmount],0)/Nz([qryTotalPaid]![PlannedAmount],0),'Percent') AS [%Paid]" & _
" From qryTotalPaid " & _
" Where PeriodID = " & Me.lstPeriods.Column(0)
Debug.Print strSQL
Set db = CurrentDb()
Set rst = db.OpenRecordset(strSQL, dbOpenSnapshot)
If Not rst.EOF Then
''''start excel
Set objApp = New Excel.Application
objApp.Visible = True
Set objBook = objApp.Workbooks.Add
Set objSheet = objBook.Worksheets("Sheet1")
objApp.ScreenUpdating = False
objSheet.Select
objSheet.Range("A2").CopyFromRecordset rst
For i = 1 To rst.Fields.Count
objSheet.Cells(1, i).Value = rst.Fields(i - 1).Name
Next i '''' do some formating
objSheet.UsedRange.EntireColumn.AutoFit
With objApp
.Range("F:G").Style = "Comma [0]"
.Range("1:1").Font.Bold = True
.Range("D:D").AutoFilter
End With
Else
MsgBox " No Record to Export", vbInformation
GoTo myExit
End If
myExit:
On Error Resume Next
objApp.ScreenUpdating = True
rst.Close
Set rst = Nothing
Set db = Nothing
Set objSheet = Nothing
Set objBook = Nothing
If Not objApp Is Nothing Then
Set objApp = Nothing
End If
Exit Sub
ErrorHandle:
MsgBox "Error:" & Err.Description & " ErrorNumbr:" & Err.number
Resume myExit
End Sub
Dim rst As DAO.Recordset
Dim db As DAO.Database
Dim objApp As Excel.Application
Dim objBook As Excel.Workbook
Dim objSheet As Excel.Worksheet
Dim strSQL As String
Dim i As Integer
On Error GoTo ErrorHandle
If Me.lstPeriods.ItemsSelected.Count = 0 Then
MsgBox " No Period Selected. Select a period to proceed", vbExclamation
Exit Sub
End If
strSQL = "SELECT qryTotalPaid.Contractor," & _
" qryTotalPaid.SectionNumber, " & _
" qryTotalPaid.SectionLength," & _
" qryTotalPaid.RoadName," & _
" qryTotalPaid.PackageRef, " & _
" qryTotalPaid.PlannedAmount," & _
" qryTotalPaid.ActualAmount," & _
"Format( Nz([qryTotalPaid]![ActualAmount],0)/Nz([qryTotalPaid]![PlannedAmount],0),'Percent') AS [%Paid]" & _
" From qryTotalPaid " & _
" Where PeriodID = " & Me.lstPeriods.Column(0)
Debug.Print strSQL
Set db = CurrentDb()
Set rst = db.OpenRecordset(strSQL, dbOpenSnapshot)
If Not rst.EOF Then
''''start excel
Set objApp = New Excel.Application
objApp.Visible = True
Set objBook = objApp.Workbooks.Add
Set objSheet = objBook.Worksheets("Sheet1")
objApp.ScreenUpdating = False
objSheet.Select
objSheet.Range("A2").CopyFromRecordset rst
For i = 1 To rst.Fields.Count
objSheet.Cells(1, i).Value = rst.Fields(i - 1).Name
Next i '''' do some formating
objSheet.UsedRange.EntireColumn.AutoFit
With objApp
.Range("F:G").Style = "Comma [0]"
.Range("1:1").Font.Bold = True
.Range("D:D").AutoFilter
End With
Else
MsgBox " No Record to Export", vbInformation
GoTo myExit
End If
myExit:
On Error Resume Next
objApp.ScreenUpdating = True
rst.Close
Set rst = Nothing
Set db = Nothing
Set objSheet = Nothing
Set objBook = Nothing
If Not objApp Is Nothing Then
Set objApp = Nothing
End If
Exit Sub
ErrorHandle:
MsgBox "Error:" & Err.Description & " ErrorNumbr:" & Err.number
Resume myExit
End Sub
what is the problem?
Ronald
Failed to edit the heading but i mean " Does not copy all records"
