Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Export Multiple filtered Subform data to one Excel Workbook

Status
Not open for further replies.

debq

Technical User
Aug 7, 2008
50
US
I am using MS Access version 2010.
I have a Main Form, frm_ClientPriceDetails with three(3)subforms: fsub_ClientDetail, fsub_ClientOrderCodePrice_Data and fsub_ClientSpecialPrices.
I have a text box control on the Main form: txtClientID with Control Source set to
Code:
=[fsub_ClientDetail].[Form]![MASTER_CLIENTID]
I need to filter the form to an individual client with the client Ordercode pricing and special pricing and then export this filtered data to an Excel Spreadsheet.

I currently can Export the data with the following code: My export produces three separate Excel Spreadsheet and I would like to only produce one spreadheet with three tabs/sheets per subform.

Here is the code that I am using:

Code:
Public Function ExportToExcel(frm As Form, Optional strSheetName As String)

Dim rst As DAO.Recordset
     Dim ApXL As Object
     Dim xlWBk As Object
     Dim xlWSh As Object
     Dim fld As DAO.Field
     Const xlCenter As Long = -4108
     Const xlBottom As Long = -4107
 
    On Error GoTo err_handler
 
    Set rst = frm.RecordsetClone
 
  
    Set ApXL = CreateObject("Excel.Application")
     Set xlWBk = ApXL.Workbooks.Add
     ApXL.Visible = True
         
    Set xlWSh = xlWBk.Worksheets("Sheet1")
     If Len(strSheetName) > 0 Then
         xlWSh.Name = Left(strSheetName, 34)
     End If
     xlWSh.Activate
     xlWSh.Range("A1").Select
 
     For Each fld In rst.Fields
         ApXL.ActiveCell = fld.Name
         ApXL.ActiveCell.Offset(0, 1).Select
     Next
 
    rst.MoveFirst
     xlWSh.Range("A2").CopyFromRecordset rst
     xlWSh.Range("1:1").Select
    
     With ApXL.Selection.Font
         .Name = "Arial"
         .Size = 12
         .Strikethrough = False
         .Superscript = False
         .Subscript = False
         .OutlineFont = False
         .Shadow = False
     End With
     ApXL.Selection.Font.Bold = True
     With ApXL.Selection
         .HorizontalAlignment = xlCenter
         .VerticalAlignment = xlBottom
         .WrapText = False
         .Orientation = 0
         .AddIndent = False
         .IndentLevel = 0
         .ShrinkToFit = False
         .MergeCells = False
     End With
     ' selects all of the cells
     ApXL.ActiveSheet.Cells.Select
     ' does the "autofit" for all columns
     ApXL.ActiveSheet.Cells.EntireColumn.AutoFit
     ' selects the first cell to unselect all cells
     xlWSh.Range("A1").Select
 
 
    rst.Close
     Set rst = Nothing
 
    Exit Function
err_handler:
     DoCmd.SetWarnings True
     MsgBox Err.Description, vbExclamation, Err.Number
     Exit Function
 


End Function

I am calling the code with the following routine on an command button ON CLICK Event Procedure:
Code:
Private Sub cmd_SendToExcel_Click()

    Call ExportToExcel(Me.fsub_ClientDetail1.Form, "ClientDetail")
    Call ExportToExcel(Me.fsub_OrderCodePrice_Data1.Form, "OrderCodePriceList")
    Call ExportToExcel(Me.fsub_ClientSpecialPrices1.Form, "ClientSpecialPrices")

End Sub

Thanks in advance for any guidence you can provide.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top