Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SpinButton changes date format "ddd dd-mmm yy" 1

Status
Not open for further replies.

davedave24

Programmer
Aug 26, 2010
113
GB
Hi, how can I make a spinbutton alter a TextBox with the date format "ddd dd-mmm yy" (Tue 19-Feb 13).

I can make it work for just the date (and another textbo for the time works fine), but how would I add the day of week into it?
Code:
Private Sub UserForm_Initialize()
    txtDate.Text = Format(Date, "dd-mmm yy")
    txtTime.Text = Format(Time, "hh:00")
End Sub

Private Sub spnDate_SpinDown()
    txtDate = Format(DateAdd("d", -1, txtDate), "dd-mmm yy")
End Sub

Private Sub spnDate_SpinUp()
    txtDate = Format(DateAdd("d", 1, txtDate), "dd-mmm yy")
End Sub

Private Sub spnTime_SpinDown()
    txtTime.Value = Format(TimeValue(txtTime.Text) - TimeSerial(0, 30, 0), "hh:mm")
End Sub

Private Sub spnTime_SpinUp()
    txtTime.Value = Format(TimeValue(txtTime.Text) + TimeSerial(0, 30, 0), "hh:mm")
End Sub
 
What happens if you replace all "dd-mmm yy" with "ddd dd-mmm yy" ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Vba needs to convert text to date, it does not recognise first "ddd" in the string, so the problem. You can remove the first part of date in the string.
It would be simpler to add DateTime variable in the userform instead:
Code:
Public dtDateTime As Date

Private Sub UserForm_Initialize()
    dtDateTime = Date + TimeValue(Format(Time, "hh:00"))
    txtDate = Format(dtDateTime, "ddd dd-mmm yy")
    txtTime.Text = Format(dtDateTime, "hh:nn")
End Sub

Private Sub spnDate_SpinDown()
    dtDateTime = dtDateTime - 1
    txtDate = Format(dtDateTime, "ddd dd-mmm yy")
End Sub

Private Sub spnDate_SpinUp()
    dtDateTime = dtDateTime + 1
    txtDate = Format(dtDateTime, "ddd dd-mmm yy")
End Sub

Private Sub spnTime_SpinDown()
    dtDateTime = dtDateTime - TimeSerial(0, 30, 0)
    txtTime.Value = Format(dtDateTime, "hh:mm")
End Sub

Private Sub spnTime_SpinUp()
    dtDateTime = dtDateTime + TimeSerial(0, 30, 0)
    txtTime.Value = Format(dtDateTime, "hh:mm")
End Sub
You can consider replacing textboxes by labels too, the user will have no access to controls' contents.

combo
 
Thanks for the star, two notices after reading my post again:
[ul]
[li]I copied format "hh:mm" in spnTime event procedure. The proper character for minute is "n",[/li]
[li]dtDateTime variable stores both date and time, so the day changes when reaching midnight. It would be better to create one procedure that adjust both txtTime and txtDate, called after any change of dtDateTime.[/li]
[/ul]

combo
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top