473,406 Members | 2,208 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,406 software developers and data experts.

FIltering on a form using just the year from a date field

I have created two unbound boxes on a form that users can enter name [cbonamefilter] and date [cmboyearfilter]. I want the form to filter by name and the "YYYY" part of the date. I have researched the forum and found most relative responses and tried to use suggested coding. The name portion of the filter works great but I'm having trouble getting the year part to work. I was successful when I was entered an exact date but not for just the year portion. I'm new to VBA so I'm not sure what I am missing. I get a runtime error when I try to run the code below.
Expand|Select|Wrap|Line Numbers
  1. Private Sub cbonamefilter_AfterUpdate()
  2. Call CheckFilter
  3. End Sub
  4. Private Sub cmboyearfilter_AfterUpdate()
  5. Me!cmboyearfilter = IIf(IsDate(Me!cmboyearfilter), _
  6.                                   Format(Me!cmboyearfilter, "mm/dd/yyyy"), _ "")
  7.  
  8. Call CheckFilter
  9. End Sub
  10.  
  11. Private Sub CheckFilter()
  12.  
  13. Dim strFilter As String, strOldFilter As String
  14. strOldFilter = Me.Filter
  15.  
  16. 'cbonamefilter - text
  17. If Me!cbonamefilter > "" Then _
  18. strFilter = strFilter & _
  19. " AND ([Initials-Pre] Like '" & _
  20. Me!cbonamefilter & "*')"
  21.  
  22. 'cmboyearfilter - date
  23. If Me!cmboyearfilter > "" Then _
  24. strFilter = strFilter & _
  25. " AND ([PreCal Date] BETWEEN #1/1/yyyy# AND #12/31/yyyy#=" & _
  26.                      Format(CDate(Me!cmboyearfilter), _
  27.                             "\#mm/dd/yyyy\#") & ")"
  28.  
  29.  
  30.  
  31. If strFilter > "" Then strFilter = Mid(strFilter, 6)
  32.      If strFilter <> strOldFilter Then
  33.          Me.Filter = strFilter
  34.          Me.FilterOn = (strFilter > "")
  35.      End If
  36.  End Sub
  37.  
Thanks for any help
Jul 2 '15 #1
4 1493
jforbes
1,107 Expert 1GB
I think this is what you are looking for:
Expand|Select|Wrap|Line Numbers
  1. strFilter = strFilter & _
  2.  " AND (YEAR([PreCal Date])=" &  Me!cmboyearfilter & ")"
Jul 2 '15 #2
Well that stopped the runtime error but no records are returned no matter what date is entered. Maybe I have to do something to make the [cmboyearfilter] formatted to a year as well?

I used the same logic as above and I think I got it. Thanks.
Jul 2 '15 #3
jforbes
1,107 Expert 1GB
I have to admit that I didn't read through all of the code. I just saw the point where the SQL was being created and so I assumed the that Me!cmboyearfilter would be just an integer to represent the Year. Glad it all worked out for you.
Jul 3 '15 #4
zmbd
5,501 Expert Mod 4TB
perhaps this will guide you, slightly different application with the grouping instead of with a cbobx; however, note how the SQL is written to return month and year from the data...
http://bytes.com/topic/access/answer...th#post3726215
you should be able to modify the concept to work as the recordsource for your cbobx
Jul 12 '15 #5

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

Similar topics

5
by: Bayla Frankl | last post by:
Hi all, I am a little stumped. I have a query I am trying to run to retrieve the last Progress Note record from the database for the current patient so that the therapists can see the last note...
7
by: Katherine | last post by:
I'm trying to filter the records on the mainform (MailingList) of my database using a field contained in a subform (Donations). I was basing my code off Allen Browne's Access Tips page (here:...
2
by: jacc14 | last post by:
Hi Hope there is someone out there that can help. I am sure this is an easy one although not easy to explain. I have a form which produces a report using a query. On the form I have a start and...
1
by: Simon | last post by:
Dear reader, Is there a function or VBA code available to find the week number out of a date field. You can find the year with Year(date field) but now I need the week number out of a...
1
by: klove1209 | last post by:
Good afternoon, I am currently working on this unbounded form that has numerous date fields. I wanted to know if it is possible to save data in a form, with a null date field. I am currently...
10
by: Jes | last post by:
Dear all I have a date field on a HTML form where the user is asked to key in dd/mm/yyyy However, when that is written to MySql it is either not accepted or another value is tored in the...
15
by: bluemoon9 | last post by:
hello everyone! I would like to add a calenda option onto the form next to the Date field, so that user can just select the date from the calendar instead of typing the date, can someone help? ...
7
by: prashantdixit | last post by:
Hi, I am beginner and i would like to open a report using where clause. Now i have to merge two condition in where clause 1. = Me!ID AND 2. is Not Null The code i have written is Dim...
10
by: Phil Frankel | last post by:
Hi all, I am having trouble with VB6 (running in windows7) talking to an Access database, and was hoping someone could help me locate the error. the database contains a "DateIn" field of type...
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
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
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,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.