Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Date formatting and IIf statement 1

Status
Not open for further replies.

RacerGirl117

Technical User
Sep 25, 2002
234
US
Hi! I posted this in the Microsoft Access: Queries forum, but thought I’d post it here as well.

I have a form (frmSvcWarrInfoEquipSub) based on a query (qrySvcWarrInfoEquipSub). For a little history on what's going on with this here you go…

The data being pulled into the table on which the query is based comes from an external (non-Access) data source via Pervasive. The software the information is coming from is Real World accounting software. When the dates come in they appear like this - 20030410.

I want to reformat the date to look like this - 4/10/30. I have the below formatting statement in one of the fields where there is a date and it works just fine - as long as there is a date in the field.
LastRepDate: CDate(Left([SV_CEQ_LAST_REPAIR_DATE],4) & "/" & Mid([SV_CEQ_LAST_REPAIR_DATE],5,2) & "/" & Right([SV_CEQ_LAST_REPAIR_DATE],2))

If there is no date (i.e. the field is null) I want the field to display "N/A". So I was going to put the following IIf statement at the end of the formatting statement
IIf(IsNull([SV_CEQ_LAST_REPAIR_DATE]),"N/A",Null)

so that in the end the complete statement looks like
LastRepDate: CDate(Left([SV_CEQ_LAST_REPAIR_DATE],4) & "/" & Mid([SV_CEQ_LAST_REPAIR_DATE],5,2) & "/" & Right([SV_CEQ_LAST_REPAIR_DATE],2)IIf(IsNull([SV_CEQ_LAST_REPAIR_DATE]),"N/A",Null))

but it keeps telling me I have invalid sytax. If I put a "," between the ) and the IIf, or a ( between those or anothe ) after the 2 and a ( before the IIf it gives me the invalid syntax message and tells me that I need to enclose the text data in quotes.

Is this combination of statements even possible? If so, what am I doing wrong here?If I put
LastRepDate: CDate(Left([SV_CEQ_LAST_REPAIR_DATE],4) & "/" & Mid([SV_CEQ_LAST_REPAIR_DATE],5,2) & "/" & Right([SV_CEQ_LAST_REPAIR_DATE],2) Or IIf([SV_CEQ_LAST_REPAIR_DATE]=0,"N/A",[SV_CEQ_LAST_REPAIR_DATE]))

it works, but it returns 12/29/1899 in all the records. ???

If I do the two statements as separate fields, one LastRepDate and one RepDate they work fine. But I really want to avoid having to create another query just so I can get the two fields into one.

Your help would be greatly appreciated.
Jessica Morgan
Fire Fighter Sales & Service Co.
Pittsburgh, PA
 
Jessica,

See if this makes sense:
Code:
LastRepDate: IIf([SV_CEQ_LAST_REPAIR_DATE]=0,"N/A",CDate(Left([SV_CEQ_LAST_REPAIR_DATE],4) & "/" & Mid([SV_CEQ_LAST_REPAIR_DATE],5,2) & "/" & Right([SV_CEQ_LAST_REPAIR_DATE],2)))
The parens might be off.......
 
Cosmo,

You are my hero! It never occured to me to do the IIf first. :) I was thinking that the formatting should be done first, but I guess it makes more sense this way. You want to assign any values before you try and format it. Makes sense.

Thanks bunches! Jessica Morgan
Fire Fighter Sales & Service Co.
Pittsburgh, PA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top