Question

Access Filter with Combo Box based on Date

Asked by: markj72000

I'm pretty new to Access so please bare with me.

I have a split form that pulls data from single table. It has a data column with a date formatted like this: 10/31/2008

I need to create a few combo box filter controls in the header.

1st one will Filter all the records by Year. Example: display 2008, 2009, 2010, etc.
    This one will go ahead and apply the filter before selecting the month combo, just in case they want to view all of that years records

2nd one will filter by month based on the Year combo box filter. Example: display Jan,Feb,Mar,Apr, etc.

3rd one based off my research i should probably be a reset button or a method to choose all items again.

4th, I also need a combo box that pulls from one of the table columns and filters that data but i may can figure that on out with the wizard.

I know this has been asked a lot here but i cant seem to find one solution that works for my situation so maybe if i see the code for my situation it will help me better understand how this works.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-08-12 at 09:37:52ID24647071
Tags

access combo box

,

access filter

Topics

Microsoft Access Database

,

Access Coding/Macros

,

Access Forms

Participating Experts
2
Points
500
Comments
13

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. Filtering Data
    Here is a task that has been given to me, and I haven’t been able to figure out how to do it. We have an Excel page that has been exported from an Oracle database. It is listing how long our current parts will last. We want to filter this report down to just the critical da...
  2. Open Query From Combo Box In Form
    hi i have querys named: jan 01 feb 01 mar 01.... ranging all the way to dec 04 I have created a table called "Month (Stats)", with one field called "Month". In this field i have all the same names as my querys jan 01, feb 02, mar 03, apr 04 etc.... I ...
  3. Add month JAN FEB MAR to column
    is ther a efficient way to add the name of month in JAN FEB MAR format to column called MYMONTH example data MYMONTH Jan Feb Mar Apr etc... i tried the following code but I get error

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: LSMConsultingPosted on 2009-08-12 at 15:23:44ID: 25083699

I find it easier to build a single function that does this, then call that from the AfterUpdate event of each combo. For example:

Function FilterIt()

Dim sFilter As String

If Me.cboYear.Column(0) <> "" then
  sFilter = "Year(YourDateField)=" & Me.cboYear
End If

If Me.cboMonth.column(0) <> "" Then
  If Len(sFilter) > 0 then sFilter = sFilter & " AND "
  sfilter = sfilter & " Month(YourDateField") = " & Me.cboMonth
End If

Me.filter = sFilter
Me.FilterOn = True
End Function

To remove your filter:

me.FilterOn = False

 

by: LSMConsultingPosted on 2009-08-12 at 15:24:49ID: 25083702

Sorry, forgot about the AfterUpdate event:

Sub cboYear_AfterUpdate()
  FilterIt
End Sub

Same would apply to other combos, as needed.

 

by: markj72000Posted on 2009-08-12 at 17:50:12ID: 25084741

I know this is going to sound silly but I'm not totally sure where to apply that code to the combo. Sorry for the rookie questions.

When i create my combo box should i point the combo box to a table that holds the lookup values or should i choose the option to type in the option i want?

Your code: will that be applied to each of the drop down option field for the year or does that apply to the entire combo box control.

I've been messing with this on and off most of the day and i i guess I'm just not getting how this is done.

thanks for you patience.

Mark

 

by: LSMConsultingPosted on 2009-08-12 at 18:38:48ID: 25084939

Your combos would be based on Value Lists. Your Year combo would hold values like 2009, 2008, 2007 etc, while your month would hold values like 1, 2, 3 etc.

The FilterIt code would be a module in the form's Code Module. Your combos would call that procedure in the AfterUpdate event.

What version of Access are you using? If this is Access 2007, are you working with a MultiValue list?

 

by: markj72000Posted on 2009-08-12 at 19:31:24ID: 25085117

To be honest i didn't know what a multivalue list was until you said it and i looked it up here.

http://office.microsoft.com/en-us/access/HA012337221033.aspx

Wow, what a good idea.. unless there is some issue with using them that you may know of.

