PDA

View Full Version : Date validation functions?



BoatwrenchV8
10-28-2012, 12:25 PM
Does anyone know of a date validation function other than IsDate()? I encounter dates in the format of ddmmmyyyy without spaces. The problem is, IsDate() returns false for dates in that format. Any suggestions or do I have to write my own function?

fumei
10-28-2012, 04:19 PM
You will have to write a custom function.

Frosty
10-29-2012, 12:29 PM
I wouldn't think the function has to be that complicated, structurally. Just use IsDate, and if it fails, then start trying to convert your test pattern into something IsDate *could* recognize...

Public Function myIsDate(ByVal sDate As String) As Boolean
Dim sTest As String
If IsDate(sDate) Then
myIsDate = True
Else
'do some custom testing...
Select Case Len(sDate)
'9 characters -- assume dd-mmm-yyy format??
Case 9
sTest = Left(sDate, 2) & "-"
sTest = sTest & Mid(sDate, 3, Len(sDate) - 6) & "-"
sTest = sTest & Right(sDate, 4)
myIsDate = IsDate(sTest)
Case Else
End If
End Function

fumei
10-29-2012, 01:10 PM
That would work.

Boatwrench, essentially ddmmmyyyy is just (as it is in Frosty's function) text, not a date. It may look like a date, but not to Word. It is a string, text - no different than awftyplqa, or pxfjknbez. So you need to make it into something Word (VBA) does recognize as a possible date. And THEN test/validate it.

BoatwrenchV8
10-29-2012, 04:17 PM
Agree and this is basically the approach I was thinking of. Not going to re invent the wheel. I hope to post some code soon and see what you all think.

fumei
10-29-2012, 08:26 PM
Jason, there may be a teaching moment regarding why you explicitly used ByVal for sDate As String.

dantzu
11-05-2012, 11:51 AM
.

BoatwrenchV8
11-17-2012, 10:27 PM
Public Function myIsDate(ByVal sDate As String) As Boolean
Dim sTest As String

If IsDate(sDate) Then
myIsDate = True
Else
'do some custom testing...
Select Case Len(sDate)
'9 characters -- assume ddmmmyyyy format and
' attempt to format as dd-mmm-yyyy, then test.
Case 9
sTest = Left(sDate, 2) & "-"
sTest = sTest & Mid(sDate, 3, Len(sDate) - 6) & "-"
sTest = sTest & Right(sDate, 4)
myIsDate = IsDate(sTest)

'8 characters -- assume dmmmyyyy format and
' attempt to format as d-mmm-yyyy, then test.
Case 8
sTest = Left(sDate, 1) & "-"
sTest = sTest & Mid(sDate, 2, Len(sDate) - 5) & "-"
sTest = sTest & Right(sDate, 4)
myIsDate = IsDate(sTest)

'7 characters -- assume ddmmmyy format and
' attempt to format as dd-mmm-yy, then test.
Case 7
sTest = Left(sDate, 2) & "-"
sTest = sTest & Mid(sDate, 3, Len(sDate) - 4) & "-"
sTest = sTest & Right(sDate, 2)
myIsDate = IsDate(sTest)
'6 characters -- assume dmmmyy format and
' attempt to format as d-mmm-yy, then test.
Case 6
sTest = Left(sDate, 1) & "-"
sTest = sTest & Mid(sDate, 2, Len(sDate) - 3) & "-"
sTest = sTest & Right(sDate, 2)
myIsDate = IsDate(sTest)

Case Else

End Select
End If
End Function


Is the reason ByVal was chosen was so only a copy of the string was supplied to the function? ByRef is the default, correct? If I wanted the function to give me a boolean flag AND a modified version of the date/string, couldn't I use ByRef so the actual variable could be changed? How would I accomplish this? Declare the variable used when the function was called as global?