473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Query criteria for Filtering Date fields: IIF Nested Operators won't work

10
Howdy All,

I have a query that is used for filtering results to be used in a combo box. The query needs to test fields from both a table and then unbound text boxes on the form that also contains the combo box. The fields I am working with are date fields that are formated as Short Date. I have written an IIF statement and placed it within the Criteria section of the Date field that I am trying to filter by. The current IIF statement works ONLY when both the BegDate field and the EndDate fields (these are the unbound fields on the form) are NULL, it does not work when the date fields contain values. Here is the partially working IIF statement:

Like IIf(IsNull([Forms]![frm_CamasData]![BegDate]),IIf(IsNull([Forms]![frm_CamasData]![EndDate]),"*", IIf(IsNull([Forms]![frm_CamasData]![EndDate]),([qry_Add_Events_frm].[Date])>=[Forms]![frm_CamasData]![BegDate],([qry_Add_Events_frm].[Date]) Like IIf(IsNull([Forms]![frm_CamasData]![BegDate]),([qry_Add_Events_frm].[Date])<=[Forms]![frm_CamasData]![EndDate],([qry_Add_Events_frm].[Date]) Between [Forms]![frm_CamasData]![BegDate] And [Forms]![frm_CamasData]![EndDate]))))

If I take parts of the nested statements and use them on their own they work, but they don't when they are part of this statment. For example, if I replace the above statement with:

Between [Forms]![frm_CamasData]![BegDate] And [Forms]![frm_CamasData]![EndDate]

The results for the combo box work fine when I put dates into both the BegDate and EndDate fields.

The same is true for other parts of the original IIF, another example that works fine when its a stand alone statment is:

>=[Forms]![frm_CamasData]![BegDate]

This works as well if I only put in a BegDate value in the unbound BegDate field.

Thus it seems the parts of my IIF statement are correct but they will not work together in the version I have.

Thanks in advance for your help!
Dec 28 '06 #1
8 6373
Howdy All,

I have a query that is used for filtering results to be used in a combo box. The query needs to test fields from both a table and then unbound text boxes on the form that also contains the combo box. The fields I am working with are date fields that are formated as Short Date. I have written an IIF statement and placed it within the Criteria section of the Date field that I am trying to filter by. The current IIF statement works ONLY when both the BegDate field and the EndDate fields (these are the unbound fields on the form) are NULL, it does not work when the date fields contain values. Here is the partially working IIF statement:

Like IIf(IsNull([Forms]![frm_CamasData]![BegDate]),IIf(IsNull([Forms]![frm_CamasData]![EndDate]),"*", IIf(IsNull([Forms]![frm_CamasData]![EndDate]),([qry_Add_Events_frm].[Date])>=[Forms]![frm_CamasData]![BegDate],([qry_Add_Events_frm].[Date]) Like IIf(IsNull([Forms]![frm_CamasData]![BegDate]),([qry_Add_Events_frm].[Date])<=[Forms]![frm_CamasData]![EndDate],([qry_Add_Events_frm].[Date]) Between [Forms]![frm_CamasData]![BegDate] And [Forms]![frm_CamasData]![EndDate]))))

If I take parts of the nested statements and use them on their own they work, but they don't when they are part of this statment. For example, if I replace the above statement with:

Between [Forms]![frm_CamasData]![BegDate] And [Forms]![frm_CamasData]![EndDate]

The results for the combo box work fine when I put dates into both the BegDate and EndDate fields.

The same is true for other parts of the original IIF, another example that works fine when its a stand alone statment is:

>=[Forms]![frm_CamasData]![BegDate]

This works as well if I only put in a BegDate value in the unbound BegDate field.

Thus it seems the parts of my IIF statement are correct but they will not work together in the version I have.

Thanks in advance for your help!