To answer your question - My field only contains one value that is pulled from a MySql database table. See my project explanation below. I'm using Access 2007.


It might be easier if i explain my project a little bit.

Our website takes all of our "Quote Request" and saves a copy of them to a MySql database. I setup a ODBC connection in Access to retrieve the records.

The point in this was so i could append more data to each record to indicate which sale person handled the lead, status of the lead. Salesperson and Status is using a Lookup combo box to retrieve these records from 2 separate tables. Along with a Note field "memo type" and a hyperlink field to give a easy link to open our online management system.

I ran into issues that i didn't think of when i linked to the MySql table since this was my first time. I couldn't figure out a way to work directly with the linked table which makes sense now that i think about it, nor could i figure out away to setup a relationship with the linked table for the amended information that would be associated with the MySql record.

So what i did was create a Query that would only appended new records. Actually it was 2 Query's which run off of a single button I created, i may try and automate this later so no user interaction is required. This copied the records to a new table with matching fields and the additional fields for the amended data that will be added later via this form I'm working on with you. I'm not sure if this is good design or not but since part of my data is retrieved in real time i couldn't figure out any other way to do it. :)


I'm getting an error when i try to select a year value from the drop down.

the expression After Update you entered as the event property setting produced the following error:
Error:  Ambiguous name detected: Year_AfterUpdate

I created the module with the following code:

Function FilterIt()

Dim sFilter As String

If Me.Year.Column(0) <> "" Then
  sFilter = "Year(Submitted Date)=" & Me.Year
End If

If Me.Month.Column(0) <> "" Then
  If Len(sFilter) > 0 Then sFilter = sFilter & " AND "
  sfilter = sfilter & " Month(Submitted Date") = " & Me.Month      <-- this line is red like there might be something wrong.
End If

Me.Filter = sFilter
Me.FilterOn = True
End Function



My After_Update Code:


Private Sub Year_AfterUpdate()

Sub Year_AfterUpdate()
  FilterIt
End Sub

End Sub




My fields are named like this:

Year = Combo Box
Submitted Date = Table field with a single date "10/31/2008



thanks for your help.

Mark

 

by: LSMConsultingPosted on 2009-08-13 at 03:14:34ID: 25086747

First: Don't go down the path of MVF combos ... while they look neat, they're difficult to work with and until they've been "polished" somewhat, shouldn't be used in a production app (at least in my opinion).

If Me.Year.Column(0) <> "" Then
  sFilter = "Year([Submitted Date])=" & Me.Year
End If

If Me.Month.Column(0) <> "" Then
  If Len(sFilter) > 0 Then sFilter = sFilter & " AND "
  sfilter = sfilter & " Month([Submitted Date]) = " & Me.Month      <-- this line is red like there might be something wrong.
End If

Me.Filter = sFilter
Me.FilterOn = True
End Function

I'd STRONGLY encourage you to NOT use spaces in your table names, especially if you're working with a server-based backend. Instead of Submitted Date, name your column DateSubmitted - or better yet dtDateSubmitted, which immediately identifies your column as a DateTime column (i.e. the "dt" prefix). Look up "naming conventions" online for a thorough discussion of this concept.

Also use naming conventions in your Control names. Your combo name "Year" is a reserved word in Access, Jet and pretty much every other database engine out there, and can cause some issues if not handle properly. Instead of Year, name you combo something like cboYear, or cmbYear or something like that. The method you use is not as important as your consistent use of that method - that is no matter what naming convention you use, just make sure you USE it. You'll be glad you did, since this sort of coding error, which seems minor to many people, can have some significant impact as your project grows.

 

by: markj72000Posted on 2009-08-13 at 08:54:02ID: 25089809

Thanks for your additional input on the naming conventions. I went thru this morning and corrected all of that. I used this guide to sort of give me an idea on how i should proceed.
http://www.mvps.org/access/general/gen0012.htm


Filtering Error Message:

Ok, i keep getting a error when i select one of the Year drop downs.

