If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > PC based Database Applications > Microsoft Access > Filtering form by date

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-03-12, 10:54
rwest rwest is offline
Registered User
 
Join Date: Dec 2011
Posts: 9
Filtering form by date

Hello everyone,
I have a form that is connected to a table called floods that has a date column as mm/dd/yyyy. I want to filter my form but only by the year. When I try to filter it nothing happens, i don't even get an error. Below is the code if anyone could help in this matter it would be greatly appreciated.
Thanks

Code:
Private Sub FindYear_AfterUpdate()

Me.FindYear = IIf(IsDate(Me.FindYear), _
Format(Me.FindYear, " yyyy"), _
"")


Dim strFilter As String, strOldFilter As String

strOldFilter = Me.Filter

'txtFind - Date
If Me.FindYear > "" Then _
strFilter = strFilter & _
" AND ([eventdatestart]=" & _
Format(CDate(Me.FindYear), _
"\#m/d/yyyy\#") & ")"
If strFilter > "" Then strFilter = Mid(strFilter, 6)
If strFilter <> strOldFilter Then
Me.Filter = strFilter
Me.FilterOn = (strFilter > "")
End If
End Sub
Reply With Quote
  #2 (permalink)  
Old 01-03-12, 12:13
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,446
Check what you have in "strFilter" after the line:
Code:
If strFilter > "" Then strFilter = Mid(strFilter, 6)
If "eventdatestart" is defined as Date/Time, filtering by year means (with 'YYYY' being the chosen year):
Code:
eventdatestart BETWEEN #1/1/YYYY# AND #12/31/YYYY#
and this only if the Time part is set to '00:00:00' (or is not stored), otherwise the upper limit must be: '#12/31/YYYY 23:59:59#', or you must change the comparison to:
Code:
eventstart >= #1/1/YYYY# AND eventstart < #1/1/YYYY+1#
__________________
Have a nice day!
Reply With Quote
  #3 (permalink)  
Old 01-03-12, 12:34
rwest rwest is offline
Registered User
 
Join Date: Dec 2011
Posts: 9
So i changed the eventdatestart so that it is between the first and end of the year but the code still does not work. What do you mean by Check what I have in "strFilter" after the line..?
Reply With Quote
  #4 (permalink)  
Old 01-03-12, 12:42
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,446
Set a breakpoint on the line following it (F9 key), when the code halts open the debug window (Ctrl+G) then in this debug windows type:
Code:
debug.print strFilter
and hit the Enter key. The value of strFilter will be printed on the next line in the debug window.
__________________
Have a nice day!
Reply With Quote
  #5 (permalink)  
Old 01-03-12, 13:02
rwest rwest is offline
Registered User
 
Join Date: Dec 2011
Posts: 9
I did this and it highlighted this: If strFilter > "" Then
I don't know what that means. If you could help again that would be great.
Thanks
Reply With Quote
  #6 (permalink)  
Old 01-03-12, 13:42
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,446
The line where to place the breakpoint is:
Code:
If strFilter <> strOldFilter Then
What was displayed in the debug window?
__________________
Have a nice day!
Reply With Quote
  #7 (permalink)  
Old 01-03-12, 14:02
rwest rwest is offline
Registered User
 
Join Date: Dec 2011
Posts: 9
nothing was displayed, it just highlights the line.
Reply With Quote
  #8 (permalink)  
Old 01-03-12, 14:40
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,446
This cannot be if you opened the Debug window and typed "debug.print strFilter" into it, except if "strFilter" is an empty string.
__________________
Have a nice day!
Reply With Quote
  #9 (permalink)  
Old 01-04-12, 10:34
rwest rwest is offline
Registered User
 
Join Date: Dec 2011
Posts: 9
Well I did what you said so I guess it is an empty string. Do you know what causes that or how I can fix it? because I do not know how that happened.
Thanks for all your help.
Reply With Quote
  #10 (permalink)  
Old 01-04-12, 18:57
Sinndho Sinndho is offline
Registered User
 
Join Date: Mar 2009
Posts: 3,446
In the code you posted, there is a condition to be met before assigning something to strFilter:
Code:
If Me.FindYear > "" Then strFilter = strFilter & " AND ([eventdatestart]="  & Format(CDate(Me.FindYear), "\#m/d/yyyy\#") & ")"
Obviously, it is not. What does Me.FindYear contain?
__________________
Have a nice day!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On