Avoiding #ERROR without using NZ()? (1 Viewer)

Sketchin

Registered User.
Local time
Today, 00:35
Joined
Dec 20, 2011
Messages
575
I have a field that calculates a date range from a record in my query.

Date: Format([TransactionDate],"yyyy") & "-" & Format([transactionDate],"yy")+1

My problem is that if TransactionDate is blank, it returns #ERROR. I am linking this query to Excel, which means I cannot use the NZ() function to return a null value. Is there any way to do this without NZ()?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:35
Joined
Aug 30, 2003
Messages
36,127
Is the field not Null then? You can use an IIf() and test the length of the field with Len().
 

Sketchin

Registered User.
Local time
Today, 00:35
Joined
Dec 20, 2011
Messages
575
Is the field not Null then? You can use an IIf() and test the length of the field with Len().

The TransactionDate field is null in some records.

So it would look something like?:

Date: IIf(Len(Format([TransactionDate],"yyyy") & "-" & Format([transactionDate],"yy")+1)=0," ",(Format([TransactionDate],"yyyy") & "-" & Format([transactionDate],"yy")+1))

In the IIF statement, how do I tell it to be NULL instead of #ERROR?
 

Sketchin

Registered User.
Local time
Today, 00:35
Joined
Dec 20, 2011
Messages
575
Here is a picture of the query as it stands.
 

Attachments

  • Commitments.jpg
    Commitments.jpg
    98.3 KB · Views: 33

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:35
Joined
Aug 30, 2003
Messages
36,127
More like:

Date: IIf(Len([TransactionDate])=0," ",(Format([TransactionDate],"yyyy") & "-" & Format([transactionDate],"yy")+1))
 

Sketchin

Registered User.
Local time
Today, 00:35
Joined
Dec 20, 2011
Messages
575
Still doesn't work so I created a field called TransactionDateLength: Len([TransactionDate]) and it returns either a 9 or a null, but not a zero. Think that's the problem?
 

Sketchin

Registered User.
Local time
Today, 00:35
Joined
Dec 20, 2011
Messages
575
Alright, I created the TransactionDateLength field and used the LEN() function on that one, instead of using transactiondate and it worked.

Thanks for pointing me in the right direction
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:35
Joined
Aug 30, 2003
Messages
36,127
Then I'm curious why Nz didn't work. Try

Date: IIf(Len(Nz([TransactionDate], ""))=0," ",(Format([TransactionDate],"yyyy") & "-" & Format([transactionDate],"yy")+1))

Or I suppose you could reverse it and test for 9.
 

Sketchin

Registered User.
Local time
Today, 00:35
Joined
Dec 20, 2011
Messages
575
NZ works if you are viewing the query in Access, but not if you are linking it to Excel. Excel doesn't know what to do with NZ.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:35
Joined
Aug 30, 2003
Messages
36,127
I'm looking at an Access query against a linked Excel file and it has Nz() in it. In any case, how about:

Date: IIf(Len([TransactionDate])=9, Format([TransactionDate],"yyyy") & "-" & Format([transactionDate],"yy")+1, "")

If I haven't goofed the parentheses.
 

Sketchin

Registered User.
Local time
Today, 00:35
Joined
Dec 20, 2011
Messages
575
Yes, that worked.

In my past experience, I haven't been able to get a query to show up in Excel if it is using functions that Excel doesn't understand.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 00:35
Joined
Aug 30, 2003
Messages
36,127
I was looking at it in Access, so likely I misunderstood how you were using it.
 

Users who are viewing this thread

Top Bottom