473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Add Filter to Report

Hi,

I have a report that works just fine.
Now I would like to add the capability to choose selection criteria to
limit what is displayed. I created several reports that do this, but
they used VBA to create SQL statements, then apply them. Now that I
have been using Access/VBA for awhile, I am wondering if there is a
simpler way to do this than coding these long sql statements.

My report has a query as its record source. Can I just add the filter
criteria to this query somehow? I have read threads about
"WhereCondition", and I have also read about using a me.filter on the
docmd.openreport statement. Any advice on which is the best way to go?
Please include details for coding, as I am still a newbie.

Thank you in advance,
Lori

ps - is there a way to set my profile up so that I get an email when I
get a response to my thread? I thought I used to have this.

Feb 23 '06 #1
1 16346
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

If the criteria is something simple like a beginning date and an ending
date you can set up the query like this:

PARAMETERS Forms!frmReportCriteria!txtBeginDate Date,
Forms!frmReportCriteria!txtEndDate Date;
SELECT *
FROM table_name
WHERE date_column BETWEEN Forms!frmReportCriteria!txtBeginDate And
Forms!frmReportCriteria!txtEndDate

In the report's Open event put this:

Private Sub Report_Open(Cancel As Integer)

Const FRM_CRIT = "frmReportCriteria"

' Using acDialog suspends this code until the
' criteria form closes or is hidden.
DoCmd.OpenForm FRM_CRIT, WindowMode:=acDialog

If Not CurrentProject.AllForms(FRM_CRIT).IsLoaded Then
' The criteria form was closed by the user
Cancel = True
' Else
' The criteria form has the needed criteria & the
' user has clicked the OK button.
' So, continue opening the Report.
End If

End Sub

In the frmReportCriteria, have the 2 Text Boxes (txtBeginDate and
txtEndDate) and 2 Command Buttons (cmdOK and cmdCancel). The Command
Buttons control whether the report runs or not. cmdCancel will close
the criteria form and the If ... IsLoaded Then statement in the report
will stop the report. When the cmdOK button is clicked, the report
IsLoaded evaulation will return True and the report will continue.

In the criteria form here are the Command Button events:

Private Sub cmdOK_Click()
' Check to see if both Text Boxes have data --
' I'll leave that to you...

' Then Hide the criteria form. This will allow the
' suspended VBA code in the report to continue at the
' If ... IsLoaded Then statement.

Me.Visible = False

End Sub

Private Sub cmdCancel_Click()
DoCmd.Close
End Sub

If there are more criteria than can be easily handled in the query's
PARAMETER statement, you can use the report's Filter property like this:

Set up the Report's Open event:

Put this in the Report's Declaration section of its VBA module:

Const FRM_CRIT = "frmReportCriteria"

Private Sub Report_Open(Cancel As Integer)

' Using acDialog suspends this code until the
' criteria form closes or is hidden.
DoCmd.OpenForm FRM_CRIT, WindowMode:=acDialog

If Not CurrentProject.AllForms(FRM_CRIT).IsLoaded Then
' The criteria form was closed by the user
Cancel = True
Else
' The criteria form has the needed criteria & the
' user has clicked the OK button.

' Run the Filter set up routine
SetUp

End If

End Sub

Private Sub SetUp()

' Set up the Report's Filter
Dim strFilter As String
Dim frm As Form_frmReportCriteria

Set frm = Forms(FRM_CRIT)

' There must be a column named SalesDate in the query
strFilter = "SalesDate BETWEEN #" & frm!txtBeginDate & "#" & _
" AND #" & frm!txtEndDate & "#"

' Set up the Report's filter
Me.Filter = strFilter
Me.FilterOn = True

End Sub

If there are more criteria on the criteria form you can concatenate them
into the strFilter using SQL WHERE clause syntax without the word WHERE.
The advantages of the Report Filter method is you can choose which
criteria on the criteria form to include in the report's Filter, which
makes the report very flexible. The disadvantage to this method is the
query may return a large recordset that the report may not use, which
will cause the query/report to run slower.

The advantages of the parameters in the query is the query/report will
run faster 'cuz the query uses the WHERE clause to eliminate all rows
that are not needed before sending the data to the report.

It's up to you to determine which is the best suited to your situation.
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQ/4lkoechKqOuFEgEQJG6ACdEOfAYznnsfNqJeYIOMfOg02HhQkA nAgb
ev6w6tIiqsEK1G+cZbTef6YP
=5RqF
-----END PGP SIGNATURE-----
lorirobn wrote:
Hi,

I have a report that works just fine.
Now I would like to add the capability to choose selection criteria to
limit what is displayed. I created several reports that do this, but
they used VBA to create SQL statements, then apply them. Now that I
have been using Access/VBA for awhile, I am wondering if there is a
simpler way to do this than coding these long sql statements.

My report has a query as its record source. Can I just add the filter
criteria to this query somehow? I have read threads about
"WhereCondition", and I have also read about using a me.filter on the
docmd.openreport statement. Any advice on which is the best way to go?
Please include details for coding, as I am still a newbie.

Feb 23 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: arthur-e | last post by:
I can filter a query for a report on a field of a subform BUT now.... I'd like to be able to select more than one item in a multi-list box to select all the records. ALSO to use two fields (or...
3
by: Glenn Zamora | last post by:
I am new to MS Access. I have created a simple entry form to track patient information. I have also created a Patient Report that prints Patient Information with a standard disclaimer statement. I...
2
by: olle | last post by:
hi everyone. I have this code to apply the filter on a form to a report. In Access97 it worked fine but when I converted it to Access2000 the report shows all records in the table. What has...
3
by: josejomonm | last post by:
I am having a list box where I will select the Plants (Plant-1 or Plant-2 Etc.) or leave blank for selecting all plants. Another list box in the same form where the supervisors list is provided. A...
5
by: favor08 | last post by:
have a mainform called PendingsMain and a subform called PendingsSub. You can filter the subform by different filters and this works fine. i want to create a report that will print out the...
94
by: mlcampeau | last post by:
I have a report (JobVacanciesOnly) that has a subreport (JobVacanciesOnlySR) that are based on two separate queries. MY - JobVacancyJobs SELECT Job.Code, Job.Title, Job.Grade, Grade.Minimum,...
1
by: Cara Murphy | last post by:
Hi There! Hoping you are able to help me with a filtering scenario: I have created a pop up form (Filter) to filter a report (Open Issues), which currently has a number of combo boxes which...
12
by: HowHow | last post by:
I need to create a few reports using one query and I wish to do the program filter from the buttons in a form. I had created a query named q_ClientsHvServices and a report based on that query named...
2
by: jim190 | last post by:
I am relatively new to using VBA and am have been trying for two days to pass a user entered value to my report for filtering when opened. I have the following subroutine in the open event of my...
25
smithj14
by: smithj14 | last post by:
I have a form that has an option group (fraReports) which holds a list of reports to print. This part works fine. I select a report name and click print and that report opens. Now I want to add a...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.