473,396 Members | 2,102 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.

How to add VBA filters to reports

Hey all,

I'm a little new to Access 2007 and need help coding filters in VBA.

Right now my report has 3 fields:
FirstName LastName Grade

In the header of my report are 1 checkbox for each grade (9 - 12).

I want to be able to display, for example, only students in Grade 12 when I click the "Grade 12" checkbox.

The ApplyFilter method won't work because, according to this it can only be used on the OnOpen event.

I've seen the Me.FilterOn method used in some forum postings but I'm having difficulty getting that to work. Here's the code that I have:

Expand|Select|Wrap|Line Numbers
  1. Private Sub grade_check_Click()
  2.     Dim strFilter As String
  3.  
  4.     strFilter = "[Grade] = '12'"
  5.     Me.Filter = strFilter
  6. End Sub
I'd appreciate suggestions on what to try, or, even better, some websites that that describe doing to this sort of thing.

Thanks!
Sep 6 '11 #1

✓ answered by NeoPa

In that case you need event procedures to handle each CheckBox being changed. Each would call a single procedure that sets the value of your filter string based on ALL the CheckBoxes. The following code is an example of what might work :

Expand|Select|Wrap|Line Numbers
  1. Private Sub Grade_9_Chk_AfterUpdate()
  2.     Call SetFilter
  3. End Sub
  4.  
  5. Private Sub Grade_10_Chk_AfterUpdate()
  6.     Call SetFilter
  7. End Sub
  8.  
  9. Private Sub Grade_11_Chk_AfterUpdate()
  10.     Call SetFilter
  11. End Sub
  12.  
  13. Private Sub Grade_12_Chk_AfterUpdate()
  14.     Call SetFilter
  15. End Sub
  16.  
  17. Private Sub SetFilter
  18.     Dim strFilter As String
  19.  
  20.     With Me
  21.         If .Grade_9_Chk Then strFilter = strFilter & " OR ([Grade]='Nine')"
  22.         If .Grade_10_Chk Then strFilter = strFilter & " OR ([Grade]='Ten')"
  23.         If .Grade_11_Chk Then strFilter = strFilter & " OR ([Grade]='Eleven')"
  24.         If .Grade_12_Chk Then strFilter = strFilter & " OR ([Grade]='Twelve')"
  25.         .Filter = Mid(strFilter, 5)
  26.         .FilterOn = (strFilter > "")
  27.     End With
  28. End Sub

7 2267
NeoPa
32,556 Expert Mod 16PB
I'm curious to note that the name of the CheckBox for Grade = 12 appears to be Grade_Check. Nothing in there to indicate it's for a value of 12, which worries me. What other traps may there be if this is not logically set up?

You also describe multiple CheckBoxes, but in such a way as to imply only one would ever be set at a time. This would require a different type of control entirely (Radio Buttons within a Group).

With all these uncertainties it's hard to know what to suggest. There are a number of easy answers but without a better understanding of what you're asking (which you need first before including in the question) they could be quite wrong for wahat you actually need.

Whatever it turns out to be though, the solution will include :
  1. A comparison of the value that matches the Type of the field ('12' is fine, but only if the field is textual).
  2. Setting the report's .FilterOn property to match whether or not there is anything set in the .Filter property.

Although there's some confusion here, you should be reasonably happy with your attempt so far. The code is on the right lines, but computers are unforgiving objects. If it's nearly right but not quite then it won't work (and you don't get to see how close you were).
Sep 7 '11 #2
Hey NeoPa,

Thanks for the reply.

I'll clarify what I'm trying to accomplish, thanks for your patience.

Each grade (9 - 12) will have its own checkbox. If I check "Grade 12" only Grade 12's will be displayed. If I then check "Grade 10", grade 10's AND grade 12's will be displayed. If I then uncheck the "Grade 12" checkbox only grade 10's will be displayed. At least, that's what I want to happen.

The code I included above was just for the Grade 12 checkbox and rather hastily assembled, without much thought to good naming practices - my apologies for the ambiguity.

I should also mention that I can already do this by right-clicking on the Grade column in the report and toggling filters on. This is a little clunky though and I would like to do it via the checkboxes.

