Solved Time Picker Minutes are off (1 Viewer)

oxicottin

Learning by pecking away....
Local time
Today, 00:53
Joined
Jun 26, 2007
Messages
851
Hello, I created this time picker from pieces of examples I have been looking at and I cant figure out why my minutes are off.
 

Attachments

  • Option Time Picker.accdb
    1.3 MB · Views: 133

Isaac

Lifelong Learner
Local time
Yesterday, 21:53
Joined
Mar 14, 2017
Messages
8,738
I get a run time error about an active control just trying to load the form.
 

oxicottin

Learning by pecking away....
Local time
Today, 00:53
Joined
Jun 26, 2007
Messages
851
Don't run the picker first it has to be opened from the main forms text box.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Yesterday, 23:53
Joined
Feb 28, 2001
Messages
27,001
OK, I'll bite. What is the symptom? You say the minutes are off. What, specifically, do you do (I'm talking manually) when you operate the picker and what does it return when you do it? Be a bit tedious in the description if you have to.
 

oxicottin

Learning by pecking away....
Local time
Today, 00:53
Joined
Jun 26, 2007
Messages
851
Ok ill try.... If you click on the text box on the main form it brings up the picker. Right off the batt the pickers time is off by one minute shown in Capture1 (look at tray time). If you select the time you want (hours and min) it moves the correct time to the textbox on the main form. Now, if I select the textbox again and it reopens the picker the hours is always correct BUT the minutes are always off.
 

Attachments

  • Capture1.JPG
    Capture1.JPG
    37.6 KB · Views: 99

MajP

You've got your good things, and you've got mine.
Local time
Today, 00:53
Joined
May 21, 2018
Messages
8,463
The code you copied had only 5 minute increments, but you have 1 minute increments
minutesVar = Int(minutesVar / 5)
minutesVar = minutesVar * 5
 

oxicottin

Learning by pecking away....
Local time
Today, 00:53
Joined
Jun 26, 2007
Messages
851
Never mind I think I got it working... I removed:

'minutesVar = Int(minutesVar / 5)
'minutesVar = minutesVar * 5

I also changed a few other things....
 

oxicottin

Learning by pecking away....
Local time
Today, 00:53
Joined
Jun 26, 2007
Messages
851
Here is a seems to be working version....
 

Attachments

  • Option Time Picker_v2.accdb
    960 KB · Views: 106

MajP

You've got your good things, and you've got mine.
Local time
Today, 00:53
Joined
May 21, 2018
Messages
8,463
I did not look at the new version, but you made it way too hard by turning it into a string then parsing the string. You should have kept it as a date the whole way through.
 

oxicottin

Learning by pecking away....
Local time
Today, 00:53
Joined
Jun 26, 2007
Messages
851
Majp, I ran into another issue... If I wanted to run the picker from a subform it wont work because its only looking for a form.

Code:
Private Sub lastControl()
    
'Store the last field used before opening this form
    lastFrm = Screen.ActiveForm.Name
    lastCtl = Screen.ActiveControl.Name
    
    fieldTime = Format(Forms(lastFrm).Form(lastCtl), "Short Time")
    
End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 00:53
Joined
May 21, 2018
Messages
8,463
@oxicottin
IMO when doing things like this it is a lot easier and more flexible to just pass in a reference to the control that called it. Not a name, but the object pointer to the control. No need for a this complicated openargs method. This is also much better than using activecontrol because that cannot be verify precise depending on where focus goes under certain conditions. Often you make get the wrong control.

You can simply use a variable in your time form, but I like to use properties because it is a little cleaner.
Simply pop open your form and specifically tell it what control called it. No need to pass information about the form because you have passed in very specific info about the control. Same if I am passing a form I would pass the form object not the name of the string and then have the called procedure have to get the object.

Exampl: Call SomeProcedure (Me) not Call SomeProcedure ("myFormName")
public SomeProcedure (Frm as Access.Form) not public SomeProcedure( FrmName as String)

in your time form
Code:
Private mCallingControl As Access.Control

Public Property Get CallingControl() As Access.Control
  Set CallingControl = mCallingControl
End Property

Public Property Set CallingControl(ByVal TheControl As Access.Control)
  Set mCallingControl = TheControl
End Property
But as said the properties are not needed you can just call the class variable if you wanted.

Code:
Dim frm As Access.Form
  DoCmd.OpenForm "TimePicker_v002", , , , , , sOpenArgs$
  Set frm = Forms("TimePicker_v002")
  'pass in the calling control
  Set frm.CallingControl = Me.txtTimeShort
  'verify it works.
  MsgBox frm.CallingControl.Name
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 12:53
Joined
May 7, 2009
Messages
19,169
another Time Picker.
the code is not yet cleaned.
 

