Results 1 to 12 of 12
  1. #1
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904

    Formula: Return Null Value while Maintaining Format

    I am doing a DLOOKUP calculation in a query, where if my record is of a certain type (based on a field setting), I want to lookup a date from a different table. If it is not of that type, I want to return a Null value (I do not want to return any date).


    I am using DLOOKUP, because if I setup a LEFT JOIN query, my query becomes un-updateable, and it needs to be updateable, since it is the Source of an entry Form.

    The issue is, if I try structuring my formula like this:
    =IIF([Field1]="Annual",DLOOKUP([DateField1],...),Null)

    The value it return is Text (even the Date Values it returns). I need it to return a Date Value, since it is being used in Due Date calculations.
    If I replace "Null" with some dummy date, like DateValue("1/1/1900"), then all the fields it returns are date, but I do not want to display this date on my Form, nor should it be used in my calculations.

    I suppose a simple workaround would be to use Conditional Formatting and format all 1/1/1900 dates to be the same color as the background so it is essentially "invisible", but I would prefer not to have to do this, if possible.

    Is there any easy way to do what I want (without making a complex workaround)?

  2. #2
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    I would try
    Code:
    =IIF([Field1]="Annual",DLOOKUP([DateField1],...),"")
    
    or
    
    =IIF([Field1]="Annual",DLOOKUP([DateField1],...),Empty)
    I did not test this, but if the value returned is text, either should work.
    (There is not a space between the quotes)

  3. #3
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    No, neither of those worked. I think you misunderstood the question.

    I do NOT want to return anything in Text format, that is exactly what I am trying to avoid.
    I want to return a valid Date value or nothing. Using Null, "", or "nothing at all seems to coerce all returned values to be Text, not Dates.

    If you try it, you'll see what I mean. All the non-empty values being returned are left justified, indicating they are returning Text and not a valid Date value.

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    I was thinking "" too.

    Maybe you can catch the query in a VBA module and break out the IIF statement there. Analyze it in a string and the pass it to a date variable.

    Then you can assign the date variable to the field.

  5. #5
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Maybe you can catch the query in a VBA module and break out the IIF statement there. Analyze it in a string and the pass it to a date variable.
    I would consider it that to be a "complex workaround", which I am trying to avoid (since I already have a simple workaround, if need be). I am not even sure how that would work anyhow, since the value needs to be passed back to a query, and once you did that, I think you may run into the exact same issue I am having now.

    I was just hoping that there might be a simple solution that eludes me.

  6. #6
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Oops, I saw the "= IIF...." and thought it was a formula in an unbound control.

    OK, so I threw together a test dB.
    I used this in the query:
    Code:
    CalcDate: IIf([Field1]="Annual",CDate(DLookUp("[DateField1]","DateLookup","[TestField]=6")),"")
    then in the form, for the text box control bound to "CalcDate", set the "Format" property to "Short Date".

    What say you???

  7. #7
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Oh wow, that returns all sort of errors on certain records.
    I think I see what is going on now. I think there is a complicating factor that might be muddling things up.
    The issue is this. In the Date field that the DLOOKUP is return, some of those dates are empty or Null.

    So some of the empty/Null values are coming from the DLOOKUP part of the formula, and others are coming from the IIF part of the formula.

    I am beginning to think that the two most straightforward workarounds are:
    1. Use a dummy date and Conditional Format it to hide it, like I mentioned in my first post
    - or -
    2. Allow it to return as Text, and just have to use the CDATE function on values that aren't blank at the point where I want/need to use the value in a calculation.

  8. #8
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    It sounds like you got this handled.

    I often times use a function that when called checks for 0 or Null or "". It simply returns true or false.

    Regardless of how you proceed it seems you will be making a couple of trips to the data. Sorry I could not of been better help.

  9. #9
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Yes, unfortunately I find that most times that when I think I may be overlooking something obvious, I usually am not. Unfortunately, Access does have its limitations that we need to work around sometimes.

    Thanks for the suggestions anyway.

  10. #10
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    You might be able to use the IsDate() function in the formula, but it is going to get messy.

    A) You might check out ELookup() by Allen Browne http://allenbrowne.com/ser-42.html

    B) You could write your own function to do the lookup. (my preference)

    2. Allow it to return as Text, and just have to use the CDATE function on values that aren't blank at the point where I want/need to use the value in a calculation.
    Nothing wrong with this.... and easier in some ways.

  11. #11
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Steve:

    A) Thanks for this link. I did notice the queries performance takes a severe hit when I use DLOOKUP, so maybe this will help.

    B) I thought about creating my own function, but an unsure how to create a function that would be any more efficient than using DLOOKUP/ELOOKUP (no sense in recreating the wheel if it is not any better). Do you have any tips on that?

    Thanke

  12. #12
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    - Your own function (IMO) would be more efficient than DLOOKUP().

    - ELOOKUP() uses recordsets and is how I would approach the problem.
    Maybe try this first to see how ELOOKUP() functions. (no pun intended)

    - Writing your own function allows you to customize your return value.


    I have some queries with (my custom) functions, that call (more custom) functions, and sometimes call another (custom) function. Opening a report that has ~4000 main records that has to do calculations from a table that has several hundred thousand records only takes about 25 seconds. A long time ago, with a much smaller record set, using DLOOKUP() would take upwards of 8 minutes to get the report.

    Your mileage may vary

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 06-03-2013, 11:02 AM
  2. Return a value of zero for null queries
    By adacpt in forum Access
    Replies: 2
    Last Post: 01-22-2012, 07:41 PM
  3. Return Null as Zero
    By bbshriver in forum Reports
    Replies: 12
    Last Post: 10-19-2010, 01:49 PM
  4. No return on Null values
    By forrestapi in forum Queries
    Replies: 4
    Last Post: 10-18-2010, 08:09 AM
  5. Return 0 instead of null
    By salisbut in forum Queries
    Replies: 1
    Last Post: 08-07-2010, 12:01 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums