Solved

MS Access, VBA Filter on Date field including Nulls

Posted on 2013-01-21
7
683 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error can't find table ?? 5 24
MYSQL responding very slow 3 21
Database keeps telling me Recordset Locked 10 21
2 subforms 1 main form 1 13
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Familiarize people with the process of utilizing SQL Server stored procedures from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Micr…
In Microsoft Access, learn the trick to repeating sub-report headings at the top of each page. The problem with sub-reports and headings: Add a dummy group to the sub report using the expression =1: Set the “Repeat Section” property of the dummy…

856 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