We’d like to remind Forumites to please avoid political debate on the Forum.

This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.

📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
The Forum now has a brand new text editor, adding a bunch of handy features to use when creating posts. Read more in our how-to guide

MS Access, Calculated Date Field

Hi All

This may be to specialized for this forum but I was hoping that someone may be able to help or even point me in the right direction of a forum that would be able to assist

I am developing an application in Access that is using calculated date fields. I have placed the code below in the "control Source" of a control

=IIf(IsNull([StartDate]),DateSerial(Year([Text26])+[Text21],Month([Text26]),Day([Text26])),DateSerial(Year([StartDate])+[Text21],Month([StartDate]),Day([StartDate])))

The code in itself works fine, however, if for any reason, [Text26] or [Text21] are Null I get a response displaying #ERROR. I would like it just to remain blank.

I cant for the life of me figure out how to get around this in this piece of code,

Hope someone can point me in the right direction

Ben

Comments

  • Hmm....It has been a while since I have done any Access programming so I am not entirely sure what I am talking about, however surely if you are trying to work out Year, Month or Day from a NULL field you are going to get an error. 0 is not a valid date in any format and so you cannot work out a year from it. You may need another IF statement saying that if the 26 or 21 are Null then leave the date empty.
  • Benjo_2
    Benjo_2 Posts: 69 Forumite
    Hi there

    That is the problem. I have another calculated field (see below) which rightly sets the value to "" if a control is blank, but i do not know how to incorportate it in the statement above.


    =IIf(IsNull([Text26]),"",DateSerial(Year([Text26])+[Text15],Month([Text26])+[TermM],Day([Text26]+[TermD])))
  • Can you not do a nested if statement?

    So basically:
    =IIf(IsNull([StartDate]),IIf(IsNull([Text26]),"",DateSerial(Year([Text26])+[Text15],Month([Text26])+[TermM],Day([Text26]+[TermD])))), DateSerial(Year([StartDate])+[Text21],Month([StartDate]),Day([StartDate])))
  • Benjo_2
    Benjo_2 Posts: 69 Forumite
    No I tried it and got a message saying,

    "The expression you entered contains invalid syntax, or you need to enclose your text data in quotes"

    Any other ideas?
  • Erm....unfortunately no :( I'm all out of ideas
  • Benjo_2
    Benjo_2 Posts: 69 Forumite
    Cheers for a good effort anyway

    anyone else?
  • PhilCo_2
    PhilCo_2 Posts: 136 Forumite
    Put some ifs at the beginning to make it blank if either Text21 or 26 are blank.

    =IIf(IsNull([text21]),"",IIf(IsNull([text26]),"",IIf(IsNull([StartDate]),DateSerial(Year([Text26])+[Text21],Month([Text26]),Day([Text26])),DateSerial(Year([StartDate])+[Text21],Month([StartDate]),Day([StartDate])))))

    Remember to add the brackets at the end.
  • fergual2
    fergual2 Posts: 179 Forumite
    =Iif(IsNull([StartDate]) and [Text26]>0 and [Text21]>0,DateSerial(Year([Text26])+[Text21],Month([Text26]),Day([Text26])),Iif([Text21]>0,DateSerial(Year([StartDate])+[Text21],Month([StartDate]),Day([StartDate])),Null))

    seems to work here for me
    Cheers
    Al
  • Benjo_2
    Benjo_2 Posts: 69 Forumite
    PhilCo that worked a treat thanks so much.

    I cant believe it was really that easy, I have been looking at that for hours

    Thank you eveybody for all your help.

    Ben
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 353.5K Banking & Borrowing
  • 254.1K Reduce Debt & Boost Income
  • 455K Spending & Discounts
  • 246.6K Work, Benefits & Business
  • 602.9K Mortgages, Homes & Bills
  • 178.1K Life & Family
  • 260.6K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16K Discuss & Feedback
  • 37.7K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.