PDA

View Full Version : [SOLVED:] Problem with format "12/31/1899"



pineapples00
08-14-2019, 12:01 PM
Hi,

I need help with formatting the time in ActiveX textboxes . For example, when I enter "22:00" in textbox7, all the other textboxes start out as "12/31/1899". The time calculated is correct, I just want to get rid of the "12/31/1899".

Looking all over online and can't find anything to fix it. Any help is appreciated!

Thanks!

Heres the code:

Private Sub ToggleButton20_Click()
If ToggleButton20.Value = True Then
ToggleButton20.BackColor = vbGreen

TextBox8.Value = TimeValue(TextBox7.Value) + TimeValue("3:25")
TextBox9.Value = TimeValue(TextBox7.Value) + TimeValue("6:50")
TextBox10.Value = TimeValue(TextBox7.Value) + TimeValue("10:15")
TextBox11.Value = TimeValue(TextBox7.Value) + TimeValue("13:40")
TextBox12.Value = TimeValue(TextBox7.Value) + TimeValue("17:05")
TextBox13.Value = TimeValue(TextBox7.Value) + TimeValue("22:30")

end if
End sub

Bob Phillips
08-14-2019, 12:27 PM
Try this alternative


TextBox8.Value = Application.Text(TimeValue(TextBox7.Value)+ TimeValue("3:25"),"[h]:mm")

pineapples00
08-14-2019, 01:01 PM
xld,

I tried the code, but it pops up with a compile error.

thanks for th response tho.

Paul_Hossler
08-14-2019, 01:21 PM
Textboxes contain strings so I think CDate is better

I'd use TimeSerial to add 3 hours and 25 minutes (for example)

Format the strings before you put them into the textboxes

Something like this maybe



Sub test()
Dim TB7 As String, TB8 As String, TB9 As String

TB7 = "22:00"
TB8 = CDate(TB7) + TimeSerial(3, 25, 0)
TB9 = CDate(TB8) + TimeSerial(6, 50, 0)

TB7 = Format(TB7, "[h]h:mm")
TB8 = Format(TB8, "[h]h:mm")
TB9 = Format(TB9, "[h]h:mm")



MsgBox TB7 & vbCrLf & TB8 & vbCrLf & TB9
End Sub

pineapples00
08-14-2019, 01:43 PM
Thanks for the help Paul! I played with the code and I only used the format function to get rid of the "12/31/1899". And it works!

Thanks for all your help!

Here is the code:

TextBox8.Value = TimeValue(TextBox7.Value) + TimeValue("3:25")
TextBox9.Value = TimeValue(TextBox7.Value) + TimeValue("6:50")


TextBox8 = Format(TextBox8.Value, "[h]h:mm")
TextBox9 = Format(TextBox9.Value, "[h]h:mm")

John Wilson
08-14-2019, 01:48 PM
You could also try


Private Sub ToggleButton20_Click()
If ToggleButton20.Value = True Then
ToggleButton20.BackColor = vbGreen


TextBox8.Value = Format(TimeValue(TextBox7.Value) + TimeValue("3:25"), "hh:mm")
TextBox9.Value = Format(TimeValue(TextBox7.Value) + TimeValue("6:50"), "hh:mm")
'etc


End If
End Sub

Bob Phillips
08-14-2019, 02:48 PM
You could also try


Private Sub ToggleButton20_Click()
If ToggleButton20.Value = True Then
ToggleButton20.BackColor = vbGreen

TextBox8.Value = Format(TimeValue(TextBox7.Value) + TimeValue("3:25"), "hh:mm")
TextBox9.Value = Format(TimeValue(TextBox7.Value) + TimeValue("6:50"), "hh:mm")
'etc

End If
End Sub

Problem with that is it does not show > 24 hours, 22:00 + 3:25 is 24:25, but that shows as 1:25.

Bob Phillips
08-14-2019, 02:53 PM
Format the strings before you put them into the textboxes

Format(TB7, "[h]h:mm") shows it as 1:25 on my machine Paul, [h] is not valid in Format, that is why I used Application.Text.

Paul_Hossler
08-14-2019, 05:32 PM
Using Win10 Pro, Office 365, 32 bit the only thing [h] seems to do is surpress a leading '0' in Hours

Adding 'ampm' formats using 12 hour clock.

24818248192482024821