By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,475 Members | 1,372 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,475 IT Pros & Developers. It's quick & easy.

How to filter report using button in Access 2000?

P: 48
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 Rpt_ClientsHvService. In the main form, I created few buttons to view clients from different progrom. When I click on a button named "HACC", the report should select all the clients that have services under PROGRAM starting with HA (and all HA*). When I click on a button named "CACP", the report should select all the clients starting with "CAC" program. Is it possible?

How should I write the filter based on the click on the buttons?

My code on my "All client" button are as follow:

Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_OpenALLClientsHvSvc_Click()
  2. On Error GoTo Err_btn_OpenALLClientsHvSvc_Click
  3.  
  4.     Dim stDocName As String
  5.  
  6.     stDocName = "Rpt_ClientsHvService"
  7.     DoCmd.OpenReport stDocName, acPreview
  8.  
  9. Exit_btn_OpenALLClientsHvSvc_Click:
  10.     Exit Sub
  11.  
  12. Err_btn_OpenALLClientsHvSvc_Click:
  13.     MsgBox Err.Description
  14.     Resume Exit_btn_OpenALLClientsHvSvc_Click
  15.  
  16. End Sub
Oct 11 '07 #1
Share this Question
Share on Google+
12 Replies


Jim Doherty
Expert 100+
P: 897
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 Rpt_ClientsHvService. In the main form, I created few buttons to view clients from different progrom. When I click on a button named "HACC", the report should select all the clients that have services under PROGRAM starting with HA (and all HA*). When I click on a button named "CACP", the report should select all the clients starting with "CAC" program. Is it possible?

How should I write the filter based on the click on the buttons?

My code on my "All client" button are as follow:

Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_OpenALLClientsHvSvc_Click()
  2. On Error GoTo Err_btn_OpenALLClientsHvSvc_Click
  3.  
  4. Dim stDocName As String
  5.  
  6. stDocName = "Rpt_ClientsHvService"
  7. DoCmd.OpenReport stDocName, acPreview
  8.  
  9. Exit_btn_OpenALLClientsHvSvc_Click:
  10. Exit Sub
  11.  
  12. Err_btn_OpenALLClientsHvSvc_Click:
  13. MsgBox Err.Description
  14. Resume Exit_btn_OpenALLClientsHvSvc_Click
  15.  
  16. End Sub

You need to build your criteria string for the WHERE clause of the openreport argument so that it knows the records to return as part of the report

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim stDocName As String
  3.     Dim stLinkCriteria As String
  4.     stDocName = "Rpt_ClientsHvService"
  5.  
  6. stLinkCriteria = "[PROGRAM] Like 'HACC*'"
  7.  
  8. DoCmd.OpenReport stDocName, acPreview, , stWhere
  9.  
I am sure you can adapt this to fit your fieldnames and other requirements .

For versatility you might want to look at referencing a selectable value from a form control as well, because hard coded like this it only does ONE thing (which may suit your purpose of course I don't know)

Regards

Jim :)
Oct 11 '07 #2

P: 48
You need to build your criteria string for the WHERE clause of the openreport argument so that it knows the records to return as part of the report

Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim stDocName As String
  3.     Dim stLinkCriteria As String
  4.     stDocName = "Rpt_ClientsHvService"
  5.  
  6. stLinkCriteria = "[PROGRAM] Like 'HACC*'"
  7.  
  8. DoCmd.OpenReport stDocName, acPreview, , stWhere
  9.  
I am sure you can adapt this to fit your fieldnames and other requirements .

For versatility you might want to look at referencing a selectable value from a form control as well, because hard coded like this it only does ONE thing (which may suit your purpose of course I don't know)

Regards

Jim :)

I still can't get it right. Is it because I don't have [Program] in my report? Does the code control the [Program] on Query?

