Need help on using a module to enable Export to excel functionality on all forms
Hi guys
i'm in the midst of improving my application but i need some questions answered.
My application has several queries, each having a form where the user can enter his/her criteria. So i've successfully tested using recordset to export the results to Excel(finally...) BUT this code is within a button's on_click event in an individual form. So if i want to re-use this code for all the forms that require the same functionality, how do i do this? i wouldnt want to copy and paste the same code over all the forms, i would just want to call it when its required.
i understand to an extent how modules would work in this situation but i'm not sure how its implemented. do i just Code the "Formatting part" into a module or class module? i.e.
and then in the form's "export to excel" command button, code the prompt for user to select directory and the copying of recordset to a worksheet in excel, i.e.
is that the right method?
or can i just put the whole line of "export to excel" on_click event into a module then call this module for all the "export to excel" buttons on all my forms?
Hi guys
i'm in the midst of improving my application but i need some questions answered.
My application has several queries, each having a form where the user can enter his/her criteria. So i've successfully tested using recordset to export the results to Excel(finally...) BUT this code is within a button's on_click event in an individual form. So if i want to re-use this code for all the forms that require the same functionality, how do i do this? i wouldnt want to copy and paste the same code over all the forms, i would just want to call it when its required.
i understand to an extent how modules would work in this situation but i'm not sure how its implemented. do i just Code the "Formatting part" into a module or class module? i.e.
Code:
sub FormatExcel() 'formatting cells in excel
With xlSheet
For Each Cell In xlSheet.Range("A1", "S1")
Cell.Font.Size = 10
Cell.Font.Name = "Arial"
Cell.Font.Bold = True
Cell.Interior.Color = rgb(204, 255, 255)
Cell.HorizontalAlignment = xlHAlignCenter
Cell.WrapText = True
Next
.Cells(1, 2).HorizontalAlignment = xlHAlignLeft
.Columns("A:S").HorizontalAlignment = xlHAlignLeft
.Columns("A").ColumnWidth = 10
.Columns("B").ColumnWidth = 24
.Columns("C:D").ColumnWidth = 12
.Columns("E").ColumnWidth = 40
.Columns("F").ColumnWidth = 20
.Columns("G").ColumnWidth = 65
.Columns("H").ColumnWidth = 7
.Columns("I").ColumnWidth = 7
.Columns("J:K").ColumnWidth = 24
.Rows(1).RowHeight = 16
End With
End sub
Code:
'assume all necessary variables have been declared
Set maindb = CurrentDb()
Set mainqdf = maindb.QueryDefs("qryCOSearch")
Set mainRst = mainqdf.OpenRecordset(dbOpenDynaset)
strFile = GetSaveFile_CLT("C:\", "Save this file as", "strDefName") 'dialog window to allow user to select directory to save
'declare excel objects used to store query data
Set xlBook = xlApp.Workbooks.Add
Set xlSheet = xlBook.Worksheets.Add
Call ModuleProcedureName
'deleting all other worksheets except for "Results"
For lngCount = lngMax To 1 Step -1
If xlBook.Worksheets(lngCount).Name <> "Results" Then
xlBook.Worksheets(lngCount).Delete
End If
Next lngCount
'copying the query results from the recordset to the excel file
With xlSheet
.Name = "Results"
.UsedRange.ClearContents
lngMax = mainRst.Fields.Count
For lngCount = lngMax To 1 Step -1
.Cells(1, lngCount).Value = mainRst.Fields(lngCount - 1).Name
Next lngCount
.Range("A2").CopyFromRecordset mainRst
End With
lngMax = xlBook.Worksheets.Count
'deleting all other worksheets except for "Results"
For lngCount = lngMax To 1 Step -1
If xlBook.Worksheets(lngCount).Name <> "Results" Then
xlBook.Worksheets(lngCount).Delete
End If
Next lngCount
xlBook.SaveAs strDefName
MsgBox "Export Successful!", vbInformation
'rest of code is clean up and error handling
is that the right method?
or can i just put the whole line of "export to excel" on_click event into a module then call this module for all the "export to excel" buttons on all my forms?
Last edited: