Conditional Change to Date Query

danmack

Registered User.
Local time
Today, 23:48
Joined
Oct 30, 2013
Messages
21
Hi all,

I need to clean up a date field (I hate dates in access!). The field in the table (imported from a spreadsheet) has records where users have simply entered a date in the correct format and then others where users feel obliged to add comments after the date rather in the 'Comments' section. Therefore Cdate alone won't work.

So far i have tried:

Code:
IIf(Len([TabDL].[PROMISE]<>10),#1/1/1900#,CDate([TabDL].[PROMISE])) AS PromiseDt1

The intention being to insert a holding date 01/01/1900 if the field contains too many characters to be a date alone. I've tried multiple variations of this code but keep getting errors across the board. Is my logic or Syntax flawed?

Any help appreciated.

Thanks
 
Hi Paul,
The entrys are usually a date followed by a comment eg: 23/02/2014 - ALLOCATED (REQ DEL DATE NOT REACHED)
or just the date '23/02/2014' or something like 'TBA'

Thanks
 
Code:
IIf(Len([TabDL].[PROMISE][B][COLOR="Red"]<>10)[/COLOR][/B],#1/1/1900#,CDate([TabDL].[PROMISE])) AS PromiseDt1

Your logic isnt flawed, your typing is :(
the <> belongs outside the ) of the Len

Typical *duh* moment, we all done it multiple times.

Keep in mind you may want to catch dates like 1/2/14 as well which also is <>10
 
Thanks that has fixed it although i'm still getting a #error if the field [PROMISE] is blank....i'm guessing this is a Null issue? I'll try a NZ type solution unless anyone can suggest a better way?
 
This is a simple function that will get you the date from the String, if it exists. Copy the function into a standard module, make sure that the Name of the module is not the same as the function. Compile the code, then use it in a Query or wherever you wish to use it..
Code:
Public Function getDate(inputStr As String) As Date
[COLOR=Green]'**********************
'Code Courtesy of
'  Paul Eugin
'**********************[/COLOR]
    Dim tmpArr() As String
    If InStr(inputStr, "/") <> 0 Then
        tmpArr = Split(inputStr, "/")
        getDate = DateSerial(Left(tmpArr(2), 4), tmpArr(1), tmpArr(0))
    Else
        getDate = 0
    End If
End Function
For the data sample you gave me.
Code:
? getDate("23/02/2014 - ALLOCATED (REQ DEL DATE NOT REACHED)")
23/02/2014 
? getDate("23/02/2014")
23/02/2014 
? getDate("TBA")
00:00:00
 
It would be better to test with the IsDate function

Edit: Well it was until Paul posted the getDate function.
 
Thanks all, really appreciate the response. I will try the function provided by Paul and let you know how i get on.

Dan
 
Paul could you give me an example of how you would call the funtion within the query please? I've googled it but am struggling as i've never done this before.

Thanks
 
Something like.
Code:
SELECT yourTable.yourStringFieldName, getDate(yourTable.yourStringFieldName) As newDate
FROM yourTable;
 
Paul, this works perfectly and has taught me something new! Thanks very much.

Dan
 

Users who are viewing this thread

Back
Top Bottom