Tuesday, September 21, 2010

Visual Basic Date and Time Function : DateValue

Purpose : The dateValue function converts a date in the form of a string into a visual basic date data type . This function changes differently formatted dates to a universal form.

Syntax :
DateValue ( datestring$ )

datestring$ : String that represents a date for the dateValue function to convert.

Examples of valid datestring$ Argument values
Valid String
01/01/2010
01/01/10
January 1, 2010
Jan 1, 1994
01-Jan-2010
01 January 2010

Option Explicit

Private Sub cmdCalculate_Click()
Dim dtmFirstDate As Date, dtmEndDate As Date
Dim vmtDays As Variant
On Error GoTo BadDate
If txtDate(0).Text = "" Or txtDate(1).Text = "" Then
ErrMsgBox "Please enter a date in each box."
Exit Sub
End If
'find date variant of entered date
dtmFirstDate = DateValue(txtDate(0).Text)
'find date variant of entered date
dtmEndDate = DateValue(txtDate(1).Text)
'display date variant
txtDate(0).Text = Str(dtmFirstDate)
'display date variant
txtDate(1).Text = Str(dtmEndDate)
'find number of days between dates
vmtDays = Abs((dtmEndDate - dtmFirstDate))
lblDifference.Caption = "Number of days between dates : " & _
Trim(vmtDays) & " Days"
Exit Sub

BadDate:
ErrMsgBox "Please enter a valid date."
Exit Sub

End Sub

Public Sub ErrMsgBox (massage As String)
MsgBox massage
End Sub

No comments:

Post a Comment