Solved

MS Access, VBA Filter on Date field including Nulls

Posted on 2013-01-21
7
704 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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
Dale Fye 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
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

AutoNumbers should increment automatically, without duplicates.  But sometimes something goes wrong, and the next AutoNumber value is a duplicate.  This article shows how to recover from this problem.
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

627 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