Solved

MS Access, VBA Filter on Date field including Nulls

Posted on 2013-01-21
7
676 Views
Last Modified: 2013-01-21
Hi,

I have a form which I would like to filter on a date field, and the results to include any Nulls.

The code below returns 0 records, yet when I split the filter, the two halves work correctly.

Private Sub Btn_Filter_Review_1_Week_Click()

Me.Filter = "[last_reviewed_Date]  <= DateAdd (" & Chr(34) & "m" & Chr(34) & ",-1,Date()) " _
& " AND [last_reviewed_date] is Null"
Me.FilterOn = True
Me.Requery

End Sub


Any help greatly apreciated.
Many thanks
Simon
0
Comment
Question by:Jamist
  • 3
  • 2
  • 2
7 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 38803377
Try this  -- using OR:
Me.Filter = "[last_reviewed_Date]  <= DateAdd (" & Chr(34) & "m" & Chr(34) & ",-1,Date()) " _
& " OR [last_reviewed_date] is Null"

Open in new window

0
 

Author Comment

by:Jamist
ID: 38803421
Hi,
Sorry, I simplified the above code too much before posting.  The actual code below is designed to filter a set of records that have been filtered already.  By using the OR operand, this re-introduces all the records that where previously filtered.
Private Sub Btn_Filter_Review_1_Week_Click()

Me.Filter = Me.Filter & " AND [last_reviewed_Date]  <= DateAdd (" & Chr(34) & "m" & Chr(34) & ",-1,Date()) " _
& " AND [last_reviewed_date] is Null"
Me.FilterOn = True
Me.Requery


End Sub

Open in new window

0
 
LVL 61

Expert Comment

by:mbizup
ID: 38803433
You'll still need to use OR...

But group your ands and ors the same way you would group multiplication and addition, respectively - using parentheses:


Me.Filter = "(" & Me.Filter & " AND [last_reviewed_Date]  <= DateAdd (" & Chr(34) & "m" & Chr(34) & ",-1,Date()) " _
& ") OR [last_reviewed_date] is Null"

Open in new window

0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 61

Accepted Solution

by:
mbizup earned 250 total points
ID: 38803437
If that doesn't work, try this:

Me.Filter =  Me.Filter & " AND ([last_reviewed_Date]  <= DateAdd (" & Chr(34) & "m" & Chr(34) & ",-1,Date()) " _
& " OR [last_reviewed_date] is Null)"

Open in new window

0
 
LVL 47

Assisted Solution

by:Dale Fye (Access MVP)
Dale Fye (Access MVP) earned 250 total points
ID: 38803443
Try something like:

Me.Filter = Me.Filter _
               & " AND cDate(NZ([last_reviewed_Date],Date()-32))  <= DateAdd ('m',-1,Date())"

By using the nz() function, and providing a date that is more than a month ago, if the field is NULL, you will get all those records where [Last_reviewed_date] is greater than or equal to a month ago, and will also get all values where [Last_reviewed_Date] are NULL
0
 

Author Closing Comment

by:Jamist
ID: 38803539
Hi,

Both of the two solutions below worked, and filtered all dates older than 1 month, and included null values.

Me.Filter =  Me.Filter & " AND ([last_reviewed_Date]  <= DateAdd (" & Chr(34) & "m" & Chr(34) & ",-1,Date()) " _
& " OR [last_reviewed_date] is Null)"

Open in new window

or
Me.Filter = Me.Filter _
               & " AND cDate(NZ([last_reviewed_Date],Date()-32))  <= DateAdd ('m',-1,Date())"

Open in new window

I have split the points between both authors, I hope this is acceptable to both.

Thank you for your help.
Simon
0
 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 38803799
Jamist,

The point is not whether it is acceptable to us, but that it works for you.  If you learned from both posts, then split the points however you want.  

When I ask a question and get multiple correct answers, I generally look at it and decide which solution I'm going to use, and award a few more points to that answer, then award the rest to the other solution (s).

Glad we could help.

Just a note about using the NZ() function.  When you use it in VBA, the result you will get is of the same datatype as the value you provide to the function.  But when you use NZ() in a query, the result will always be a string.  For that reason, you should always explicitly type the result using one of the type conversion functions (cdate, cint, cdbl, ...).
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now