Attachments

  • TimePicker.zip
    146.6 KB · Views: 92

oxicottin

Learning by pecking away....
Local time
Today, 00:53
Joined
Jun 26, 2007
Messages
851
Thanks for the versions! Here is a modified version of the original, how do I get the bound textboxs value from the main form OR its subform to show as the hours and minutes instead of the current time.
 

Attachments

  • Option Time Picker_v2.1.accdb
    1.2 MB · Views: 108

MajP

You've got your good things, and you've got mine.
Local time
Today, 00:53
Joined
May 21, 2018
Messages
8,463
Code:
Public Function fncShowTimePicker(ByRef txt As TextBox)
'Purpose:   Open the time picker form, identifying the text box to return the length to.
'Arguments: txt = the text box to return the time to.
    Set gtxtTimeTarget = txt
    DoCmd.OpenForm FormName:="frm_TimePicker"
    Dim frm As Access.Form
    Set frm = Forms("frm_timePicker")
    Set frm.CallingControl = txt
End Function

Code:
Option Compare Database
Option Explicit

Dim intHours As Integer, intMinutes As Integer

Private mCallingControl As Access.Control

Public Property Get CallingControl() As Access.Control
  Set CallingControl = mCallingControl
End Property

Public Property Set CallingControl(ByVal TheControl As Access.Control)
  Set mCallingControl = TheControl
  SetTime
End Property


Private Sub cmdConfirm_Click()
    Me.CallingControl.Value = Me.txtTargetTime.Value
    DoCmd.Close acForm, Me.Name
End Sub

Private Sub cmdClose_Click()
    DoCmd.Close
End Sub
Private Sub SetTime()
   Dim intStartPos As Integer
   Dim varTargetTime As Date
'Get the time from the previous form if it exists _
    and if not use the current time for the varTargetTime
    If Not IsDate(Me.CallingControl.Value) Then
        varTargetTime = Format(Now, "HH:MM")
    Else
       varTargetTime = CallingControl.Value
    End If
    
    
    intStartPos = IIf(Mid(varTargetTime, 2, 1) = ":", 1, 2)
    
    intHours = Left(varTargetTime, intStartPos)
    Me![optGroupHours] = intHours
    
    intMinutes = Mid(varTargetTime, intStartPos + 2, 2)
    Me![OptGroupMinutes] = intMinutes
    
'Update this forms textbox txtTargetTime
    Call updateTargetTime
    

End Sub
Private Sub optGroupHours_Click()
    intHours = Me!optGroupHours
    Call updateTargetTime
End Sub

Private Sub OptGroupMinutes_Click()
    intMinutes = Me!OptGroupMinutes
    Call updateTargetTime
End Sub

Private Sub updateTargetTime()
    Me.txtTargetTime = Format(intHours, "00") & ":" & Format(intMinutes, "00")
End Sub
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 00:53
Joined
May 21, 2018
Messages
8,463
@oxicottin
This is a little cleaner. Instead of converting your times to strings leave them as date time using timeserial as a helper.

Code:
Private mCallingControl As Access.Control

Public Property Get CallingControl() As Access.Control
  Set CallingControl = mCallingControl
End Property

Public Property Set CallingControl(ByVal TheControl As Access.Control)
  Set mCallingControl = TheControl
  SetTime
End Property
Private Sub cmdConfirm_Click()
    Me.CallingControl.Value = Me.txtTargetTime.Value
    DoCmd.Close acForm, Me.Name
End Sub
Private Sub cmdClose_Click()
    DoCmd.Close
End Sub
Private Sub SetTime()
   Dim varTargetTime As Date
'Get the time from the previous form if it exists _
    and if not use the current time for the varTargetTime
    If Not IsDate(Me.CallingControl.Value) Then
        varTargetTime = DateSerial(Year(Now), Month(Now), 0)
    Else
       varTargetTime = CallingControl.Value
    End If
    Me.txtTargetTime = varTargetTime
    Me![optGroupHours] = Hour(varTargetTime)
    Me![OptGroupMinutes] = Minute(varTargetTime)
End Sub
Private Sub optGroupHours_Click()
    Me.txtTargetTime = TimeSerial(Me.optGroupHours, Minute(Me.txtTargetTime), 0)
End Sub
Private Sub OptGroupMinutes_Click()
    Me.txtTargetTime = TimeSerial(Hour(Me.txtTargetTime), Me.OptGroupMinutes, 0)
End Sub

Makes it a bit shorter code. Passing a specific reference to a the calling control gives some more flexibility. You can call the code from anywhere like a command button. Or reference a subform control from the main form. The screen.active control is not precise and there are times when the intended calling control loses focus and the referenced control may not be what you think.
 

Users who are viewing this thread

Top Bottom