Hi I do that below before Wend, though I did not paste it. I have pasted the whole code down. The issue is that, the excel sheet remains empty except the Heading. Another thing that I noticed is that if I give a specific badge_no instead of rsemployeedetails!badge_no in MySQL, access transfers data to Excel. It does not happen with a variable.
txtFromDate.SetFocus
dtFromDate = CDate(txtFromDate.Text)
txtToDate.SetFocus
dtToDate = CDate(txtToDate.Text)
Private Sub ManagerReport()
i = 1
j = 0
Set rsEmployeeDetails = New ADODB.Recordset
Set rsManagerReport = New ADODB.Recordset
rsEmployeeDetails.Open "select * from tblEmployeeDetails WHERE Designation='OP'", con, adOpenKeyset, adLockReadOnly
If rsEmployeeDetails.RecordCount > 0 Then
Form_ManagerForm.Repaint
Me.ProgressBar1.Max = rsEmployeeDetails.RecordCount
On Error Resume Next
Set ExcelApp = GetObject(, "Excel.Application")
If Err.Number <> 0 Then
Set ExcelApp = CreateObject("Excel.Application")
End If
Set ExcelWorkBook = ExcelApp.Workbooks.Add
Set ExcelWorkSheet = ExcelWorkBook.Sheets(1)
ExcelWorkSheet.Rows(1).Font.Bold = True
ExcelWorkSheet.Cells(i, 1).Value = "Badge No" 'Column A
ExcelWorkSheet.Cells(i, 2).Value = "Name" 'Column B
ExcelWorkSheet.Cells(i, 3).Value = "PO Number" 'Column C
ExcelWorkSheet.Cells(i, 4).Value = "Order Numbers" 'Column D
While Not rsEmployeeDetails.EOF
i = i + 2
MySQL = "SELECT * from tblOrderEntry where Reviewed_By= '" & rsEmployeeDetails!badge_no & "' AND Review_Date BETWEEN " & "#" & dtFromDate & "# AND #" & dtToDate & "# ORDER BY sno"
MsgBox MySQL
rsManagerReport.Open MySQL, con, adOpenKeyset, adLockPessimistic
' rsManagerReport.MoveLast
' MsgBox rsManagerReport.RecordCount & " " & rsEmployeeDetails!badge_no
rsManagerReport.MoveFirst
If rsManagerReport.RecordCount > 0 Then
With rsManagerReport
Do While Not .EOF
ExcelWorkSheet.Cells(i, 1).Value = rsEmployeeDetails!badge_no
ExcelWorkSheet.Cells(i, 2).Value = rsEmployeeDetails!Name
ExcelWorkSheet.Cells(i, 3).Value = !PO_Number
i = ExportOrderNumbers(rsManagerReport, i, 4) 'Function to print order numbers in column 6, one in each row.
'The function would return the row number from which next set of data would
'be printed.
.MoveNext
Loop
End With
rsManagerReport.Close
' ExcelWorkSheet.Rows(i).Font.Bold = True
ExcelWorkSheet.Rows(i).Font.Bold = True
ExcelWorkSheet.Cells(i, 1).Value = "Total"
ExcelWorkSheet.Cells(i, 2).Value = "Orders : " & rsManagerReport!Total
rsManagerReport.Close
rsManagerReport.Open "SELECT DISTINCT po_number FROM tblOrderEntry where Reviewed_By = '" & rsEmployeeDetails!badge_no & "' AND Review_Date BETWEEN " & "#" & dtFromDate & "# AND #" & dtToDate & "#", con, adOpenKeyset, adLockPessimistic
rsManagerReport.MoveLast
ExcelWorkSheet.Cells(i, 3).Value = "PO : " & rsManagerReport.RecordCount
rsManagerReport.Close
End If
ProgressBar1.Value = ProgressBar1.Value + 1
rsEmployeeDetails.MoveNext
Wend
ExcelApp.Visible = True
ExcelWorkSheet.Visible = xlSheetVisible
Set ExcelWorkSheet = Nothing
Set ExcelWorkBook = Nothing
Set ExcelApp = Nothing
MsgBox "Finished exporting to Excel.", vbInformation, "Export"
txtFromDate.SetFocus
txtFromDate.Text = ""
txtToDate.SetFocus
txtToDate.Text = ""
ProgressBar1.Value = 0
Else
MsgBox "No records found", vbInformation, "Order Entry Report"
End If
Set rsManagerReport = Nothing
end sub