I wanted to add a clock feature to a form for both Start Time and End Time fields. I followed the instructions from the Help files to make the Start Time work smoothly. The Help files only gave the code for one field, however, and I need two. Following is the OnCurrent Event Procedure for the Start Time field.
All of my controls for the End Time clock feature are exactly the same with a "2" behind them.
The question is: Do I need to add lines of code in the following code to perform the same tasks for the End Time field, or do I copy and paste the exact code and go through and correct the control names to be ending with 2? If the latter, there are a few items in the code I need to know if I should update as well.
1-oCtl (what is this? Do I need to rename it oCtl2 for the End Time control?)
2-fIncrement (don't know what this is, either. Do I rename to fIncrement2?)
3-iChange(again, don't know what it is for. Name it iChange2?)
I assume I can rename the second copy of code to ClockChangeTime 2 as well as ClockFlipAMPM to ClockFlipAMPM2, ClockAddLeading Zeros to ClockAddLeading Zeros2, ClockLoadDate to ClockLoadDate2 and ClockSaveDate to ClockSaveDate2? ???
Also, how do I write this code and save it for future use in other databases? I'm sure I'll use this feature often and as little as I know about VBA I'd like to avoid having to re-do all of this.
Any help is appreciated.
All of my controls for the End Time clock feature are exactly the same with a "2" behind them.
The question is: Do I need to add lines of code in the following code to perform the same tasks for the End Time field, or do I copy and paste the exact code and go through and correct the control names to be ending with 2? If the latter, there are a few items in the code I need to know if I should update as well.
1-oCtl (what is this? Do I need to rename it oCtl2 for the End Time control?)
2-fIncrement (don't know what this is, either. Do I rename to fIncrement2?)
3-iChange(again, don't know what it is for. Name it iChange2?)
I assume I can rename the second copy of code to ClockChangeTime 2 as well as ClockFlipAMPM to ClockFlipAMPM2, ClockAddLeading Zeros to ClockAddLeading Zeros2, ClockLoadDate to ClockLoadDate2 and ClockSaveDate to ClockSaveDate2? ???
Code:
'// This routine will change the time part + or - 1. Public Sub ClockChangeTime(oCtl As TextBox, fIncrement As Boolean) Dim iChange As Integer 'See if we are adding or subtracting. If fIncrement Then iChange = 1 Else iChange = -1 End If 'Find out what part of the time we are supposed to act on. Select Case oCtl.Name 'Check for the hour. Case Is = "txtHour" 'Check to see if we need to change am/pm. If txtHour = 11 And iChange = 1 Then ClockFlipAMPM If txtHour = 12 And iChange = -1 Then ClockFlipAMPM 'Change the hour. txtHour = txtHour + iChange 'See if we need to roll over. If txtHour <= 0 Then txtHour = 12 ElseIf txtHour >= 13 Then txtHour = 1 End If 'Check for the minutes. Case Is = "txtMin" txtMin = txtMin + iChange 'See if we need to roll over. If txtMin <= -1 Then txtMin = 59 ClockChangeTime txtHour, False ElseIf txtMin >= 60 Then txtMin = 0 ClockChangeTime txtHour, True End If 'Check for the seconds. Case Is = "txtSec" txtSec = txtSec + iChange 'See if we need to roll over. If txtSec <= -1 Then txtSec = 59 ClockChangeTime txtMin, False ElseIf txtSec >= 60 Then txtSec = 0 ClockChangeTime txtMin, True End If End Select 'Be sure we have 0's in front. ClockAddLeadingZeros 'Save the date back to the recordset. ClockSaveDate 'Let's refresh in case the user is holding down the button. DoEvents End Sub '// Adds 0's to the front of the time parts. Private Sub ClockAddLeadingZeros() If Len(txtHour) <> 2 Then txtHour = "0" & txtHour If Len(txtMin) <> 2 Then txtMin = "0" & txtMin If Len(txtSec) <> 2 Then txtSec = "0" & txtSec End Sub '// Used to write the date back to the form value. Private Sub ClockSaveDate() If cboAMPM = "PM" And txtHour <> 12 Then [Start Time].Value = TimeSerial(txtHour + 12, txtMin, txtSec) Else [Start Time].Value = TimeSerial(txtHour, txtMin, txtSec) End If End Sub '// Called to initalize the textboxes with the date. '// Can be called on oncurrent event to sync with form. '// recordset movement Private Sub ClockLoadDate(sDate As String) 'Check to see if we have a valid date. If IsDate(sDate) Then 'Load the hour. txtHour = Hour(sDate) If txtHour = 0 Then txtHour = 12 If txtHour > 12 Then txtHour = txtHour - 12 'Load the minutes and seconds. txtMin = Minute(sDate) txtSec = Second(sDate) 'Load the AM/PM. If Hour(sDate) < 12 Then cboAMPM.Value = "AM" Else cboAMPM.Value = "PM" End If 'Be sure we have 0's in front. ClockAddLeadingZeros Else 'Not a valid date; set a default. txtHour = "12" txtMin = "00" txtSec = "00" cboAMPM = "AM" End If End Sub '// Used to flip am/pm when rolling over between the 2. Sub ClockFlipAMPM() If cboAMPM.Value = "AM" Then cboAMPM.Value = "PM" Else cboAMPM.Value = "AM" End If End Sub
Also, how do I write this code and save it for future use in other databases? I'm sure I'll use this feature often and as little as I know about VBA I'd like to avoid having to re-do all of this.
Any help is appreciated.
Comment