MS Access Form filtered results to excel sheet with condition statements! (1 Viewer)

scotty123

New member
Local time
Today, 15:25
Joined
Nov 1, 2018
Messages
5
Hi,
I have found the motherload of a question and I cannot find how to do it. I want to output the results of my form after the person has filtered it so some VBA like this
DoCmd.OutputTo acOutputForm, "Report 13", acFormatXLS, "C:\DATABASE\BLQ-10\ExportedResults.xls"
Know-how in the world can i include my conditional formatting please help i have been stuck on this for weeks
 

theDBguy

I’m here to help
Staff member
Local time
Today, 15:25
Joined
Oct 29, 2018
Messages
21,474
Hi. Welcome to AWF! I am just thinking out loud here, but I think, from what I am understanding from your question, because it's a "special" requirement, that you'll probably have to "automate" Excel to get what you want. Specifically, you may have to use the CopyFromRecordset method to get the filtered data from your form to Excel. With "Excel automation," you can then apply Conditional Formatting.
 

Micron

AWF VIP
Local time
Today, 18:25
Joined
Oct 20, 2018
Messages
3,478
Pretty sure you can outputto the sql itself. So you would have to construct new or modify the existing sql and output that.
EDIT - I missed the cf part, so maybe ignore my post.
 

scotty123

New member
Local time
Today, 15:25
Joined
Nov 1, 2018
Messages
5
the ideas seem good how would I even go about coding them?
 

Micron

AWF VIP
Local time
Today, 18:25
Joined
Oct 20, 2018
Messages
3,478
I don't think you're including what I wrote as a "good" idea? It doesn't help you with cf.
If you are, then I owe you and answer. If not, then you have one example of Automation to review.
 

scotty123

New member
Local time
Today, 15:25
Joined
Nov 1, 2018
Messages
5
I don't think you're including what I wrote as a "good" idea? It doesn't help you with cf.
If you are, then I owe you and answer. If not, then you have one example of Automation to review.
No, I think you are on track for an answer but how could I select the filtered SQL from the form and import that to an excel sheet?
 

scotty123

New member
Local time
Today, 15:25
Joined
Nov 1, 2018
Messages
5
ok, know we are getting some were TY theDBGuy love the image btw so he found this code below and yes so it exports the data as I have selected it to step 1 know I guess i need to add in condition statements for what I want?

Code:
Public Sub cmdExport_Click()

Call ExportRecordset2XLS(Me.RecordsetClone)




End Sub
'---------------------------------------------------------------------------------------
' Procedure : ExportRecordset2XLS
' Author    : Daniel Pineault, CARDA Consultants Inc.
' Website   : http://www.cardaconsultants.com
' Purpose   : Export the passed recordset to Excel
' Copyright : The following is release as Attribution-ShareAlike 4.0 International
'             (CC BY-SA 4.0) - https://creativecommons.org/licenses/by-sa/4.0/
'
' Input Variables:
' ~~~~~~~~~~~~~~~~
' rs        : Recordset object to export to excel
'
' Usage:
' ~~~~~~
' Call ExportRecordset2XLS(Me.RecordsetClone)
'
' Revision History:
' Rev       Date(yyyy/mm/dd)        Description
' **************************************************************************************
' 1         2017-Mar-13             Initial Release
' 2         2018-09-20              Updated Copyright
'---------------------------------------------------------------------------------------
Public Sub ExportRecordset2XLS(ByVal rs As DAO.Recordset)
    '#Const EarlyBind = True 'Use Early Binding, Req. Reference Library
    #Const EarlyBind = False    'Use Late Binding
    #If EarlyBind = True Then
        'Early Binding Declarations
        Dim oExcel            As Excel.Application
        Dim oExcelWrkBk       As Excel.Workbook
        Dim oExcelWrSht       As Excel.Worksheet
    #Else
        'Late Binding Declaration/Constants
        Dim oExcel            As Object
        Dim oExcelWrkBk       As Object
        Dim oExcelWrSht       As Object
        Const xlCenter = -4108
    #End If
    Dim bExcelOpened          As Boolean
    Dim iCols                 As Integer
 
    'Start Excel
    On Error Resume Next
    Set oExcel = GetObject(, "Excel.Application")    'Bind to existing instance of Excel
 
    If Err.Number <> 0 Then    'Could not get instance of Excel, so create a new one
        Err.Clear
        On Error GoTo Error_Handler
        Set oExcel = CreateObject("Excel.Application")
        bExcelOpened = False
    Else    'Excel was already running
        bExcelOpened = True
    End If
    On Error GoTo Error_Handler
 
    oExcel.ScreenUpdating = False
    oExcel.Visible = False   'Keep Excel hidden until we are done with our manipulation
    Set oExcelWrkBk = oExcel.Workbooks.Add()    'Start a new workbook
    Set oExcelWrSht = oExcelWrkBk.Sheets(1)
 
    With rs
        If .RecordCount <> 0 Then
            .MoveFirst    'This is req'd, had some strange behavior in certain instances without it!
            'Build our Header
            '****************
            For iCols = 0 To rs.Fields.Count - 1
                oExcelWrSht.Cells(1, iCols + 1).Value = rs.Fields(iCols).Name
            Next
            'Format the header
            With oExcelWrSht.Range(oExcelWrSht.Cells(1, 1), _
                                   oExcelWrSht.Cells(1, iCols))
                .Font.Bold = True
                .Font.ColorIndex = 2
                .Interior.ColorIndex = 1
                .HorizontalAlignment = xlCenter
            End With
            'Copy the data from our query into Excel
            '***************************************
            oExcelWrSht.Range("A2").CopyFromRecordset rs
 
            'Some formatting to make things pretty!
            '**************************************
            'Freeze pane
            oExcelWrSht.Rows("2:2").Select
            With oExcel.ActiveWindow
                .SplitColumn = 0
                .SplitRow = 1
                .FreezePanes = True
            End With
            'AutoFilter
            oExcelWrSht.Rows("1:1").AutoFilter
            'Fit the columns to the content
            oExcelWrSht.Range(oExcelWrSht.Cells(1, 1), _
                              oExcelWrSht.Cells(1, iCols)).EntireColumn.AutoFit
            'Start at the top
            oExcelWrSht.Range("A1").Select
        Else
            MsgBox "There are no records returned by the specified queries/SQL statement.", _
                   vbCritical + vbOKOnly, "No data to generate an Excel spreadsheet with"
            GoTo Error_Handler_Exit
        End If
    End With
 