My purpose is to know the number of the client received service from particular program and get a client name list from that result. If I put [Program] to my Report, the client numbers will be more than what I expected because it group by program and then by client's name. Well, I had tried to put [Program] to report and tested on the code, the report gave all the clients from all the program. The code is as below, can you spot where goes wrong?

Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_OpenALLClientHasSvc_Click()
  2. On Error GoTo Err_btn_OpenALLClientHasSvc_Click
  3.  
  4.  
  5.     Dim stDocName As String
  6.     Dim stLinkCriteria As String
  7.     stDocName = "Rpt_ClientHasService"
  8.     stLinkCriteria = "[Program] like 'HA*' "
  9.  
  10.     DoCmd.OpenReport stDocName, acPreview, , stWhere
  11.  
  12. Exit_btn_OpenALLClientHasSvc_Click:
  13.     Exit Sub
  14.  
  15. Err_btn_OpenALLClientHasSvc_Click:
  16.     MsgBox Err.Description
  17.     Resume Exit_btn_OpenALLClientHasSvc_Click
  18.  
  19. End Sub

Previously I created query for each program, then created reports based on the queries. It is accurate but I found too many queries and reports to maintan everytime something changed. So I am thinking of using one standard Query and one standard Report but control the program filter in Query thru button that has Program caption in Form.

What should I do to control the filter in Query from Form (button) so that I will get the correct Report? Is it possible?
Oct 12 '07 #3

Jim Doherty
Expert 100+
P: 897
I still can't get it right. Is it because I don't have [Program] in my report? Does the code control the [Program] on Query?

My purpose is to know the number of the client received service from particular program and get a client name list from that result. If I put [Program] to my Report, the client numbers will be more than what I expected because it group by program and then by client's name. Well, I had tried to put [Program] to report and tested on the code, the report gave all the clients from all the program. The code is as below, can you spot where goes wrong?

Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_OpenALLClientHasSvc_Click()
  2. On Error GoTo Err_btn_OpenALLClientHasSvc_Click
  3.  
  4.  
  5. Dim stDocName As String
  6. Dim stLinkCriteria As String
  7. stDocName = "Rpt_ClientHasService"
  8. stLinkCriteria = "[Program] like 'HA*' "
  9.  
  10. DoCmd.OpenReport stDocName, acPreview, , stWhere
  11.  
  12. Exit_btn_OpenALLClientHasSvc_Click:
  13. Exit Sub
  14.  
  15. Err_btn_OpenALLClientHasSvc_Click:
  16. MsgBox Err.Description
  17. Resume Exit_btn_OpenALLClientHasSvc_Click
  18.  
  19. End Sub

Previously I created query for each program, then created reports based on the queries. It is accurate but I found too many queries and reports to maintan everytime something changed. So I am thinking of using one standard Query and one standard Report but control the program filter in Query thru button that has Program caption in Form.

What should I do to control the filter in Query from Form (button) so that I will get the correct Report? Is it possible?

Hi HowHow :)

With the greatest of respect read your original post you said

"select all the clients that have services under PROGRAM starting with HA"

From that I can only assume that PROGRAM is a fieldname in your table or query and that it is this that you are going to compare in returning a dataset where the PROGRAM contains data starting with HA...hence the LIKE operand.

The logic in all of this is to return datasets based on comparisons you make against fielded data using the WHERE clause of an SQL statement.

So......

SELECT * FROM tblMyData WHERE [Surname]='Jones'"
returns everyone who has a surname of Jones

SELECT * FROM YourTableName WHERE [Program]='Rusty Car' AND [Service]='15,000,000 miles'
returns all cars that either need scrapping or awarding a congratulatory certificate of longevity.

The code displayed for the report is correct! in that it 'would' open a report where and IF a field existed on there called PROGRAM and where the field starts with HA.

You build your reports around your tables or queries and you call your reports restricting any data it returns by using the WHERE clause of the openreport method as displayed

Query by FORM or QBF as it is known in short terms from your perspective is an accepted method of calling a report and supplying criteria to it from FORM controls where you have picked or entered your criteria into the form (usually unbound) upon which the report will rely when it is opened.

If this is what you are wanting then yes it can be done. there are lots of references to the query by form methodology for you to research

Jim :)
Oct 12 '07 #4

P: 48
Hi HowHow :)

With the greatest of respect read your original post you said

"select all the clients that have services under PROGRAM starting with HA"

From that I can only assume that PROGRAM is a fieldname in your table or query and that it is this that you are going to compare in returning a dataset where the PROGRAM contains data starting with HA...hence the LIKE operand.

The logic in all of this is to return datasets based on comparisons you make against fielded data using the WHERE clause of an SQL statement.

So......

SELECT * FROM tblMyData WHERE [Surname]='Jones'"
returns everyone who has a surname of Jones

SELECT * FROM YourTableName WHERE [Program]='Rusty Car' AND [Service]='15,000,000 miles'
returns all cars that either need scrapping or awarding a congratulatory certificate of longevity.

The code displayed for the report is correct! in that it 'would' open a report where and IF a field existed on there called PROGRAM and where the field starts with HA.

You build your reports around your tables or queries and you call your reports restricting any data it returns by using the WHERE clause of the openreport method as displayed

Query by FORM or QBF as it is known in short terms from your perspective is an accepted method of calling a report and supplying criteria to it from FORM controls where you have picked or entered your criteria into the form (usually unbound) upon which the report will rely when it is opened.

If this is what you are wanting then yes it can be done. there are lots of references to the query by form methodology for you to research

Jim :)

Hi Jim :),
Sorry for the unclear question previously.You are right that [Program] is in my table as well as in the query, however, it wasn't show in my report because I have "WHERE" in my query and I cannot tick "show" box.

Please forget about my previous set up. Now I had created a new query (called q_RA_unapproved), and a new report (called rpt_RA_unapproved) based on that query, both has [Program] in it. In the form, I created unbound button where I want to view "rpt_RA_unapproved" by program. However, the code below still doesn't work. It brings all the unapproved service from all the programs instead of the specific program I want.

Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_CACP_RA_unapproved_Click()
  2. On Error GoTo Err_btn_CACP_RA_unapproved_Click
  3.  
  4.     Dim stDocName As String
  5.     Dim stLinkCriteria As String
  6.  
  7.     stDocName = "rpt_RA_unapproved"
  8.     stLinkCriteria = "[Program] like ' *C.A.C* ' "
  9.     DoCmd.OpenReport stDocName, acPreview, , stWhere
  10.  
  11.  
  12. Exit_btn_CACP_RA_unapproved_Click:
  13.     Exit Sub
  14.  
  15. Err_btn_CACP_RA_unapproved_Click:
  16.     MsgBox Err.Description
  17.     Resume Exit_btn_CACP_RA_unapproved_Click
  18.  
  19. End Sub
Can you help spot where goes wrong? appreciate your time... :)
Oct 15 '07 #5

Jim Doherty
Expert 100+
P: 897
Hi Jim :),
Sorry for the unclear question previously.You are right that [Program] is in my table as well as in the query, however, it wasn't show in my report because I have "WHERE" in my query and I cannot tick "show" box.

Please forget about my previous set up. Now I had created a new query (called q_RA_unapproved), and a new report (called rpt_RA_unapproved) based on that query, both has [Program] in it. In the form, I created unbound button where I want to view "rpt_RA_unapproved" by program. However, the code below still doesn't work. It brings all the unapproved service from all the programs instead of the specific program I want.

Expand|Select|Wrap|Line Numbers
  1. Private Sub btn_CACP_RA_unapproved_Click()
  2. On Error GoTo Err_btn_CACP_RA_unapproved_Click
  3.  
  4. Dim stDocName As String
  5. Dim stLinkCriteria As String
  6.  
  7. stDocName = "rpt_RA_unapproved"
  8. stLinkCriteria = "[Program] like ' *C.A.C* ' "
  9. DoCmd.OpenReport stDocName, acPreview, , stWhere
  10.  
  11.  
  12. Exit_btn_CACP_RA_unapproved_Click:
  13. Exit Sub
  14.  
  15. Err_btn_CACP_RA_unapproved_Click:
  16. MsgBox Err.Description
  17. Resume Exit_btn_CACP_RA_unapproved_Click
  18.  
  19. End Sub
Can you help spot where goes wrong? appreciate your time... :)
Read your code strLinkCriteria is not being passed to the where clause of the openreport command you have

DoCmd.OpenReport stDocName, acPreview, , stWhere

it should be

DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria

You created criteria yes and assigned a variable to it but are not passing it the the command correctly

Regards

Jim
Oct 15 '07 #6

P: 48
Read your code strLinkCriteria is not being passed to the where clause of the openreport command you have

