Clock script

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • apartain
    New Member
    • Nov 2006
    • 58

    Clock script

    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? ???

    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.
  • apartain
    New Member
    • Nov 2006
    • 58

    #2
    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?

    Comment

    • Rabbit
      Recognized Expert MVP
      • Jan 2007
      • 12517

      #3
      What are you trying to accomplish?
      What do you mean by adding a clock feature for Start Time and End Time?

      Comment

      • MMcCarthy
        Recognized Expert MVP
        • Aug 2006
        • 14387

        #4
        Originally posted by Rabbit
        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

        Comment

        • apartain
          New Member
          • Nov 2006
          • 58

          #5
          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.

          Comment

          • ADezii
            Recognized Expert Expert
            • Apr 2006
            • 8834

            #6
            Originally posted by apartain
            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? ???

            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.
            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:
            Code:
            Input Mask: 99:00:00\ >LL;0;_
            Format    : hh:nn:ss AM/PM

            Comment

            Working...