Forum Discussion

Tony2021's avatar
Tony2021
Steel Contributor
Aug 20, 2022

Using IIF and Date()

Experts,

 

I am using IIF to evaluate whether or not a date [InvDueDate] is between the criteria below.  It is not evaluating correctly in all cases since it returns "Payment is Pending" even when [InvDueDate] is outside the criteria.  I seem to remember that IIF is not the best way to do this.  Maybe there is a better way to write the expression (DateSerial or CDate?). 

 

Any help is appreciated.  

the below is in a text box on a report. 

=IIf([txtInvDueDate] Between Date() And Date()+20,"Payment is Pending","")

 

I have also tried the below using DateAdd but the results are the same:

=IIf([txtInvDueDate] Between Date() And DateAdd("d",20,Date()),"Payment is Pending","")

  • That tells you the result is the wrong datatype. The control is expecting one datatype, but the expression is returning a different datatype.

    And, with that, I see my mistake. You want to put either a valid date or a text string ("Payment is Pending") in that control. You can't put text into a date field. You'll have to present an alternative way to signal to the user that the payment is pending, a control that does accept text.
  • George_Hepworth's avatar
    George_Hepworth
    Silver Contributor
    You have to account for the fact that Date() generates a date with time of 00:00 AM. All dates, in fact, do have both date and time, but sometimes the time is not specified so it defaults to midnight.

    Now, if the value in that Invoice Due Date field is also a date with a time, and that time is NOT 00:00AM, i.e. not midnight, it could fall outside your criteria by the number of hours in that time component.

    Is that possibly the reason for this problem?

    If I were doing this, I would indeed want to account for that by using DateValue() on the InvoiceDueDate field to limit the comparison to the date, without the times.
    • Tony2021's avatar
      Tony2021
      Steel Contributor

      George_Hepworth 

      HI George, I cant say for sure if it has something to do with as you describe above. I looked at it further and I forgot I am using a Union and on one of the queries I put "" as InvDueDate for the column since the field InvDueDate is not in that table. I think its termed "dummy" column.


      Below are the columns in the Union. I am only taking a snippet of the entire code. I am not sure if its advisable to use "" as I did below but I think I must since the field is not in the table. 
      this is the first query in the union:
      , "" AS InvDueDate,
      this is the first query in the union:
      ,tblFXRollsChild.InvDueDate,
      The correct invoice date is being returned by the Union though so to me using "" is not the issue (but it could be...see far below DateValue part) since its referring to the correct invoice date.

       

      I dont have a format placed on the field.  I do nto see a time being added to the date in the datasheet display so to me it also doesnt seem likely its due to the time as you touched on above.  

       

      Using DateValue()

      I might be on to something. 

      =IIf(DateValue([txtInvDueDate]) Between Date() And Date()+20,"Payment is Pending","")

      It returned #Type on some of the records. 

      It ight have something to do with me assigning "" to the InvDueDate? 

      What else can I do than assign a "" for a dummy column?  

       

      • arnel_gp's avatar
        arnel_gp
        Steel Contributor
        although solved, you can still use expression like this:

        =IIf(CDate("0" & [txtInvDueDate]) Between Date() And Date()+20,"Payment is Pending","")

Resources