Monday, September 20, 2010

Visual Basic Date and Time Function : DateSerial

Purpose : The DateSerial function converts the numeric values of an indicated date to a Visual Basic date data type.

Syntax :
DateSerial ( year%, month%, day% )

year% : A number or expression that evaluates to between 100 and 9999 inclusive
month% : A number or expression that evaluates to between 1 and 12 inclusive
day% : A number or expression that evaluates to between 1 and 31 inclusive

Option Explicit

Dim thisYear%, thisMonth%, thisDay%, monthsToAdd%
Dim newDate As Date


Private Sub Command1_Click()
'how many months to add to current date
monthsToAdd% = Val(Combo1.Text)
thisYear% = Year(Now())
thisMonth% = Month(Now())
thisDay% = Day(Now())
On Error GoTo baddate
newDate = DateSerial(thisYear%, thisMonth% + monthsToAdd%, thisDay)
Text1.Text = Format$(newDate, "dd-mm-yyyy")
Exit Sub

baddate:
MsgBox "The result date is not acceptable"
Exit Sub

End Sub

Private Sub Form_Load()
Dim i%
For i = 1 To 12
Combo1.AddItem i
Next i
Combo1.ListIndex = 0
End Sub

No comments:

Post a Comment