It's difficult to tell what you're trying to do here. There does appear to be a problem with your 2nd nested conditional IIF(ISNULL([EndDate])... You're saying in that 2nd conditional: If the end date is null then output *, otherwise, if the end date is null then...

It looks like this is at least one problem with your conditionals. If you want to continue to use nested conditionals here, I would do something like:

Like IIF(IsNull([BegDate]) AND IsNull([EndDate]), "*", IIF(IsNull([EndDate]), Whatever happens when EndDate is null and BegDate isn't, IIF(IsNull([BegDate]), Whatever happens when BegDate is Null and EndDate isn't, Whatever happens when they're both not null)))
Dec 28 '06 #2
Ragbrai
10
Thanks for the feedback. I have tried your suggestion and put the two IsNull statements together, however when I do this no records are returned. This is the code that combines them:

Like IIf(IsNull([Forms]![frm_CamasData]![BegDate]) and IsNull([Forms]![frm_CamasData]![EndDate]),"*",IIf(IsNull([Forms]![frm_CamasData]![EndDate]),([qry_Add_Events_frm].[Date])>=[Forms]![frm_CamasData]![BegDate],IIf(IsNull([Forms]![frm_CamasData]![BegDate]),([qry_Add_Events_frm].[Date])<=[Forms]![frm_CamasData]![EndDate],([qry_Add_Events_frm].[Date]) Between [Forms]![frm_CamasData]![BegDate] And [Forms]![frm_CamasData]![EndDate])))

Seems like the above (using your suggestion) should work but it doesn't, in fact after changing the code and then updating the form the code is changed by Access to what you see below:

Like IIf(([qry_Add_Events_frm].[Date])=IsNull([Forms]![frm_CamasData]![BegDate]) And ([qry_Add_Events_frm].[Date])=IsNull([Forms]![frm_CamasData]![EndDate]),"*",IIf(IsNull([Forms]![frm_CamasData]![EndDate]),([qry_Add_Events_frm].[Date])>=[Forms]![frm_CamasData]![BegDate],IIf(IsNull([Forms]![frm_CamasData]![BegDate]),([qry_Add_Events_frm].[Date])<=[Forms]![frm_CamasData]![EndDate],([qry_Add_Events_frm].[Date]) Between [Forms]![frm_CamasData]![BegDate] And [Forms]![frm_CamasData]![EndDate])))

As you can see Access is changing my reference from the unbound BegDate and EndDate Fields to the bound DATE field, which is not at all what I want to happen. I can't seem to figure out a way to tell it not to do this.

A little more details may help. On the form I'm working with (frm_CamasData) there are two unbound date fields, one is BegDate and one is EndDate. I want to use these two fields as the basis of the filter criteria for the combo box. What happens is the user inputs date information into either one or both of these fields, then they have the option to select one of three command buttons that Filter the record set based on their input, If only BegDate data is entered then the filter returns all records with dates >= the BegDate value and the EndDate value is set as NULL, If EndDate data is entered but no BegDate then the Filter returns all records with a date value <= EndDate value and sets the BegDate value to NULL. If both Beg and End Date values are entered then the filter returns the records whose date values is between the Beg and End Values. All of these described filters work fine on the form. The problem is that I also want a combo box that can be used for navigation and that ONLY displays the records that have been filtered as described. Getting the correct values to display in the combo box is where the problem is.

This may be a bit complex but I am hopeful that someone out there has some ideas.
Dec 28 '06 #3
Ragbrai
10
Well I figured out how to get the combined IsNull part of the statement to work:

Like IIf(IsNull([Forms]![frm_CamasData]![BegDate])=True And IsNull([Forms]![frm_CamasData]![EndDate])=True,"*",...

However everything after that part won't return any records still, here is a copy of the complete statement but it only works when BOTH the BegDate and EndDate fields are null.

Like IIf(IsNull([Forms]![frm_CamasData]![BegDate])=True And IsNull([Forms]![frm_CamasData]![EndDate])=True,"*",IIf(IsNull([Forms]![frm_CamasData]![EndDate])=True,([qry_Add_Events_frm].[Date])>=[Forms]![frm_CamasData]![BegDate],IIf(IsNull([Forms]![frm_CamasData]![BegDate])=True,([qry_Add_Events_frm].[Date])<=[Forms]![frm_CamasData]![EndDate],([qry_Add_Events_frm].[Date]) Between [Forms]![frm_CamasData]![BegDate] And [Forms]![frm_CamasData]![EndDate])))
Dec 28 '06 #4
Ragbrai
10
One more discovery, I think if we can figure this out then I can use it to fix the above.

If I take the IF statement above and simplify for testing purposes to:

Like IIf(IsNull([Forms]![frm_CamasData]![BegDate])=True And IsNull([Forms]![frm_CamasData]![EndDate])=True,"*",[Forms]![frm_CamasData]![BegDate])

It works but only returns the exact date of the BegDate field if both BegDate and EndDate are Null. If I modify it to:

Like IIf(IsNull([Forms]![frm_CamasData]![BegDate])=True And IsNull([Forms]![frm_CamasData]![EndDate])=True,"*",>=[Forms]![frm_CamasData]![BegDate])

It no longer works for the dates when the Beg and End Date values are not both NULL, and the computer changes the expression to:

Like IIf(IsNull([Forms]![frm_CamasData]![BegDate])=True And IsNull([Forms]![frm_CamasData]![EndDate])=True,"*",([qry_Add_Events_frm].[Date])>=[Forms]![frm_CamasData]![BegDate])

So it seems the problem is how I am dealing with the >=, <=, and Between operators as they are the items that are not working correctly. What should this syntex look like?
Dec 29 '06 #5
One more discovery, I think if we can figure this out then I can use it to fix the above.

If I take the IF statement above and simplify for testing purposes to:

Like IIf(IsNull([Forms]![frm_CamasData]![BegDate])=True And IsNull([Forms]![frm_CamasData]![EndDate])=True,"*",[Forms]![frm_CamasData]![BegDate])

It works but only returns the exact date of the BegDate field if both BegDate and EndDate are Null. If I modify it to:

Like IIf(IsNull([Forms]![frm_CamasData]![BegDate])=True And IsNull([Forms]![frm_CamasData]![EndDate])=True,"*",>=[Forms]![frm_CamasData]![BegDate])

It no longer works for the dates when the Beg and End Date values are not both NULL, and the computer changes the expression to:

Like IIf(IsNull([Forms]![frm_CamasData]![BegDate])=True And IsNull([Forms]![frm_CamasData]![EndDate])=True,"*",([qry_Add_Events_frm].[Date])>=[Forms]![frm_CamasData]![BegDate])

So it seems the problem is how I am dealing with the >=, <=, and Between operators as they are the items that are not working correctly. What should this syntex look like?
Just a thought, but one problem might be with the "IsNull" function, Instead of IsNull(<expr>) = true, try <expr> IS NULL.

I think it's likely that you're having problems because Access sometimes has trouble evaluating expressions of a time/date data type. For the >= piece, try using the syntax > <expr> + 1.

Like IIf([Forms]![frm_CamasData]![BegDate] IS NULL And [Forms]![frm_CamasData]![EndDate] IS NULL,"*",>[Forms]![frm_CamasData]![BegDate]-1

If that doesn't work, move 'Like' inside -

IIf([Forms]![frm_CamasData]![BegDate] IS NULL And [Forms]![frm_CamasData]![EndDate] IS NULL,"Like '*'",>=[Forms]![frm_CamasData]![BegDate]
Dec 29 '06 #6
Ragbrai
10
Thanks for the suggestions but no luck.

The issue really seems to be focused on the operators that I'm using with the IIF statement (between, >=,<=) since if they are not used then thing work (but not how I need them too.
Dec 29 '06 #7
Thanks for the suggestions but no luck.

The issue really seems to be focused on the operators that I'm using with the IIF statement (between, >=,<=) since if they are not used then thing work (but not how I need them too.
I guess if you can't get it to work with the nested IIF operators, set the rowsource of your combo manually with vba in the after update events of your text boxes. In the after update event of the text box, have it call a use this function like this:
Expand|Select|Wrap|Line Numbers
  1. Me.ComboName.Rowsource = strWhereClause()
  2.  
Make sure you have your Row Source Type on the combobox set to "Table/Query." Then make the function:
Expand|Select|Wrap|Line Numbers
  1. Public Function strWhereClause
  2. strWhereClause = "SELECT [Date] FROM [qryName] "
  3.  
  4. Select Case IsNull(Me.BegDate.Value)
  5.  
  6. Case True
  7. If IsNull(Me.EndDate.Value) = True then 
  8. Exit Function
  9. Else
  10. strWhereClause = strWhereClause & "WHERE [Date] <= " & me.EndDate.Value
  11. End If
  12.  
  13. Case False
  14. If IsNull(me.EndDate.Value) = True Then 
  15. strWhereClause = strWhereClause & "WHERE [Date] >= " & Me.BegDate.Value
  16. Else
  17. strWhereClause = strWhereClause & "WHERE [Date] BETWEEN " & Me.Begdate.Value & " AND " & Me.EndDate.Value
  18. End If
  19.  
  20. End Select
  21. End Function
  22.  
Dec 29 '06 #8
NeoPa
32,556 Expert Mod 16PB
Does this get you anywhere?
Expand|Select|Wrap|Line Numbers
  1. Between Nz(Forms!frm_CamasData.BegDate,#1/1/1900#) 
  2.     And NZ(Forms!frm_CamasData.EndDate,#12/31/9999#)
Dec 30 '06 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Enterprise | last post by:
I'm stuck. I use Access 2000. I have a date field in a query. What I need done is if a Limit_Date function returns a true, I want the date field to be Between 1/1/02 and 1/1/03, otherwise I want it...
2
by: Matthew | last post by:
Hey , I have built a query which takes values from unbounded fields on a form, and it all works except for one thing. I have a few fields in my query that are dates. I also have a start and...
2
by: Mark Roughton | last post by:
I have a form where the users need to view records for various criteria, one of which is a date field on which they may wish to view all related data for the selected date, for all dates upto and...
2
by: jennwilson | last post by:
I am trying to generate a report based on a query that will list any records where an individual has a date listed that matches the specified time for one or both of the date fields. The two fields...
1
by: fishing | last post by:
Hi, I am a total novice at access. I have a table which has fields such as rep, client, work, week 1, week 2, week 3, week 4 etc. In the week fields I put in dates. i.e week 1 is the first week...
6
craigfr
by: craigfr | last post by:
I am making a graph comparing last year's defect data with YTD defect data. Our fiscal year starts Nov.1 and ends Oct.31. To get the YTD, I started used a simple date serial criteria: Between...
2
by: cmartin1986 | last post by:
First of all I want to thank all of you that have helped me in the past this is an awesome fourm. My problem today is I have a database that builds charts that are viewed by a large group every...
17
by: sharsy | last post by:
Hello guys, I would like some help in generating query criteria that will identify credit cards that have expired on an access database. The specific Field is formatted with a Data Type of...
5
by: hbaf208 | last post by:
I have an option group (TimeFrameOption) with three options: All, Last 6 months, and Last 12 Months. When an option is selected, I want to query dates in a listbox (CallReport) to display all dates...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.