Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

MS Access, VBA Filter on Date field including Nulls

Posted on 2013-01-21
7
Medium Priority
?
714 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 61

Accepted Solution

by:
mbizup earned 1000 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 49

Assisted Solution

by:Dale Fye
Dale Fye earned 1000 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 49

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Microsoft Access has a limit of 255 columns in a single table; SQL Server allows tables with over 255 columns, but reading that data is not necessarily simple.  The final solution for this task involved creating a custom text parser and then reading…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

885 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