Dim db As DAO.Database
Dim rst As DAO.Recordset
Dim rstenq As DAO.Recordset
Dim rstquo As DAO.Recordset
Dim rstqlivat As DAO.Recordset
Dim rstqlinon As DAO.Recordset
Dim obExcel As Object
Dim intStart As Integer
Dim intCol As Integer
Dim irow As Integer
Set db = CurrentDb()
Set rst = db.OpenRecordset("SELECT * FROM tblCompany WHERE c_id = " & Me.c_id)
Set rstenq = db.OpenRecordset("SELECT * FROM tblEnquiries WHERE e_id = " & Me.e_id)
Set rstquo = db.OpenRecordset("SELECT * FROM tblQuote WHERE q_id = " & Me.q_id)
[COLOR="Red"]Set rstqlivat = db.OpenRecordset("SELECT tblQuoteLineItems.*, tblDrpSuppliers.supp_name FROM tblDrpSuppliers INNER JOIN tblQuoteLineItems ON tblDrpSuppliers.ID = tblQuoteLineItems.qli_supplier WHERE q_id = " & Me.q_id & " And qli_vatable = " & "-1 ORDER BY qli_order")
Set rstqlinon = db.OpenRecordset("SELECT tblQuoteLineItems.*, tblDrpSuppliers.supp_name FROM tblDrpSuppliers INNER JOIN tblQuoteLineItems ON tblDrpSuppliers.ID = tblQuoteLineItems.qli_supplier WHERE q_id = " & Me.q_id & " And qli_vatable = " & "0 ORDER BY qli_order")[/COLOR]
Set obExcel = CreateObject("Excel.Application")
With obExcel
.workbooks.Add
'Incase sheet does not exist
On Error Resume Next
.Sheets("Sheet2").Delete
.Sheets("Sheet3").Delete
With obExcel
If .activesheet.Name = "Sheet1" Then
.activesheet.Name = strSheet_Name
'Add column headers******************************************************
.activesheet.cells(1, 1) = Me.cmbPerson.Column(2)
.activesheet.cells(2, 1) = Me.c_name
.activesheet.cells(3, 1) = rst!c_add1
.activesheet.cells(4, 1) = rst!c_add2
.activesheet.cells(5, 1) = rst!c_add3
.activesheet.cells(6, 1) = rst!c_add4
.activesheet.cells(7, 1) = rst!c_county
.activesheet.cells(8, 1) = rst!c_postcode
.activesheet.cells(10, 1) = "Quote Number:" & " " & rstquo!q_creator & rstquo!q_id
.activesheet.cells(11, 1) = "Quote Name:" & " " & rstquo!q_name
.activesheet.cells(13, 1).Font.Bold = True
.activesheet.cells(13, 1) = "VATable items"
.activesheet.cells(14, 1).Font.Bold = True
.activesheet.cells(14, 1) = "Item"
.activesheet.cells(14, 2).Font.Bold = True
.activesheet.cells(14, 2) = "Qty in unit"
.activesheet.cells(14, 3).Font.Bold = True
.activesheet.cells(14, 3) = "No. of units"
.activesheet.cells(14, 4).Font.Bold = True
.activesheet.cells(14, 4) = "Sell"
.activesheet.cells(14, 5).Font.Bold = True
.activesheet.cells(14, 5) = "Cost"
.activesheet.cells(14, 6).Font.Bold = True
.activesheet.cells(14, 6) = "Supplier"
.activesheet.cells(14, 7).Font.Bold = True
.activesheet.cells(14, 7) = "Profit"
'Move last and first, cycle through records******************************************************
rstqlivat.MoveLast
rstqlivat.MoveFirst
irow = 15
For i = 1 To rstqlivat.RecordCount
'Add data ******************************************************
.activesheet.cells(irow, 1) = rstqlivat!qli_line_item
.activesheet.cells(irow, 2) = rstqlivat!qli_per
.activesheet.cells(irow, 3) = rstqlivat!qli_quantity
.activesheet.cells(irow, 4).numberformat = "£#,##0.00"
.activesheet.cells(irow, 4) = rstqlivat!qli_sell
.activesheet.cells(irow, 5).numberformat = "£#,##0.00"
.activesheet.cells(irow, 5) = rstqlivat!qli_cost
.activesheet.cells(irow, 6) = rstqlivat!Supp_name
.activesheet.cells(irow, 7).numberformat = "£#,##0.00"
.activesheet.cells(irow, 7).Font.Italic = True
.activesheet.cells(irow, 7) = rstqlivat!qli_profit
irow = irow + 1
rstqlivat.MoveNext
Next i
irow = irow + 3
.activesheet.cells(irow - 2, 1).Font.Bold = True
.activesheet.cells(irow - 2, 1) = "Non VATable items"
.activesheet.cells(irow - 1, 1).Font.Bold = True
.activesheet.cells(irow - 1, 1) = "Item"
.activesheet.cells(irow - 1, 2).Font.Bold = True
.activesheet.cells(irow - 1, 2) = "Qty in unit"
.activesheet.cells(irow - 1, 3).Font.Bold = True
.activesheet.cells(irow - 1, 3) = "No. of units"
.activesheet.cells(irow - 1, 4).Font.Bold = True
.activesheet.cells(irow - 1, 4) = "Sell"
.activesheet.cells(irow - 1, 5).Font.Bold = True
.activesheet.cells(irow - 1, 5) = "Cost"
.activesheet.cells(irow - 1, 6).Font.Bold = True
.activesheet.cells(irow - 1, 6) = "Supplier"
.activesheet.cells(irow - 1, 7).Font.Bold = True
.activesheet.cells(irow - 1, 7) = "Profit"
rstqlinon.MoveLast
rstqlinon.MoveFirst
For i = 1 To rstqlinon.RecordCount
.activesheet.cells(irow, 1) = rstqlinon!qli_line_item
.activesheet.cells(irow, 2) = rstqlinon!qli_per
.activesheet.cells(irow, 3) = rstqlinon!qli_quantity
.activesheet.cells(irow, 4).numberformat = "£#,##0.00"
.activesheet.cells(irow, 4) = rstqlinon!qli_sell
.activesheet.cells(irow, 5).numberformat = "£#,##0.00"
.activesheet.cells(irow, 5) = rstqlinon!qli_cost
.activesheet.cells(irow, 6) = rstqlinon!Supp_name
.activesheet.cells(irow, 7).numberformat = "£#,##0.00"
.activesheet.cells(irow, 7).Font.Italic = True
.activesheet.cells(irow, 7) = rstqlinon!qli_profit
irow = irow + 1
rstqlinon.MoveNext
Next i
'Add totals **********************************************
irow = irow + 1
.activesheet.cells(irow, 1).Font.Bold = True
.activesheet.cells(irow, 1) = "Subtotal"
.activesheet.cells(irow, 4).numberformat = "£#,##0.00"
.activesheet.cells(irow, 4) = Me.txtq_sell_subtotal
.activesheet.cells(irow, 5).numberformat = "£#,##0.00"
.activesheet.cells(irow, 5) = Me.txtq_cost_subtotal
.activesheet.cells(irow, 7).numberformat = "£#,##0.00"
.activesheet.cells(irow, 7).Font.Italic = True
.activesheet.cells(irow, 7) = Me.txtq_profit
irow = irow + 1
.activesheet.cells(irow, 1).Font.Bold = True
.activesheet.cells(irow, 1) = "VAT"
.activesheet.cells(irow, 4).numberformat = "£#,##0.00"
.activesheet.cells(irow, 4) = Me.txtq_sell_vat
.activesheet.cells(irow, 5).numberformat = "£#,##0.00"
.activesheet.cells(irow, 5) = Me.txtq_cost_vat
irow = irow + 1
.activesheet.cells(irow, 1).Font.Bold = True
.activesheet.cells(irow, 1) = "Total"
.activesheet.cells(irow, 4).Font.Bold = True
.activesheet.cells(irow, 4).numberformat = "£#,##0.00"
.activesheet.cells(irow, 4) = Me.txtq_sell_total
.activesheet.cells(irow, 5).Font.Bold = True
.activesheet.cells(irow, 5).numberformat = "£#,##0.00"
.activesheet.cells(irow, 5) = Me.txtq_cost_total
'change size of columns in excel *******************************
.columns("B:G").autofit
.columns("A:A").ColumnWidth = 50
End If
End With
.Sheets(1).Select
'Make workbook visible
.Visible = True
End With
'Close recordsets and database******************************************************
Set db = Nothing
Set obExcel = Nothing
Set rst = Nothing
Set rstenq = Nothing
Set rstquo = Nothing
Set rstqlivat = Nothing
Set rstqlinon = Nothing
Set rstSuppliers = Nothing
Set obExcel = Nothing