Error_Handler_Exit:
    On Error Resume Next
    oExcel.Visible = True   'Make excel visible to the user
    Set rs = Nothing
    Set oExcelWrSht = Nothing
    Set oExcelWrkBk = Nothing
    oExcel.ScreenUpdating = True
    Set oExcel = Nothing
    Exit Sub
 
Error_Handler:
    MsgBox "The following error has occurred" & vbCrLf & vbCrLf & _
           "Error Number: " & Err.Number & vbCrLf & _
           "Error Source: ExportRecordset2XLS" & vbCrLf & _
           "Error Description: " & Err.Description & _
           Switch(Erl = 0, "", Erl <> 0, vbCrLf & "Line No: " & Erl) _
           , vbOKOnly + vbCritical, "An Error has Occurred!"
    Resume Error_Handler_Exit
End Sub
 

HalloweenWeed

Member
Local time
Today, 18:25
Joined
Apr 8, 2020
Messages
213
I would write the data from the Access query into a table (maketable query?) within a db, then import & link to a table in blank spreadsheet within an Excel workbook. Then, in another spreadsheet you could query the info from that (DLookup, DCount, etc.).
 

Micron

AWF VIP
Local time
Today, 18:25
Joined
Oct 20, 2018
Messages
3,478
how could I select the filtered SQL from the form and import that to an excel sheet
I don't yet know what the record source for the form is and the answer entirely depends on that. Suppose it was a stored query. Then you could retrieve the sql property, modify it (add the filter part as criteria) and output the new sql. That becomes the parameter in the output method where you specify the what.
If you've used a sql statement as the form recordsource, then you could retrieve and modify that.
If you've based the form on a table, you'd need to create a sql statement to filter the table and use that.
AFAIK, none of these will help you with conditional formatting though.
 

zeroaccess

Active member
Local time
Today, 17:25
Joined
Jan 30, 2020
Messages
671
Hi,
I have found the motherload of a question and I cannot find how to do it. I want to output the results of my form after the person has filtered it so some VBA like this
DoCmd.OutputTo acOutputForm, "Report 13", acFormatXLS, "C:\DATABASE\BLQ-10\ExportedResults.xls"
Know-how in the world can i include my conditional formatting please help i have been stuck on this for weeks
Can you show us a picture of your form?

I can't imagine how Excel would interpret a form. How does it know where to place the data? The answer is you're going to need to make a template in Excel that Access will export the record to. It is going to be tabular data and you can have conditional formatting applied in the template.
 

scotty123

New member
Local time
Today, 15:25
Joined
Nov 1, 2018
Messages
5
this all seems great what if I simply use
Code:
DoCmd.OutputTo acOutputForm, "frmResults", acFormatXLS
and then somehow call it to make changes to the data adding condition statements
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:25
Joined
May 21, 2018
Messages
8,529
and then somehow call it to make changes to the data adding condition statements
Sure you can open an instance of excel via code and apply the conditional formatting that you wish. However, if you are going about that route, that is no simpler than what DBguy suggested. They did some formatting and coloring of the worksheet by cell, but you can code the formatconditions by column just as easily.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 18:25
Joined
May 21, 2018
Messages
8,529
FYI,
Export your worksheet. Go into Excel and turn on the macro recorder. Apply all your formatting, not just conditional formatting. Copy the code and post here. Now it will be a matter of converting the recorded macro into something that can be automated by Access. You will have to tell us what columns those formats apply to, because your recorded code will only say "selection" not the range. Not tried, but I assume the recorder will capture the code of adding conditions to the format collection.
 

Users who are viewing this thread

Top Bottom