The Calendar Application
Implementing the Calandar, Page 4
By itself the Calandar application only has a limited use as a stand alone application. To get the most use from the application it needed to built in such a fashion so that it could be used not only as a stand alone application but also as a reusable component that could be re-used in other applications.
As described above the calendar application is ready to go as a stand alone application which will accept a command line argument of any valid date but to be useful with other Windows applications it will need the ability to pass the users selected date back to the calling application. There are already a number of VBX Calendar applications commercially available but these were designed and implemented before the release of Win95 and all unfortunately do not reflect changes made to the International settings in Win95. At first sight it would therefore seem logical to take the prototype calendar application developed in Visual Basic and to rewrite it as an OXC implementation that could be used as an add-on component for any VB application that required calendar functionality. However as I almost exclusively use VB and as I did not wish to go to the trouble of having yet another VBX/OCX file to remember to distribute with my applications I did not persue the development of a Calendar OCX. Also the code that I have used works equally as well(except for international considerations) with the 16 and 32 bit versions of VB as well as with Access. Instead I have chosen to use the application as is and to store the form file as a read-only file that I could then include in my applications or copy and paste from the file to implement different versions of the same calendar application ( see CALENDAR.FRM included in CALENDAR.ZIP which can be downloaded from this ftp site).
To add calendar functionality to any exiting VB project simply add the calendar.frm to your VB project and add a button which the user can click in order to display the form and select a date. eg.
Private Sub cmdGetDate_Click(Index As Integer)
Select Case Index
Case 0
SettlementDate = GetDate_Dialog(SettlementDate)
Case 1
MaturityDate = GetDate_Dialog(MaturityDate)
Case Else
End Select
End Sub
The GetDate_Dialog() function loads and displays the Calendar form modally and
then passes the chosen date back to the calling application : This function
should be placed in a utilities module that can also be added to your projects
as required.
Function GetDate_Dialog(Optional dt As Variant) As Variant
' Show and display the Calendar dialog and return the chosen date.
Load Calendar
If IsDate(dt) Then
Calendar.lblLastDate = dt ' tell the Calendar form to display the
passed date
Else
Calendar.lblLastDate = Date ' default to todays date if no date passed
End If
Calendar.Show 1
GetDate_Dialog = Calendar.DateSelected ' get the users selected date
Unload Calendar
Set Calendar = Nothing ' free memory resources
End Function
Alternatively you may wish to implement the calendar as a floating
toolbar in an MDI application. All that is required is to set the
forms' MDIChild property to true after the form has been added to your
project. If you are working in a team it would be advisable to keep
two copies of the Calendar form, one as an MDI child form and one as a
normal form so that there are no version conflicts.
The most common use of a calendar application however is not as free floating form but rather as a component on a form in some other application like an appointment book. To implement calendar functionality simply copy and pasted the controls and code from the Calendar form into a containing form. I have stored the calendar application in a library using my product Object Library from which I can simply drag and drop the complete calendar (controls and code) directly into a containing form. As the calendar is made from lightweight controls there are no overheads involved in adding additional OCX or VBX files to my project to implement calendar functionality.
The beauty of this VB application is that all of the code used in the VB form can be reused in equivalent applications for MS Access, MS Excel and MS Word with little or no modification to the syntax. An access equivalent calendar is included with the ZIP file that accompanies this article.
A listing of VBA date functions and subroutines.
| Weekday(dateexpression) | Returns an integer between 1 (Sunday) and 7 (Saturday) that represents the day of the week for a date argument. |
| Date[$] | Date returns a Variant of VarType 7 (Date) containing a date
stored internally as a Double.
Date$ returns a 10-character string of the form mm-dd-yyyy, where mm is the month (01-12), dd is the day (01-31), and yyyy is the year (1980-2099). The return value of the Date$ function is equivalent to: Format$(Now,"mm-dd-yyyy") To set the system date, use the Date[$] statement. |
| Date[$] = expression | Sets the current system date. |
| Day(dateexpression) | Returns an integer between 1 and 31, inclusive, that represents the day of the month for a date argument. |
| Month(dateexpression) | Returns an integer between 1 and 12, inclusive, that represents the month of the year for a date argument. |
| Year(dateexpression) | Returns an integer between 100 and 9999, inclusive, that represents the year of a date argument. |
| DateDiff( interval, date1, date2 ) | Returns a Variant containing the number of time intervals between two specified dates. |
| DateAdd( interval, number, date ) | Returns a Variant containing a date to which a specified time interval has been added. |
| Weekday(dateexpression) | Returns an integer between 1 (Sunday) and 7 (Saturday) that represents the day of the week for a date argument. |