By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,853 Members | 2,186 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,853 IT Pros & Developers. It's quick & easy.

Clock script

P: 58
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 ClockChangeTime2 as well as ClockFlipAMPM to ClockFlipAMPM2, ClockAddLeadingZeros to ClockAddLeadingZeros2, ClockLoadDate to ClockLoadDate2 and ClockSaveDate to ClockSaveDate2????

Expand|Select|Wrap|Line Numbers
  1.  '// This routine will change the time part + or - 1.
  2. Public Sub ClockChangeTime(oCtl As TextBox, fIncrement As Boolean)
  3.     Dim iChange As Integer
  4.  
  5.     'See if we are adding or subtracting.
  6.     If fIncrement Then
  7.         iChange = 1
  8.     Else
  9.         iChange = -1
  10.     End If
  11.  
  12.     'Find out what part of the time we are supposed to act on.
  13.     Select Case oCtl.Name
  14.         'Check for the hour.
  15.         Case Is = "txtHour"
  16.             'Check to see if we need to change am/pm.
  17.             If txtHour = 11 And iChange = 1 Then ClockFlipAMPM
  18.             If txtHour = 12 And iChange = -1 Then ClockFlipAMPM
  19.  
  20.             'Change the hour.
  21.             txtHour = txtHour + iChange
  22.  
  23.             'See if we need to roll over.
  24.             If txtHour <= 0 Then
  25.                 txtHour = 12
  26.             ElseIf txtHour >= 13 Then
  27.                 txtHour = 1
  28.             End If
  29.  
  30.         'Check for the minutes.
  31.         Case Is = "txtMin"
  32.             txtMin = txtMin + iChange
  33.  
  34.             'See if we need to roll over.
  35.             If txtMin <= -1 Then
  36.                 txtMin = 59
  37.                 ClockChangeTime txtHour, False
  38.             ElseIf txtMin >= 60 Then
  39.                 txtMin = 0
  40.                 ClockChangeTime txtHour, True
  41.             End If
  42.  
  43.         'Check for the seconds.
  44.         Case Is = "txtSec"
  45.             txtSec = txtSec + iChange
  46.  
  47.             'See if we need to roll over.
  48.             If txtSec <= -1 Then
  49.                 txtSec = 59
  50.                 ClockChangeTime txtMin, False
  51.             ElseIf txtSec >= 60 Then
  52.                 txtSec = 0
  53.                 ClockChangeTime txtMin, True
  54.             End If
  55.     End Select
  56.  
  57.     'Be sure we have 0's in front.
  58.     ClockAddLeadingZeros
  59.  
  60.     'Save the date back to the recordset.
  61.     ClockSaveDate
  62.  
  63.     'Let's refresh in case the user is holding down the button.
  64.     DoEvents
  65. End Sub
  66.  
  67. '// Adds 0's to the front of the time parts.
  68. Private Sub ClockAddLeadingZeros()
  69.      If Len(txtHour) <> 2 Then txtHour = "0" & txtHour
  70.      If Len(txtMin) <> 2 Then txtMin = "0" & txtMin
  71.      If Len(txtSec) <> 2 Then txtSec = "0" & txtSec
  72. End Sub
  73.  
  74. '// Used to write the date back to the form value.
  75. Private Sub ClockSaveDate()
  76.     If cboAMPM = "PM" And txtHour <> 12 Then
  77.         [Start Time].Value = TimeSerial(txtHour + 12, txtMin, txtSec)
  78.     Else
  79.         [Start Time].Value = TimeSerial(txtHour, txtMin, txtSec)
  80.     End If
  81. End Sub
  82.  
  83. '// Called to initalize the textboxes with the date.
  84. '// Can be called on oncurrent event to sync with form.
  85. '// recordset movement
  86. Private Sub ClockLoadDate(sDate As String)
  87.     'Check to see if we have a valid date.
  88.     If IsDate(sDate) Then
  89.         'Load the hour.
  90.         txtHour = Hour(sDate)
  91.         If txtHour = 0 Then txtHour = 12
  92.         If txtHour > 12 Then txtHour = txtHour - 12
  93.  
  94.         'Load the minutes and seconds.
  95.         txtMin = Minute(sDate)
  96.         txtSec = Second(sDate)
  97.  
  98.         'Load the AM/PM.
  99.         If Hour(sDate) < 12 Then
  100.             cboAMPM.Value = "AM"
  101.         Else
  102.             cboAMPM.Value = "PM"
  103.         End If
  104.  
  105.         'Be sure we have 0's in front.
  106.         ClockAddLeadingZeros
  107.     Else
  108.         'Not a valid date; set a default.
  109.         txtHour = "12"
  110.         txtMin = "00"
  111.         txtSec = "00"
  112.         cboAMPM = "AM"
  113.     End If
  114. End Sub
  115.  
  116. '// Used to flip am/pm when rolling over between the 2.
  117. Sub ClockFlipAMPM()
  118.     If cboAMPM.Value = "AM" Then
  119.        cboAMPM.Value = "PM"
  120.     Else
  121.         cboAMPM.Value = "AM"
  122.     End If
  123. End Sub
  124.  



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.
Mar 16 '07 #1
Share this Question
Share on Google+
5 Replies


