Solved

Cdate in query

Posted on 2008-06-25
2
1,950 Views
Last Modified: 2008-06-25
I amusing the function Cdate in my query and continue to get #Error for some records that cannot compute a date even if I use IsError function.  

MatDate: IIf(IsError(CDate(Mid([Maturity_Date],5,2) & "/" & Right([Maturity_Date],2) & "/" & Left([Maturity_Date],4))),0,1)

To make this simpler, I just want it to result in 0 or 1.  0 should display if there is an error, and 1 if not.  However, I only get 1s and a #Error instead of 0.  What can I do?
0
Comment
Question by:mak345
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
2 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 125 total points
ID: 21866433
A better idea would be to write this as a VBA function, which can handle graceful error handling and if-thens in a somewhat readable way, then call that function from your query:

Your query:
MatDate: MyFunction(Maturity_Date)

The function:
Public Function MyFunction(sString as String) as string

On Error Goto eh

If IsNull(Nz(sString, "") = "" then
   'sString is either null or empty.  Bail here.
   MyFunction = 0
   goto ex
End If

If IsDate(Mid([Maturity_Date],5,2) & "/" & Right([Maturity_Date],2) & "/" & Left([Maturity_Date],4)) then
   'Rock and roll.
   MyFunction = 1
   goto ex
Else
   'sString is a value, but can not be formatted as a date correctly.
   MyFunction = 0
   goto ex
end if

ex:
  exit function

er:
  MyFunction = 0
  msgbox "An error occured in MyFunction: " & err.Number & ", " & err.Description
  goto ex

End Function


0
 
LVL 66

Expert Comment

by:Jim Horn
ID: 21866441
btw I recommend against using IsError() in query expressions.  Just too many possibilities, no graceful way of dealing with the error, and bad data can flow downstream.
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
This post contains step-by-step instructions for setting up alerting in Percona Monitoring and Management (PMM) using Grafana.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

617 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question