With thanks to previous help on here for creating the code below I am able to export quotes out of our access CRM.
Currently the staff here need to then move items around to make it fit on the page properly and so I have been looking for a way to simplify this.
I see 2 ways this could work:
1 - use a template and when creating a quote bring up a save as box. (How would I reference the template and open the save as dialog to a different folder to the one the template is located in?)
2 - As current but with entering formatting into the vba code above (how can I set the format of the excel page e.g. narrow margins and move the image to the top right corner - currently in a cell near but is offset )
Is there a suggested way for this to work as per the above or alternative method and some guidance to get me on the right lines?
The text in column A can sometimes be quite long and would need to appear on multiple lines after a certain number of characters - is there a way to do this?
Thanks very much in advance
Code:
Private Sub btnQuote_Click()
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
Dim strDBPath As String
Dim strPictureName As String
strDBPath = CurrentProject.Path & "\"
strPictureName = "logo.jpg"
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)
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_item_category")
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")
Set obExcel = CreateObject("Excel.Application")
MsgBox "logo = " & strDBPath & strPictureName
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
.cells.Font.Name = "Calibri"
.cells.Font.Size = 11
.columns("A").ColumnWidth = 200
'Add column headers******************************************************
.activesheet.cells(1, 2).Select
.activesheet.pictures.Insert (strDBPath & strPictureName)
.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) = "Enquiry:" & " " & rstenq!e_name
.activesheet.cells(11, 1) = "Quote:" & " " & 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) = "Cost"
'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
iRow = iRow + 1
rstqlivat.MoveNext
Next i
iRow = iRow + 2
.activesheet.cells(iRow - 1, 1).Font.Bold = True
.activesheet.cells(iRow - 1, 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) = "Cost"
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
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
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
iRow = iRow + 1
.activesheet.cells(iRow, 1).Font.Bold = True
.activesheet.cells(iRow, 1) = "Cost"
.activesheet.cells(iRow, 4).Font.Bold = True
.activesheet.cells(iRow, 4).numberformat = "£#,##0.00"
.activesheet.cells(iRow, 4) = Me.txtq_sell_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
End Sub
Currently the staff here need to then move items around to make it fit on the page properly and so I have been looking for a way to simplify this.
I see 2 ways this could work:
1 - use a template and when creating a quote bring up a save as box. (How would I reference the template and open the save as dialog to a different folder to the one the template is located in?)
2 - As current but with entering formatting into the vba code above (how can I set the format of the excel page e.g. narrow margins and move the image to the top right corner - currently in a cell near but is offset )
Is there a suggested way for this to work as per the above or alternative method and some guidance to get me on the right lines?
The text in column A can sometimes be quite long and would need to appear on multiple lines after a certain number of characters - is there a way to do this?
Thanks very much in advance