Solved

MS Access, VBA Filter on Date field including Nulls

Posted on 2013-01-21
7
698 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Forrester Webinar: xMatters Delivers 261% ROI

Guest speaker Dean Davison, Forrester Principal Consultant, explains how a Fortune 500 communication company using xMatters found these results: Achieved a 261% ROI, Experienced $753,280 in net present value benefits over 3 years and Reduced MTTR by 91% for tier 1 incidents.

 
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 48

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 48

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It’s the first day of March, the weather is starting to warm up and the excitement of the upcoming St. Patrick’s Day holiday can be felt throughout the world.
The Windows Phone Theme Colours is a tight, powerful, and well balanced palette. This tiny Access application makes it a snap to select and pick a value. And it doubles as an intro to implementing WithEvents, one of Access' hidden gems.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

710 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