DatabaseLessons.com

'Serving the Microsoft® Access
Community since 1997'

News/Blog
Tables
Queries
Forms
Reports
Modules
Miscellaneous
Subscribe
Free Samples
Videos
Services
Links
About Us

 

 

Custom Calendar Form in MS Access - Part 4 Code Listing

Option Compare Database
Option Explicit

'----------------------------

Private Sub cboMonth_AfterUpdate()

   Call basCreateCalendar
   
End Sub

'----------------------------

Private Sub cmdMonthMinus_Click()

   '--- subtract 1 from current month #
   cboMonth = cboMonth - 1
   '--- if January was the old month, answer will be "0",
   '--- change to December of previous year
   If cboMonth < 1 Then
       Me.txtYear = Me.txtYear - 1
       cboMonth = 12
   End If

   '--- this call recreates the calendar based on the new month
   Call basCreateCalendar
   
End Sub

'----------------------------

Private Sub cmdMonthPlus_Click()

   '--- add 1 to current month #
   cboMonth = cboMonth + 1
   '--- if December was the old month, answer will be "13",
   '--- change to January of next year
   If cboMonth > 12 Then
       Me.txtYear = Me.txtYear + 1
       cboMonth = 1
   End If

   '--- this call recreates the calendar based on the new month
   Call basCreateCalendar
   
End Sub

'----------------------------

Private Sub cmdYearMinus_Click()

   Me.txtYear = Me.txtYear - 1

   Call basCreateCalendar

End Sub

'----------------------------

Private Sub cmdYearPlus_Click()

   Me.txtYear = Me.txtYear + 1

   Call basCreateCalendar

End Sub

'----------------------------

Private Sub Form_Open(Cancel As Integer)

    Me.txtYear = Year(Date)
    cboMonth = Month(Date)

    Call basCreateCalendar

End Sub

'----------------------------

Private Sub basCreateCalendar()

   Dim intFirstDay As Integer
   Dim intLastDay As Integer
   Dim i As Integer

   '--- this will make all the date buttons invisible
   For i = 1 To 42
      Me("cmd" & Format(i, "00")).Visible = False
   Next i

   '--- determine day of week of first day of the selected month
   intFirstDay = Weekday(DateSerial(Me.txtYear, cboMonth, 1))
   '--- which command button will be the last day of the month
   intLastDay = Day(DateSerial(Me.txtYear, cboMonth + 1, 0)) + intFirstDay - 1

   '--- this will make the appropriate date buttons visible
   For i = intFirstDay To intLastDay
      Me("cmd" & Format(i, "00")).Visible = True
      Me("cmd" & Format(i, "00")).Tag = i - intFirstDay + 1
      Me("cmd" & Format(i, "00")).Caption = i - intFirstDay + 1
   Next i

End Sub

'----------------------------

Private Function ChooseDate()

   Dim intDay As Integer

   '--- I did not really need the intDay variable,
	 '--- I could have just put the Screen reference in the call
	 '--- to DateSerial,
   '--- but it made the listing look neater on this page
   intDay = Screen.ActiveForm.ActiveControl.Caption
   gChosenDate = DateSerial(Me.txtYear, cboMonth, intDay)

   DoCmd.Close '--- close the calendar form

End Function

 
Note: This web site dedicated to MS Access database users is an independent publication of Richard W. Killey and is not affiliated with, nor has it been authorized, sponsored, or otherwise approved by Microsoft® Corporation.
 

 

© 2006, 2007, 2008 Richard W. Killey. All Rights Reserved. - Privacy Policy