Thanks!
Sep 7 '11 #3
NeoPa
32,556 Expert Mod 16PB
If you can tell me what the names of the various CheckBoxes are then, and the field type of [Grade] in your table, I think I can help you.
Sep 7 '11 #4
Cool!

Let's see then...
The checkboxes:
  • grade_12_chk
  • grade_11_chk
  • grade_10_chk
  • grade_9_chk

Grade is a text field, ie. "twelve", "eleven", etc.

Thanks!
Sep 7 '11 #5
NeoPa
32,556 Expert Mod 16PB
In that case you need event procedures to handle each CheckBox being changed. Each would call a single procedure that sets the value of your filter string based on ALL the CheckBoxes. The following code is an example of what might work :

Expand|Select|Wrap|Line Numbers
  1. Private Sub Grade_9_Chk_AfterUpdate()
  2.     Call SetFilter
  3. End Sub
  4.  
  5. Private Sub Grade_10_Chk_AfterUpdate()
  6.     Call SetFilter
  7. End Sub
  8.  
  9. Private Sub Grade_11_Chk_AfterUpdate()
  10.     Call SetFilter
  11. End Sub
  12.  
  13. Private Sub Grade_12_Chk_AfterUpdate()
  14.     Call SetFilter
  15. End Sub
  16.  
  17. Private Sub SetFilter
  18.     Dim strFilter As String
  19.  
  20.     With Me
  21.         If .Grade_9_Chk Then strFilter = strFilter & " OR ([Grade]='Nine')"
  22.         If .Grade_10_Chk Then strFilter = strFilter & " OR ([Grade]='Ten')"
  23.         If .Grade_11_Chk Then strFilter = strFilter & " OR ([Grade]='Eleven')"
  24.         If .Grade_12_Chk Then strFilter = strFilter & " OR ([Grade]='Twelve')"
  25.         .Filter = Mid(strFilter, 5)
  26.         .FilterOn = (strFilter > "")
  27.     End With
  28. End Sub
Sep 7 '11 #6
Awesome!

Thanks so much; that's very helpful.
Sep 8 '11 #7
NeoPa
32,556 Expert Mod 16PB
I was pleased to help Clinton :-)
Sep 8 '11 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: cory | last post by:
Hi, I am looking for info on reports in MS Access. I am looking to create a report where the report is based on the relationships in the database. For Example: I have a project for a client. ...
3
by: RC | last post by:
I can't quite grasp the concept of creating custom reports depending upon what options a user picks on a Form. For example, the user clicks on a "Print Reports" button and a Form pops up. On the...
1
by: longtim | last post by:
I have been having endless difficulty creating reports/queries that set any relevent parameters from controls in forms. I am creating an application under access 2003 but will target access...
2
by: Cassie Pennington | last post by:
I am using VBA to determine which query sits behind a report, depending upon the preferred sort order. This has worked perfectly for ordinary select queries behind reports, but it seems to be...
0
by: John Dalberg | last post by:
I am having issues with Crystal Reports and user criteria (filters) supplied in the report aspx page. The user chooses a date range on the same page where the report resides. The problem is that...
69
by: DeJuan Jackson | last post by:
Just dropping a quick not for Tom Lane. I sent a personal message today, but I wasn't sure if you'd get it after I remembered all of the spam filters you've got set up. Sorry for the off topic...
3
by: MJP | last post by:
I've managed to preview multiple instances of a report, all with different filters. The problem now occurs when I try to print these reports or export as snapshot etc. If you try to print any of...
1
by: monskie | last post by:
Hello to all, I have a problem which could be trivial to you guys. This concerns opening several crystal reports on the a crystal viewer on an ASPX page by calling window.open. My...
6
by: smcdonald | last post by:
I have a report that opens up using a pretty complex query. I then pop up a form with combo boxes so the user can apply a filter to the existing report and then refresh the report. I need to export...
3
by: k2storm | last post by:
I'm having problems with Reports and I'm seeking some help, or advice on how to solve this problem. Any help will be much appriciated! It’s hard to explain but I will try to do my best: In my...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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: 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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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.