I have a form with a bunch of combo boxes and a datasheet subform. When the form is loaded the subform displays all the records from a specific table. The user may then use the combo boxes to set any number of conditions that will filter the data in the subform and display records based on those conditions.
I use vba to change the subform's recordsource based on what conditions the user has selected in the form.
What I want to do is export the subform exactly how appears in the main form to Excel. Right now I have it set up so that it determines the recordsource of the subform, creates a query based on the recordsource, then uses the "DoCmd.OuputTo" approach to export it to Excel. The problem is the formatting is much different than the way it is displayed in the subform (labels, order, etc.).
Is there a way to directly export a subform to Excel so that is maintains any changes to the recordsource made from the main form? Here's the code I have so far for reference:
I use vba to change the subform's recordsource based on what conditions the user has selected in the form.
What I want to do is export the subform exactly how appears in the main form to Excel. Right now I have it set up so that it determines the recordsource of the subform, creates a query based on the recordsource, then uses the "DoCmd.OuputTo" approach to export it to Excel. The problem is the formatting is much different than the way it is displayed in the subform (labels, order, etc.).
Is there a way to directly export a subform to Excel so that is maintains any changes to the recordsource made from the main form? Here's the code I have so far for reference:
Code:
'Create Excel Application object
Dim objExcelApp As Excel.Application
Set objExcelApp = CreateObject("Excel.Application")
'Create New Excel Workbook
'All data will be copied into
'this Workbook later
Dim objExcelWrkBk1 As Excel.Workbook
Set objExcelWrkBk1 = objExcelApp.Workbooks.Add()
objExcelApp.Visible = True
'Variables to be used to store
'names of files and forms
Dim strExcelFileName As String
Dim strQryName As String
'Now we must determine the username of
'the person that is logged into
'the computer in order to save the image
'to the correct "Desktop" address.
'This line of code calls the function above.
Dim strUser As String
strUser = fWin2KUserName
'Set Filepath name (Desktop) for temporarily
'created Excel workbooks
Dim strFilePath As String
strFilePath = "C:\Documents and Settings\" & strUser & "\Desktop\"
'Create query based on datasheet subform
Dim db As Database
Dim QryDef As QueryDef
strQryName = "Production Log"
Set db = CurrentDb()
'First delete query if it already exists.
'By setting the error routine to "Resume
'Next", if an error occurs when the code
'tries to delete a query that is not there
'then it will move on.
On Error Resume Next
db.QueryDefs.Delete (strQryName)
On Error GoTo Err_Handler
'Create query
Set QryDef = db.CreateQueryDef(strQryName, Me.SubForm.Form.RecordSource)
'Datasheet Table
strExcelFileName = "Table.xls"
DoCmd.OutputTo acOutputQuery, strQryName, _
acFormatXLS, strFilePath & strExcelFileName, False, , False
'Delete query created earlier
db.QueryDefs.Delete (strQryName)
'Create another Excel Workbook variable
'where data will be exported to and stored
'temporarily
Dim objExcelWrkBk2 As Excel.Workbook
Set objExcelWrkBk2 = objExcelApp.Workbooks.Open(strFilePath & strExcelFileName)
'Copy data from Workbook2 into
'master workbook (Workbook1)
objExcelWrkBk2.Sheets.Copy objExcelWrkBk1.Sheets(1)
'Close temp workbook
objExcelWrkBk2.Close
'Zoom in
objExcelApp.ActiveWindow.Zoom = 80
'Adjust columns of master Workbook
objExcelApp.Cells.Columns.AutoFit
'Format Date Column
objExcelApp.Columns("A:A").Select
objExcelApp.Selection.NumberFormat = "[$-409]m/d/yy h:mm AM/PM;@"
'Highlight upper left box
objExcelApp.Range("A1").Select
'Delete temp Workbook
Kill (strFilePath & strExcelFileName)