Advertisement
Guest User

Untitled

a guest
Sep 19th, 2014
244
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.04 KB | None | 0 0
  1. Option Explicit
  2. Option Compare Database
  3.  
  4. Const constShaded = 12632256 ' Shaded text box
  5. Const constUnshaded = 16777215 ' Unshaded text box
  6. Const constBackground = -2147483633 ' Background color for form (for unused textboxes)
  7.  
  8. Private Sub btnNextMonth_Click()
  9. Dim ReferenceDate As Date
  10. Dim NewDate As Date
  11.  
  12. ' Load the current date from the form
  13. ReferenceDate = Me.txtCalendarHeading
  14.  
  15. ' Add 1 month to the date
  16. NewDate = DateAdd("m", 1, ReferenceDate)
  17.  
  18. RefreshCalendar DatePart("m", NewDate), DatePart("yyyy", NewDate)
  19.  
  20. End Sub
  21.  
  22. Private Sub btnPrevMonth_Click()
  23. Dim ReferenceDate As Date
  24. Dim NewDate As Date
  25.  
  26. ' Load the current date from the form
  27. ReferenceDate = Me.txtCalendarHeading
  28.  
  29. ' Subtract 1 month from the date
  30. NewDate = DateAdd("m", -1, ReferenceDate)
  31.  
  32. RefreshCalendar DatePart("m", NewDate), DatePart("yyyy", NewDate)
  33.  
  34. End Sub
  35.  
  36. Private Sub CalendarOverlay_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  37. Dim Row As Integer
  38. Dim Col As Integer
  39. Dim TextBoxIndex As Integer
  40. Dim DayIndex As Integer
  41. Dim strNum As String
  42. Dim ctl As Control
  43. Dim intYear As Integer
  44. Dim intMonth As Integer
  45. Dim intMaxDays As Integer
  46.  
  47. ' MsgBox "Button Mouse Down - X: " & X & " Y: " & Y ' <== Use this to figure out dimensions
  48. Const ButtonWidth = 3045 ' Maximum X value (found by experimenting with MsgBox enabled)
  49. Const ButtonHeight = 2025 ' Maximum Y value (found by experimenting with MsgBox enabled)
  50.  
  51. ' Convert X and Y to Row, Col equivalents on the table
  52. Col = Int(X / (ButtonWidth / 7)) + 1 ' Divide width across 7 days
  53. Row = Int(Y / (ButtonHeight / 6)) + 0 ' Divide height across 6 rows (for the calendar)
  54. ' MsgBox "Button Mouse Down - Col: " & Col & " Row: " & Row ' Debugging statement
  55.  
  56. ' Calculate the index and figure out which text box
  57. TextBoxIndex = Row * 7 + Col
  58.  
  59. ' Test to see if it is a day in the month
  60. DayIndex = TextBoxIndex - Weekday(Me.txtCalendarHeading) + 1
  61.  
  62. intMaxDays = Day(DateAdd("d", -1, DateAdd("m", 1, Me.txtCalendarHeading)))
  63.  
  64. If (DayIndex >= 1) And (DayIndex <= intMaxDays) Then
  65.  
  66. ' Make a 2-digit string with the number, e.g. "01" or "08" or "12" etc.
  67. strNum = Right("00" & TextBoxIndex, 2)
  68. Set ctl = Me("CalDay" & strNum) ' Note: similar to Me.Caldayxx, but allows a string
  69.  
  70. ' Toggle shading -- Just for demonstration
  71. If ctl.BackColor = constUnshaded Then
  72. ctl.BackColor = constShaded
  73. Else
  74. ctl.BackColor = constUnshaded
  75. End If
  76.  
  77. ' MsgBox the click -- Just for demonstration
  78. intYear = Year(Me.txtCalendarHeading)
  79. intMonth = Month(Me.txtCalendarHeading)
  80. MsgBox "Clicked on " & DateSerial(intYear, intMonth, DayIndex)
  81.  
  82. End If
  83.  
  84. End Sub
  85.  
  86. Private Sub Form_Load()
  87.  
  88. ' Call the refresh procedure
  89. ' Use the current date to start
  90. RefreshCalendar DatePart("m", Date), DatePart("yyyy", Date)
  91.  
  92. End Sub
  93.  
  94. Public Function RefreshCalendar(intMonth As Integer, intYear As Integer)
  95.  
  96. ' Initialize the calendar grid
  97. ClearCalendar
  98.  
  99. ' Set the date into the Calendar Heading
  100. ' Note this date is always the first of the displayed month (but field only shows month/year)
  101. Me.txtCalendarHeading = DateSerial(intYear, intMonth, 1)
  102.  
  103. ' Add numbers to the calendar
  104. NumberCalendar
  105.  
  106. End Function
  107.  
  108. Private Sub ClearCalendar()
  109. Dim TextBoxIndex As Integer
  110. Dim strNum As String
  111. Dim ctlCalendar As Control
  112. Dim ctlInitial As Control
  113.  
  114. ' Initialize the calendar grid to blanks
  115. For TextBoxIndex = 1 To 42
  116.  
  117. ' Make a 2-digit string with the number, e.g. "01" or "08" or "12" etc.
  118. strNum = Right("00" & TextBoxIndex, 2)
  119.  
  120. Set ctlCalendar = Me("CalDay" & strNum) ' Note: similar to Me.Caldayxx, but allows a string
  121. ctlCalendar.Value = ""
  122. ctlCalendar.BackColor = constBackground
  123. Next
  124.  
  125. Set ctlCalendar = Nothing
  126.  
  127. End Sub
  128.  
  129. Private Sub NumberCalendar()
  130. Dim FirstDay As Integer
  131. Dim DayIndex As Integer
  132. Dim TextBoxIndex As Integer
  133. Dim Done As Boolean
  134.  
  135. Dim ctlCalendar As Control
  136. Dim strNum As String
  137.  
  138. FirstDay = Weekday(Me.txtCalendarHeading) ' Figure out the first day of the week
  139. DayIndex = 1 ' Start counting days at 1
  140. TextBoxIndex = FirstDay ' Start indexing text boxes at first day in month
  141. Done = False
  142.  
  143. While Not (Done)
  144. ' Set the value of the correct CalDayxx text box
  145.  
  146. ' Make a 2-digit string with the number, e.g. "01" or "08" or "12" etc.
  147. strNum = Right("00" & TextBoxIndex, 2)
  148.  
  149. Set ctlCalendar = Me("CalDay" & strNum) ' Note: similar to Me.Caldayxx, but allows a string
  150. ctlCalendar.Value = DayIndex
  151. ctlCalendar.BackColor = constUnshaded
  152.  
  153. DayIndex = DayIndex + 1
  154. TextBoxIndex = TextBoxIndex + 1
  155.  
  156. ' Are we done? Check to see if we have indexed into next month
  157. If (Month(Me.txtCalendarHeading + (DayIndex - 1)) <> Month(Me.txtCalendarHeading)) Then
  158. Done = True
  159. End If
  160.  
  161. Wend
  162.  
  163. Set ctlCalendar = Nothing
  164. End Sub
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement