473,473 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Access 2007 - Create a command button that will filter my subform?

2 New Member
Hello out there!

I've turned into an internet junkie trying to find the answer to this seemingly simple question. Every suggestion I try has failed. Form wizard, VBA, macros, modules ... I never wanted to know so much about SQL statements.

If it's not obvious, please be aware I'm not a programmer, just a clerk who happens to know too much about Access for her own good. I AM willing to learn whatever you are patient enough to teach, if needs be.

So ...

I have a form that uses an unbound combo box to filter to my subform. There is a combo box on the subform,[Priority] that has has a Row Source I entered manually. The options are High, Med, Low and Done.

I need a command button to filter out the 'Done' records (and possibly one to show all records).

Thoughts/Questions/Comments?
Sep 10 '10 #1
5 15956
liimra
119 New Member
First of all, good on you and welcome to Bytes (I don't know if I am allowed to welcome you since I am new too!). Anyways, it doesn't really matter if you place the button or the combobox on the form or the sub form as both will work.

Of course, the source for the records of the subform is table/query and most probably it will have the priority field, otherwise how would we sort them! If this is the case you can create two buttons. One for showing records that have the selected priority and another for showing all records. The first would have this VB code attached to its OnClick Event:

Expand|Select|Wrap|Line Numbers
  1. strSQL = "Select * from TableName where"
  2. strSQL = strSQL & " Priority=" & "Forms!FormName!ComboBoxName"
  3. Me!SubFormName.Form.RecordSource = strSQL 
This will show only the records that you want on the subform. If you choose "Done", then only the records that are done will be showing. Suppose that you don't choose any value, then records with no priority set will be showing.

You can use this code for showing all records (No Filter)

Expand|Select|Wrap|Line Numbers
  1. strSQL = "Select * from TableName"
  2. Me!SubFormName.Form.RecordSource = strSQL
  3.  

Please note what I just stated in the attached database. I created it in Acc2007 and coverted it to 2003 (in case you are using it). I just added one more thing there (Will return Message if no value is selected).

Regards,
Ali
Attached Files
File Type: zip Done.zip (31.9 KB, 991 views)
Sep 10 '10 #2
beacon
579 Contributor
Hi Kat,

Welcome to Bytes!

Just to make sure I'm clear on what you're asking, you want a command button on the Main form to filter out the results in the subform if the value of the Priority combo box on the subform has a value of "Done", correct? Also, you are wanting a command button that will basically reset the action of the command button and show all records if the "Done" records were filtered out?

If so, I think you would probably get the most out of your form if you create a command button that serves as a toggle to show/hide the "Done" records.

Ok, assuming that you don't already have the command button on the form, go ahead and add it to the Main form. You'll need to know the SQL for the subform, both with the "Done" records and without. An easy way to do this is to go to the query for the subform and enter
Expand|Select|Wrap|Line Numbers
  1. Not "Done"
  2.  
for the Criteria. Then go to View -> SQL View and copy/paste the SQL into the strRemoveSQL variable in the code I'm about to provide. Then go back to Design view for the query, remove the Not "Done" and go back to SQL View. You will need to copy/paste the SQL for this into the strAllSQL variable in the code I'm about to provide.

Once you've gotten this far, right-click on the command button, click on properties, and scroll down to "On Click" event. Click the dropdown and choose "[Event Procedure]" and then click on the elipsis (...) to the right. The VBA window should open for you with the template for the click event entered for you. In the event, copy/paste the following code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub [YourCommandButtonName]_Click()
  2.  
  3.     Dim strRemoveSQL As String, strAllSQL As String
  4.  
  5.     strRemoveSQL = "SELECT * FROM [YourTable/QueryName] WHERE ((Not ([YourTable/QueryName].[Priority])='Done'));"
  6.     strAllSQL = "SELECT * FROM [YourTable/QueryName];"
  7.  
  8.     If [YourCommandButtonName].Caption = "Remove Completed Records" Then
  9.         Forms![YourMainFormName]![YourSubformName].Form.RecordSource = strRemoveSQL
  10.         Forms!Forms![YourMainFormName]![YourSubformName].Form.Requery
  11.         [YourCommandButtonName].Caption = "Show All Records"
  12.     Else
  13.         Forms!Forms![YourMainFormName]![YourSubformName].Form.RecordSource = strAllSQL
  14.         Forms!Forms![YourMainFormName]![YourSubformName].Form.Requery
  15.         [YourCommandButtonName].Caption = "Remove Completed Records"
  16.     End If
  17.  
  18. End Sub
  19.  
You will need to substitute the names of your table/query, form, subform, and command button names in the areas I've identified.

When all is said and done, and you run the form, the command button should start out saying "Remove Completed Records" and should remove all records with "Done" in the [Priority] field when the button is clicked. It should also change the caption of the button to now read "Show All Records". If you click the button again, all records will show and the caption will change back.

Hope this helps,
beacon
Sep 10 '10 #3
Kat7
2 New Member
First, thank you for the welcome and for taking on my problem! I got back to it yesterday afternoon and was anxoius to try out the solutions.

liimra's suggestion worked, but didn't really do what I was looking for. I just need to remove the "Done" records (and show all). Thank you though!

Beacon's solution worked best but there is one snag. The form is based on a table, not a query. I created a query to mimic my table, but then the unbound combo box on my form was not filtering the subform.

A little extra info in case that proves confusing. It's a very simple database (just a tool to organize managerial issues). I've got the original table with issue info. A second table I made with list of the managers, specifically for the combo box filter on the form. The form has the combo box manager filter and the subform with all the info.

Really, I thought this was going to be an easy project. Hah! Thanks again for all your help!

Kat
Sep 14 '10 #4
liimra
119 New Member
You thought this was going to be easy project and yes it is an easy project.

If you want to exclude the selection (done or else) you just change = to <>!


Expand|Select|Wrap|Line Numbers
  1. strSQL = strSQL & " Priority<>" & "Forms!FormName!ComboBoxName"
The thing you want is accomplished in this approach (filtering records), the criteria is the last thing to worry about whether it is one or many.



Regards,
Ali
Sep 14 '10 #5
beacon
579 Contributor
I apologize Kat...I assumed, incorrectly, that you were using queries on your form, not tables.

I'm glad that at least part of what I suggested worked out for you and hopefully Ali's change to my suggestion will help you finish the project.
Sep 15 '10 #6

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

Similar topics

2
by: Senthil | last post by:
Hi All I need to create an Excel report and create a command button and have to run a macro on the click event that will print all the pages in the Excel workbook. I am able to create the report...
13
by: shreyansghia | last post by:
Hello , I am using MS Access 2007. Now if i ve a subform based on a table, filtering the subform is extremely easy . I only ve to click the relevant column head on the subform and check/uncheck...
2
by: kanthee23 | last post by:
How to create different shaped command buttons in VB6?
3
by: dstork | last post by:
Has anyone else experienced Access 2007 crashing when users right click on the subform (with the intention to use the copy feature in the shortcut menu). I went through Allen Browne's...
4
by: olseni | last post by:
Hi all I hope someone can help me. I need to make some reports out of access, and store them on a network share under a specific report ID. This means that once I have the Report Id I want to create...
2
by: EORTIZ | last post by:
I have a report that is grouped by a field. I have looked for ways in which it would be possible to obtain an individual report for each group to export to PDF. Since if I have all groups in the...
2
by: falroc | last post by:
I created a Access 2007 database that had a add record command button that worked just fine until I split the database and made Sqlexpress 2005 the backend. Now, when I click on the add record...
2
by: Suprim | last post by:
Hi, In visual basic 6 program i am tired of using command button of rectangular and square shape. So I want to make the command button of different shape. For example, of round and oval shape. I...
1
by: Lori Jones | last post by:
Access 2007, have seen several posts on this, but no answer. Trying to load from Query in the Advanced Filter, but no filters/queries show up. I can create a filter and choose to save it as a...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.