Adding "0" in front of hour

D

Deki

This is my code:

Private Sub cmdEdit_Click()
Dim get_hour As String
Me.cmdPrevious.Visible = True
Me.cmdNext.Visible = True
Me.cboOwner.ControlSource = "OwnerID"
Me.dtDate.ControlSource = "DateWorkedOnProject"
get_hour = Hour(Me.txtTimeStart.Value)
If Len(get_hour) = 1 Then
get_hour = "0" & get_hour
End If
Me.cboHourStart.Value = get_hour
Me.cboMinuteStart.Value = Mid(Me.txtTimeStart, 3, 2)
Me.cboAmPMStart.Value = Right(Me.txtTimeStart, 2)
get_hour = Hour(Me.txtTimeEnd.Value)
If Len(get_hour) = 1 Then
get_hour = "0" & get_hour
End If
Me.cboHourEnd.Value = get_hour
Me.cboMinuteEnd.Value = Mid(Me.txtTimeEnd, 3, 2)
Me.cboAmPmEnd.Value = Right(Me.txtTimeEnd, 2)
End Sub

Time format for txtTimeStart is hh:nnAM/PM.
How can I add "0" to cboHourStart and/or cboHourEnd if it is a single digit,
but I don't want time to be displayed as military time (2:00 vs 14:00). As
you can see, I tried with Hour but I'm getting military time.
Thanks in advance
 
D

Douglas J. Steele

Dim lngHour As Long
Dim get_hour As String

lngHour = Hour(Me.txtTimeStart.Value)
If lngHour > 12 Then
lngHour = lngHour - 12
End If
get_hour = Format(lngHour, "00")
 
6

'69 Camaro

Hi, Deki.

If I understand your question correctly, then you want to display two digits
in the combo box, no matter whether it's AM or PM. To avoid the military
time that the Hour( ) function produces, subtract 12 from it. Try this
snippet:

get_hour = Hour(Me.txtTimeStart.Value)

If (get_hour > 12) Then
get_hour = get_hour - 12
End If

If Len(get_hour) = 1 Then
get_hour = "0" & get_hour
End If
Me.cboHourStart.Value = get_hour

HTH.
Gunny

See http://www.QBuilt.com for all your database needs.
See http://www.Access.QBuilt.com for Microsoft Access tips.

(Please remove ZERO_SPAM from my reply E-mail address so that a message will
be forwarded to me.)
- - -
If my answer has helped you, please sign in and answer yes to the question
"Did this post answer your question?" at the bottom of the message, which
adds your question and the answers to the database of answers. Remember that
questions answered the quickest are often from those who have a history of
rewarding the contributors who have taken the time to answer questions
correctly.
 
D

Deki

Thank you guys! I was hoping that there might be some predefined function
that I can use instead of Hour, but I will go with your suggestions.
 
Top