Code:
Dim xLWB As Object
Dim apXL As Excel.Application
Dim fOpen As String, pOpen As String, fSave As String, pSave As String, mSource As String, mP As String, mpY As String
Dim rs As DAO.Recordset, rs2 As DAO.Recordset
Dim mStart As Date, mEnd As Date
Dim PaidYes As Currency, PaidNo As Currency, mTotal As Currency
Dim tQty As Long, rsQty As Long, SheetQty As Long, AddQty As Long
mSource = Me.cboReportOptions
mStart = Forms!frmMainMenu!cboStartDate
mStart = Forms!frmMainMenu!cboEndDate
If IsNull(PaidYes = DSum("Price", "tblInvoices", "[Source] = '" & mSource & "' And [InvoiceDate] Between #" & mStart & "# And #" & mEnd & "# And [Paid] = Yes")) Then
PaidYes = "00"
Else
PaidYes = DSum("Price", "tblInvoices", "[Source] = '" & mSource & "' And [InvoiceDate] Between #" & mStart & "# And #" & mEnd & "# And [Paid] = Yes")
mpY = "Yes"
End If
If IsNull(PaidNo = DSum("Price", "tblInvoices", "[Source] = '" & mSource & "' And [InvoiceDate] Between #" & mStart & "# And #" & mEnd & "# And [Paid] = No")) Then
PaidNo = "00"
Else
PaidNo = DSum("Price", "tblInvoices", "[Source] = '" & mSource & "' And [InvoiceDate] Between #" & mStart & "# And #" & mEnd & "# And [Paid] = No")
mP = "No"
End If
pOpen = "T:\MY FOLDER\XL Files\REPORTS\WCBYS\"
fOpen = "REPORT TEMPLATE" & ".xlsx"
pSave = "T:\MY FOLDER\XL Files\REPORTS\WCBYS\"
fSave = "From " & Format(Me.cboStartDate, "dd-mm-yy") & " To " & Format(Me.cboEndDate, "dd-mm-yy") & ".xlsx"
Set apXL = CreateObject("Excel.Application")
Set xLWB = apXL.Workbooks.Open(pOpen & fOpen)
apXL.ActiveWorkbook.SaveAs pSave & fSave
apXL.Workbooks.Open pSave & fSave, True, False
apXL.Visible = False
Set rs = CurrentDb.OpenRecordset("SELECT tblInvoices.InvoiceDate, tblInvoices.InvoiceNumber, tblInvoices.Customer, tblInvoices.Item, tblInvoices.Ref, tblInvoices.Paid, tblInvoices.PaidDate, " _
& "tblInvoices.Price From tblInvoices " _
& "WHERE tblInvoices.InvoiceDate Between #" & Forms!frmMainMenu!cboStartDate & "# And #" & Forms!frmMainMenu!cboEndDate & "#" & " And Source = '" & mSource & "'")
mTotal = DSum("Price", "tblInvoices", "[InvoiceDate] Between #" & Forms!frmMainMenu!cboStartDate & "# And #" & Forms!frmMainMenu!cboEndDate & "#" & " And Source = '" & mSource & "'") ' new to test
rsQty = rs.RecordCount ' new to test
tQty = DCount("Item", "tblInvoices", "[InvoiceDate] Between #" & Forms!frmMainMenu!cboStartDate & "# And #" & Forms!frmMainMenu!cboEndDate & "#" & " And Source = '" & mSource & "'") ' new to test
SheetQty = "22" ' new to test
AddQty = tQty - rs.RecordCount ' new to test
If tQty >= rs.RecordCount Then ' new to test
AddQty = tQty - rs.RecordCount ' new to test
Else ' new to test
AddQty = "0" ' new to test
End If ' new to test
With xLWB
.Worksheets(1).Cells(12 + AddQty, 1).EntireRow.Insert ' new to test
.Worksheets(1).Cells(12, 1).CopyFromRecordset rs
.Worksheets(1).Cells(rs.RecordCount + 1, 8) = mTotal
.Worksheets(1).Cells(rs.RecordCount + 2, 8) = mpY
.Worksheets(1).Cells(rs.RecordCount + 3, 8) = mP
.Worksheets(1).Cells.EntireColumn.AutoFit
.Worksheets(1).Cells.EntireColumn.HorizontalAlignment = xlLeft
.Worksheets(1).Range("A & rs.RecordCount + 4:" & rs.RecordCount & 9).Borders(xlEdgeBottom).LineStyle = xlContinuous
.Save
End With
xLWB.Close
apXL.Quit
Set apXL = Nothing