Compile error: Invalid outside procedure   <- this opens Microsoft Visual Basic Editor and high lights the "0" in the following first line of code:      If Me.cboYear.Column(0) <> "" Then

Here is my naming convention that i have used:

Year Combo Box:
cboYear
cboYear Combo Box also has the following properties:
Row Source: 2008;2009;2010
Row Source type: Value List

Month Combo Box:
cboMonth

Module:
sFilter    I wasn't sure about this one, i had it named something else but after looking at your code i thought this is what i should do.



This is my Module Code:

Option Compare Database

If Me.cboYear.Column(0) <> "" Then
  sFilter = "Year(dtSubmittedDate)=" & Me.cboYear
End If

If Me.cboMonth.Column(0) <> "" Then
  If Len(sFilter) > 0 Then sFilter = sFilter & " AND "
  sFilter = sFilter & "Month(dtSubmittedDate) = " & Me.cboMonth
End If

Me.Filter = sFilter
Me.FilterOn = True
End Function



cboYear After Update Event Procedure Code:

Sub cboYear_AfterUpdate()

  sFilter
 
End Sub

I assumed this was to match the Module name which is why i chose this for the module name.


Thanks Mr Consulting. :)


Mark

 

by: LSMConsultingPosted on 2009-08-13 at 09:10:47ID: 25090023

In regards to this:

Option Compare Database

Function FilterIt()  <<<<<<<<<<<<<<<< Add this line

If Me.cboYear.Column(0) <> "" Then
  sFilter = "Year(dtSubmittedDate)=" & Me.cboYear
End If

If Me.cboMonth.Column(0) <> "" Then
  If Len(sFilter) > 0 Then sFilter = sFilter & " AND "
  sFilter = sFilter & "Month(dtSubmittedDate) = " & Me.cboMonth
End If

Me.Filter = sFilter
Me.FilterOn = True
End Function

--------------------

Did you put the above code in the FORM code module, or in another module? This should be in the Form code module where you need to do the filtering.

You should NEVER name a Module the same as a Procedure inside that module. For example, if I have a Module named "FilterIt", I would NOT name a procedure "FilterIt". Your Modules should be named something like "basFilter" or "modFilter" ... your Procedures are generally named WITHOUT prefixes:

Function FilterIt()
Sub ChangeName()
Function CalculateTotal()
etc etc

 

by: markj72000Posted on 2009-08-13 at 10:02:45ID: 25090523

Ok, we are close. You were correct, i was using the stand alone module and not the Form Module. It took me little time to figure out how to add to a module without using an event but i think i got it now.

The Year sorts perfect but when i choose a month i get a pop message asking me to:
Enter Parameter Value
Jan

I have formatted my list for months like so:
Jan;Feb;Mar;Apr;etc

Also the reset button DOES clear the filter but it DOES NOT clear the selection from the Combo Boxes. Do i need to do some kind of refresh on the combo box or the entire form?

I know this is kind of running long, if you like I'll go ahead and award points and open a new question so i can give you the points you certainly have earned helping me. I would feel better about this if you would let me do this because i just noticed something else about the sort order of the date that i wanted to ask.

 

by: bobby6055Posted on 2009-08-15 at 04:46:53ID: 25105047

markj72000:

somehow the link below is no longer valid:
http://allenbrowne.com/ser-51.htm

 

by: markj72000Posted on 2009-08-15 at 05:22:33ID: 25105140

thanks bobby but I'm not really sure how to use those controls to apply a filter to my form. I have gotten half of this to work with the Year but when i choose Month it asks me to input a value, i finally figured out that if i put in the corresponding month number like when i choose Mar from the drop down it ask for a value and i put in 3 it filters properly but after no changes to the drop downs have any effect on the filter.

thanks for your time. I'll post a new question with a better description of my total need from the filters.

 

by: markj72000Posted on 2009-08-15 at 05:24:22ID: 31614897

thanks you were a great help and gave me a lot of info i needed that i didn't even ask for.

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...