How To Make An Access Report Based On Form Inputs

Hi, this post is on “How To Make An Access Report Based On Form Inputs”.

Many times you want your report based on dynamic criteria. In this post I am going
to show you how you can generate a report based on data selected from combo box inputs on your form.

It’s easier to do with VBA. I’ll show you here:

Here I have a form called “frmParameters” with 3 combo boxes reflecting data from my table, “tblReportData”.

When I make my selections and click the “Produce Report” button, I get the following report result:

Here is my code for the filter form, “frmParameters”:

Option Compare Database

Private Sub cboFindingID_AfterUpdate()

    Dim strSQL2 As String

    If Len(Me.cboFindingID) > 0 Then
        strSQL2 = "SELECT DISTINCT [Bldg Number], [Finding ID] FROM tblReportData WHERE  [Finding ID]= '" & Me.cboFindingID & "'"
    Else
        strSQL2 = "SELECT DISTINCT [Bldg Number] FROM tblReportData "
    End If
    
    
    Me.cboBuildingNumber.RowSource = strSQL2
    
End Sub

Private Sub cboBuildingNumber_AfterUpdate()

    Dim strSQL3 As String
    
    If Len(Me.cboBuildingNumber) > 0 Then
        strSQL3 = "SELECT DISTINCT [Rating Description] FROM tblReportData WHERE  [Bldg Number]= '" & Me.cboBuildingNumber & "'"
    Else

        strSQL3 = "SELECT DISTINCT [Rating Description] FROM tblReportData  "
    End If

    Me.cboRatingDesc.RowSource = strSQL3
End Sub


Private Sub btnProduceReport_Click()
    
    DoCmd.OpenReport "rptReportData", acViewPreview
    
End Sub

…and here is the code for the report, “rptReportData”

Option Compare Database

Private Sub Report_Open(Cancel As Integer)
    Dim strSQL As String
    Dim strWhere As String
    
    strSQL = "SELECT * FROM tblReportData WHERE "
    
    If Not IsNull(Forms("frmParameters").Controls("cboFindingID")) Then
        strWhere = "[Finding ID] = '" & Forms("frmParameters").Controls("cboFindingID") & "'"
    End If
    
    If Not IsNull(Forms("frmParameters").Controls("cboBuildingNumber")) Then
        strWhere = strWhere & " AND [Bldg Number] = '" & Forms("frmParameters").Controls("cboBuildingNumber") & "'"
    End If
        
    If Not IsNull(Forms("frmParameters").Controls("cboRatingDesc")) Then
        strWhere = strWhere & " AND [Rating Description] = '" & Forms("frmParameters").Controls("cboRatingDesc") & "'"
    End If
            
    'clean strWhere string
    If Left(strWhere, 4) = " AND" Then
        strWhere = Mid(strWhere, 5)
    End If
    
    Me.RecordSource = strSQL & strWhere
End Sub

Here’s the database:
access report parameters.accdb

Let me know if you have any questions.




By the way, if you got or are getting value from the VBA information, please give me a tip, thanks!


These posts may help answer your question too...

How To Escape Apostrophe In SQL Update Query

If you are looping a table with thousands of records, you’ll probably run into at least one that has an apostrophe in the field name. Like “Mike’s” or “M’cormick”, or something else. Anyway, here is one way to escape the string when you are doing your update query. Option Compare Database Sub YDriveLoop() ‘4/23/24 erik@loeblcomservices.com […]

How To Make An Access Form Time Picker

Here is a relatively easy way to select times for your time entry text boxes. It’s a reusable form that allows you to pick a time from an Access form. There are probably different ways to do this but here is the way I would do it. On the form that has the time fields, […]

How To Parse A Flat File In Excel VBA

In another post I demonstrated how to access a file on your computer using the MS Office Library. Here it is if you don’t know what I’m talking about. In this post, I am going to show you how to access the file and load it into your spreadsheet. I will do the same thing […]

How to pick a file to load In VBA

Picking a file to load in your Microsoft App is a very important skill to know. In this blog post you will see how to do it. First you need to set a reference to the MS office object library From VBE editor –> select Tools > MS office object library (click check mark) Sub […]

Previous Post

How do I create a query between two dates in Access?

Next Post

How To Embed A YouTube Video In Access Form