DoCmd.OpenReport stDocName, acPreview, , stWhere

it should be

DoCmd.OpenReport stDocName, acPreview, , stLinkCriteria

You created criteria yes and assigned a variable to it but are not passing it the the command correctly

Regards

Jim
Thank you! This is a big help! Now I can stop creating lots of queries and reports (for each program) now! yeah!

I guess I have no way to do the same if the field is not in the report even though the field [Program] is in my query and table. When I put the [Program] to the report, the total number of client is wrong because a client can have more than one program. I only want to have each client counted once and produce a non duplicate name list.
Oct 16 '07 #7

Jim Doherty
Expert 100+
P: 897
Thank you! This is a big help! Now I can stop creating lots of queries and reports (for each program) now! yeah!

I guess I have no way to do the same if the field is not in the report even though the field [Program] is in my query and table. When I put the [Program] to the report, the total number of client is wrong because a client can have more than one program. I only want to have each client counted once and produce a non duplicate name list.
Group your Report by Client in the report itself. Look at Report sorting and grouping

Jim
Oct 16 '07 #8

P: 48
Group your Report by Client in the report itself. Look at Report sorting and grouping

Jim
I had tried few times but still didn't get it. Even recreate my query and report...Really lost already!
When I have [Program] in both of my query and report, the report is group by both [Program] and client's name. For example, when a client received 3 programs many times from Jan to June, the report show client's name three times and each with a program beside (different program). Thus my text box with
Expand|Select|Wrap|Line Numbers
  1. =Count([Name]) 
total up the same client as 3 instead of 1. My name list should be one client but end up with 3 same name. Even after I make the [Program] invisible in report, it still does the same thing. Need your advise again please...thanks!
Oct 17 '07 #9

Jim Doherty
Expert 100+
P: 897
I had tried few times but still didn't get it. Even recreate my query and report...Really lost already!
When I have [Program] in both of my query and report, the report is group by both [Program] and client's name. For example, when a client received 3 programs many times from Jan to June, the report show client's name three times and each with a program beside (different program). Thus my text box with
Expand|Select|Wrap|Line Numbers
  1. =Count([Name]) 
total up the same client as 3 instead of 1. My name list should be one client but end up with 3 same name. Even after I make the [Program] invisible in report, it still does the same thing. Need your advise again please...thanks!
If you are grouping by client do you have the headers and footers displayed in your report for the client? if not do so and place any calculated control within the header or footer, that way your count function will count on that grouping only if I am reading you correctly.

Regards

Jim
Oct 17 '07 #10

P: 48
If you are grouping by client do you have the headers and footers displayed in your report for the client? if not do so and place any calculated control within the header or footer, that way your count function will count on that grouping only if I am reading you correctly.

Regards

Jim
Hi Jim,
Yes, I placed the calculated control on the report header. When I placed in other places, it won't work...It just shows #Error

I retried and re-read many times since yesterday and finally I know what goes wrong! Sorry I had mislead you with my previous question! I missed to point out this to you and this is very important! Each program is different!
For example, DC (Day Centre) program, we have shopping, outing-weekend, outing-weekday, young people with disability, transport, dementia, aboriginal group..etc. That is why when I choose *DC*, it chose all the clients under Day Centre including "DC-Shopping", "DC-Transport", "DC-Dementia"...etc. This is consider as DC program only but at the same time, they group up differently because they named differently.
In *HACC*, I have HACC-Domestic Assistance, HACC-Social Support, HACC-Transport, HACC-Gardening, HACC-Personal Care...etc. It is only HACC program but it will group differently if I show [Program] because the name is different.

That is why, at first, when I use a query named q_DC_ClientHvSvc. It works.In that query, I have [client name] and [Program] only. Under [Program] field, I use WHERE in "total" and type like *DC* in "criteria". Under [client name], I just have GROUP in "total". Result is, I have 121 client name listed. I created a report using that query and get the correct name list. With this method, I created few more queries, each for one program. And each program here means including different name within that program. I can get the name list for each program correctly because the [program] itself is not showing thus not group by program.

