Hi,
As ija said, the string2Date function is good for conversion, I've modified it to check for valid (European) dates. The returnMonth function (not shown) used in this function is just a case statement used to convert a month from a number to a string (in my case I use a short version, e.g. Jan).
If the user inputs 01032002, the function returns 1 Jan 2002
Function String2Date(s, Fmt As String)
'
' Converts strings to dates depending on Fmt
'
On Error GoTo err_handling
'*** Declare variables
Dim dayEnt As Long, monthEnt As Long, yearEnt As Long
Dim myDate As Date
Dim tempDateTest As String, userDate As String, myDateTest As String
'*** Convert month into form mmm
tempDateTest = returnMonth(Mid(s, 3, 2))
dayEnt = Left(s, 2)
monthEnt = Mid(s, 3, 2)
yearEnt = Mid(s, 5)
myDate = DateSerial(yearEnt, monthEnt, dayEnt)
myDateTest = myDate
userDate = Left(s, 2) + "/" + Mid(s, 3, 2) + "/" + Mid(s, 5)
If Not userDate = myDateTest Then
MsgBox "Date is invalid, please try again"
Exit Function
End If
'*** The original code uses a case statement to allow you to choose which format
'*** of date to use. Although this has been set for one type only
Select Case Fmt
Case "DDMMMYYYY" '27051993
String2Date = Left(s, 2) + " " + tempDateTest + " " & Mid(s, 5)
Case Else
String2Date = Null
End Select
'*** If the first character is a zero, drop it
Dim firstChar As String, length As Long
firstChar = Left(String2Date, 1)
If firstChar = "0" Then
length = Len(String2Date)
String2Date = Right(String2Date, (length - 1))
End If
err_handling:
If Err.Number = 13 Then
Exit Function
End If
End Function
Hope this helps,
pmrankine