P: 58
BTW, I do not have a Seconds field but did not know which lines of code I could delete. Do I even need to delete andy Seconds code?
Mar 16 '07 #2

Rabbit
Expert Mod 10K+
P: 12,359
What are you trying to accomplish?
What do you mean by adding a clock feature for Start Time and End Time?
Mar 16 '07 #3

MMcCarthy
Expert Mod 10K+
P: 14,534
What are you trying to accomplish?
What do you mean by adding a clock feature for Start Time and End Time?
If you are talking about an activeX clock where the user selects a time and it is passed to the textbox then you will need a separate instance of the clock for each textbox.

Mary
Mar 16 '07 #4

P: 58
I did not know there is an ActiveX clock. I do know how to use the ActiveX Calendar control. Is it the same?

The code I referenced is directly from the Access help files for adding a "digital" clock feature to a form for a single text box. I need it for TWO text boxes, a start and stop time for an Employee Time Card database. Since I'm not familiar with VBA, I wasn't sure how to modify the code I downloaded to work for both text boxes.
Mar 20 '07 #5

ADezii
Expert 5K+
P: 8,627
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 ClockChangeTime2 as well as ClockFlipAMPM to ClockFlipAMPM2, ClockAddLeadingZeros to ClockAddLeadingZeros2, ClockLoadDate to ClockLoadDate2 and ClockSaveDate to ClockSaveDate2????