Now, I created a standart query. In that query, I have [client name] and [Program]. I created report using this new query but filter thru the button (which you showed me how to do earlier) so that each button will get the client name list from each program. When I click on the button that takes *DC* client, the client name list is 158 instead of 121 because some of the clients received more than one "activity" in one program. In the report, some of the client name repeated twice or three time depending on the "activity" they receive even though it is still in one program.

If I don't show [Program] field in my query and report, I cannot filter the [Program] using the button from my form. However, when I have [Program] in my query and in my report, my client name list is wrong.

Is there anyway to show or count client's name only once regardless of the "activity" they receive in the program? What should I do next?
Oct 22 '07 #11

Jim Doherty
Expert 100+
P: 897
Hi Jim,
Yes, I placed the calculated control on the report header. When I placed in other places, it won't work...It just shows #Error

I retried and re-read many times since yesterday and finally I know what goes wrong! Sorry I had mislead you with my previous question! I missed to point out this to you and this is very important! Each program is different!
For example, DC (Day Centre) program, we have shopping, outing-weekend, outing-weekday, young people with disability, transport, dementia, aboriginal group..etc. That is why when I choose *DC*, it chose all the clients under Day Centre including "DC-Shopping", "DC-Transport", "DC-Dementia"...etc. This is consider as DC program only but at the same time, they group up differently because they named differently.
In *HACC*, I have HACC-Domestic Assistance, HACC-Social Support, HACC-Transport, HACC-Gardening, HACC-Personal Care...etc. It is only HACC program but it will group differently if I show [Program] because the name is different.

That is why, at first, when I use a query named q_DC_ClientHvSvc. It works.In that query, I have [client name] and [Program] only. Under [Program] field, I use WHERE in "total" and type like *DC* in "criteria". Under [client name], I just have GROUP in "total". Result is, I have 121 client name listed. I created a report using that query and get the correct name list. With this method, I created few more queries, each for one program. And each program here means including different name within that program. I can get the name list for each program correctly because the [program] itself is not showing thus not group by program.

Now, I created a standart query. In that query, I have [client name] and [Program]. I created report using this new query but filter thru the button (which you showed me how to do earlier) so that each button will get the client name list from each program. When I click on the button that takes *DC* client, the client name list is 158 instead of 121 because some of the clients received more than one "activity" in one program. In the report, some of the client name repeated twice or three time depending on the "activity" they receive even though it is still in one program.

If I don't show [Program] field in my query and report, I cannot filter the [Program] using the button from my form. However, when I have [Program] in my query and in my report, my client name list is wrong.

Is there anyway to show or count client's name only once regardless of the "activity" they receive in the program? What should I do next?
Hi How How,

This can doubtless be sorted and no need to apologise you like all of us can easily make mistakes, however descriptively speaking on here you have lost me I'm afraid. If you PM (private message) me with your email address we can communicate offline and maybe you can send me an empty copy of your database and I'll take a look at it.

There may be a simple adjustment of design that can be had to return what you need but I'm afraid without looking at it my end I'm blind somewhat.

Regards

Jim :)
Oct 22 '07 #12

P: 48
Hi How How,

This can doubtless be sorted and no need to apologise you like all of us can easily make mistakes, however descriptively speaking on here you have lost me I'm afraid. If you PM (private message) me with your email address we can communicate offline and maybe you can send me an empty copy of your database and I'll take a look at it.

There may be a simple adjustment of design that can be had to return what you need but I'm afraid without looking at it my end I'm blind somewhat.

Regards

Jim :)
Thank you! Finally I got it! The Function works fine and my mistake was didn't take out the date, and service type...etc.
Only after I removed every field, left only [client name] and [ProgramGroup], it grouped up as what I need. Thanks again Jim! You are very kind and helpful!

Just to share the function with others if they need it.

Expand|Select|Wrap|Line Numbers
  1. Public Function ProgramType(strProgram As String)
  2.  
  3. On Error Resume Next
  4. If InStr(strProgram, "-DC-") > 0 Then
  5. ProgramType = "DC"
  6. End If
  7.  
  8. ' Group the program that has the word *DC* to DC
  9.  
  10. End Function
And in query, call the function by typing the below in field:

Expand|Select|Wrap|Line Numbers
  1. ProgramType: ProgramType([Program])
Oct 23 '07 #13

Post your reply

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