Validating dates in Visual Basic
Submitted By:
Unknown
Rating:
Not rated (
Rate It)
OPTION Explicit
FUNCTION ffnValidTimeDate (vTime2Check AS Variant, dynDataSet AS Dynaset, zFieldNameToCheck AS STRING, fSayMessage AS INTEGER) AS INTEGER
' Returns true if time value is valid.
' Data expected:
' vTime2Check has time to validate
' dynDataSet is the dynaset we will use to validate the date
' zFieldNameToCheck is the name of the dynaset field we will use to test the date
' If fSayMessage is true display a message
' This function uses the database itself to validate the date.
' A clone of the incomming dynaset is used so that the record pointer of
' the original dynaset is not disturbed.
DIM dynDynasetClone AS Dynaset
DIM zMsgText AS STRING
Set dynDynasetClone = dynDataSet.Clone()
' the function only returns false if an error is triggered.
ffnValidTimeDate = True
dynDynasetClone.AddNew
ON ERROR GOTO ErrorffnValidTimeDate
' Attempt to set the date field to the date supplied to the function.
' An error indicates that the date was not valid.
dynDynasetClone.Fields(zFieldNameToCheck) = vTime2Check
ON ERROR GOTO 0
' a second addnew clears the new record buffer
dynDynasetClone.AddNew
' because the close occurs without an update, no new records will be added.
dynDynasetClone.CLOSE
Set dynDynasetClone = Nothing
GOTO ExitffnValidTimeDate
ErrorffnValidTimeDate:
ffnValidTimeDate = False
IF fSayMessage = True THEN
IF ERR = 13 THEN
' Err 13 is a type mismatch error
zMsgText = "The date you entered does not appear to be correct. "
zMsgText = zMsgText & "Please try again."
ELSE
' Always a good idea to provide for the unknown
zMsgText = "An unexpected error occur while validating the date. "
zMsgText = zMsgText & "Please try again."
END IF
END IF
MsgBox zMsgText
RESUME NEXT
ExitffnValidTimeDate:
END FUNCTION
SUB Main ()
Load Form1
Form1!Data1.DatabaseName = app.Path + "\" + "valdate.mdb"
Form1!Data1.RecordSource = "Invoices"
Form1!Text2.DataField = "ShipDate"
Form1!Text1.DataField = "InvoiceNumber"
Form1!Text3.DataField = "CustomerID"
Form1.Show
END SUB