The approach I take to this is slightly different. I have 3 labels and three
spinbutton controls on the form, one each for day, month and year, each
spinputtin being positioned so it is oriented vertially and is next to its
label. The labels are called lDay, lMonth and lYear, and the spinbuttons are
spDay, spMonth and spYear.
On the form, I have a module-level variable called mDate, which is of type
Date, which hold the date which is displayed. The code to control it all is
as follows.
Private Sub UserForm_Initialize()
mDate = Now
SetDate mTerminationDate, lbTerminationDay, lbTerminationMonth,
lbTerminationYear
End Sub
Private Sub spDay_SpinUp()
mDate = DateAdd("d", 1, mDate)
SetDate mDate, lbDay, lbMonth, lbYear
End Sub
Private Sub spDay_SpinDown()
mDate = DateAdd("d", -1, mDate)
SetDate mDate, lbDay, lbMonth, lbYear
End Sub
Private Sub spMonth_SpinUp()
mDate = DateAdd("m", 1, mDate)
SetDate mDate, lbDay, lbMonth, lbYear
End Sub
Private Sub spMonth_SpinDown()
mDate = DateAdd("m", -1, mDate)
SetDate mDate, lbDay, lbMonth, lbYear
End Sub
Private Sub spYear_SpinUp()
mDate = DateAdd("yyyy", 1, mDate)
SetDate mDate, lbDay, lbMonth, lbYear
End Sub
Private Sub spYear_SpinDown()
mDate = DateAdd("yyyy", -1, mDate)
SetDate mDate, lbDay, lbMonth, lbYear
End Sub
Private Sub SetDate(dDate As Date, lDay As Label, lMonth As Label, lYear As
Label)
lDay.Caption = Format(dDate, "d")
lMonth.Caption = Format(dDate, "MMMM")
lYear.Caption = Format(dDate, "yyyy")
End Sub
The UserForm_Initialize routine sets the date to today, and calls the
SetDate routine to set the labels as appropriate.
Each of the spinputton routines increases or decreases the date by the
appropriate interval, and then calls the SetDate routine to update the
labels.
There are four things about this that I like.
- it doesn't require a reference to an external DLL or OCX
- it doesn't require very much code.
- it is impossible to set an invalid date (Feb 31st for instance)
- When you spin past the end of a month or year, the whole date is properly
incremented or decremented, i.e. going up a day from Jan 31 takes you to Feb
1.
--
Regards
Jonathan West - Word MVP
http://www.multilinker.com
Please reply to the newsgroup