Expand|Select|Wrap|Line Numbers
  1.  '// This routine will change the time part + or - 1.
  2. Public Sub ClockChangeTime(oCtl As TextBox, fIncrement As Boolean)
  3.     Dim iChange As Integer
  4.  
  5.     'See if we are adding or subtracting.
  6.     If fIncrement Then
  7.         iChange = 1
  8.     Else
  9.         iChange = -1
  10.     End If
  11.  
  12.     'Find out what part of the time we are supposed to act on.
  13.     Select Case oCtl.Name
  14.         'Check for the hour.
  15.         Case Is = "txtHour"
  16.             'Check to see if we need to change am/pm.
  17.             If txtHour = 11 And iChange = 1 Then ClockFlipAMPM
  18.             If txtHour = 12 And iChange = -1 Then ClockFlipAMPM
  19.  
  20.             'Change the hour.
  21.             txtHour = txtHour + iChange
  22.  
  23.             'See if we need to roll over.
  24.             If txtHour <= 0 Then
  25.                 txtHour = 12
  26.             ElseIf txtHour >= 13 Then
  27.                 txtHour = 1
  28.             End If
  29.  
  30.         'Check for the minutes.
  31.         Case Is = "txtMin"
  32.             txtMin = txtMin + iChange
  33.  
  34.             'See if we need to roll over.
  35.             If txtMin <= -1 Then
  36.                 txtMin = 59
  37.                 ClockChangeTime txtHour, False
  38.             ElseIf txtMin >= 60 Then
  39.                 txtMin = 0
  40.                 ClockChangeTime txtHour, True
  41.             End If
  42.  
  43.         'Check for the seconds.
  44.         Case Is = "txtSec"
  45.             txtSec = txtSec + iChange
  46.  
  47.             'See if we need to roll over.
  48.             If txtSec <= -1 Then
  49.                 txtSec = 59
  50.                 ClockChangeTime txtMin, False
  51.             ElseIf txtSec >= 60 Then
  52.                 txtSec = 0
  53.                 ClockChangeTime txtMin, True
  54.             End If
  55.     End Select
  56.  
  57.     'Be sure we have 0's in front.
  58.     ClockAddLeadingZeros
  59.  
  60.     'Save the date back to the recordset.
  61.     ClockSaveDate
  62.  
  63.     'Let's refresh in case the user is holding down the button.
  64.     DoEvents
  65. End Sub
  66.  
  67. '// Adds 0's to the front of the time parts.
  68. Private Sub ClockAddLeadingZeros()
  69.      If Len(txtHour) <> 2 Then txtHour = "0" & txtHour
  70.      If Len(txtMin) <> 2 Then txtMin = "0" & txtMin
  71.      If Len(txtSec) <> 2 Then txtSec = "0" & txtSec
  72. End Sub
  73.  
  74. '// Used to write the date back to the form value.
  75. Private Sub ClockSaveDate()
  76.     If cboAMPM = "PM" And txtHour <> 12 Then
  77.         [Start Time].Value = TimeSerial(txtHour + 12, txtMin, txtSec)
  78.     Else
  79.         [Start Time].Value = TimeSerial(txtHour, txtMin, txtSec)
  80.     End If
  81. End Sub
  82.  
  83. '// Called to initalize the textboxes with the date.
  84. '// Can be called on oncurrent event to sync with form.
  85. '// recordset movement
  86. Private Sub ClockLoadDate(sDate As String)
  87.     'Check to see if we have a valid date.
  88.     If IsDate(sDate) Then
  89.         'Load the hour.
  90.         txtHour = Hour(sDate)
  91.         If txtHour = 0 Then txtHour = 12
  92.         If txtHour > 12 Then txtHour = txtHour - 12
  93.  
  94.         'Load the minutes and seconds.
  95.         txtMin = Minute(sDate)
  96.         txtSec = Second(sDate)
  97.  
  98.         'Load the AM/PM.
  99.         If Hour(sDate) < 12 Then
  100.             cboAMPM.Value = "AM"
  101.         Else
  102.             cboAMPM.Value = "PM"
  103.         End If
  104.  
  105.         'Be sure we have 0's in front.
  106.         ClockAddLeadingZeros
  107.     Else
  108.         'Not a valid date; set a default.
  109.         txtHour = "12"
  110.         txtMin = "00"
  111.         txtSec = "00"
  112.         cboAMPM = "AM"
  113.     End If
  114. End Sub
  115.  
  116. '// Used to flip am/pm when rolling over between the 2.
  117. Sub ClockFlipAMPM()
  118.     If cboAMPM.Value = "AM" Then
  119.        cboAMPM.Value = "PM"
  120.     Else
  121.         cboAMPM.Value = "AM"
  122.     End If
  123. End Sub
  124.  



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.
The code you are attempting to reproduce reflects Actual Flight Arrival Times which the User can enter directly into an Arrival Time Text Box, or produce using several 'Spin Button' type effects to enter Hours. Minutes, Seconds, and AM/PM Values. The result of all this interaction is to store the Arrival Time in a Date/Time Field and convert/reflect the proper value for each Current Record being shown. If you also require an End Time Field, you would have to duplicate much of this functionality in order to accomplish this. Unless you are proficient in VB coding, I suggest you use 2 Text Boxes with the appropriate Input Mask and Format Properties set, and bind them to the appropriate Fields as in:
Expand|Select|Wrap|Line Numbers
  1. Input Mask: 99:00:00\ >LL;0;_
  2. Format    : hh:nn:ss AM/PM
Mar 21 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.