PDA

View Full Version : Button with RESET function



johnnyexit
09-16-2015, 12:19 AM
Hi,

can somebody please help me with the file in attachment.
This is counter of days and it works fine when i put the date in VBA. But
I want when somebody opet this file have a field where he can write a date and start to count
from this date(today) or maybe if it is possible button with reset function to today date.

Thanks in advice!:help:help:help

SamT
09-16-2015, 01:12 PM
Dim myVar As Date

MyVar = CDate(InputBox "Please enter a date in the format d-mmm-yy.")

johnnyexit
09-16-2015, 02:05 PM
Thank you very much. Now if you can help me with these:
my textbox variable is a Date. And if a suddenly press any letter on keyboard everything is freeze and put an error.

How to make if is not a date in dd.mm.yyyy to ignore the error and put a message wrong format of date?

Public startDateTime As Date

startDateTime = UserForm1.TextBox1

SamT
09-17-2015, 08:27 AM
my textbox variable is a Date
TextBox Value is a String that looks like a date to humans.

And if a suddenly press any letter on keyboard everything is freeze and put an error.
Need to see the workbook. Use the "Go Advanced" option. Below the message editor is "Manage Attachments."

How to make if is not a date in dd.mm.yyyy to ignore the error and put a message wrong format of date?
In TextBox_Change Sub, Read the TextBox Value, Convert to Date, Convert to String, Set Value to String.

'Something like this
SubTextBox1_Change()
On Error GotTo BadDateMsg

TextBox1.Value = CStr(Format(CDate(TextBox1.Value), "dd.mm.yyyy"))
Exit Sub

BadDateMsg:
MsgBox "You must enter a date" 'must be a date in any format that VBA recognises.
TextBox1.Value = ""
TextBog1.SetFocus
EndSub


Public startDateTime As Date

startDateTime = CDate(UserForm1.TextBox1.Value)

johnnyexit
09-17-2015, 12:02 PM
TextBox Value is a String that looks like a date to humans.

And if a suddenly press any letter on keyboard everything is freeze and put an error.
Need to see the workbook. Use the "Go Advanced" option. Below the message editor is "Manage Attachments."

In TextBox_Change Sub, Read the TextBox Value, Convert to Date, Convert to String, Set Value to String.

'Something like this
SubTextBox1_Change()
On Error GotTo BadDateMsg

TextBox1.Value = CStr(Format(CDate(TextBox1.Value), "dd.mm.yyyy"))
Exit Sub

BadDateMsg:
MsgBox "You must enter a date" 'must be a date in any format that VBA recognises.
TextBox1.Value = ""
TextBog1.SetFocus
EndSub


Public startDateTime As Date

startDateTime = CDate(UserForm1.TextBox1.Value)


Please SamT can you please help me with that. I am sending in attachment a file. Can you please solve me this message and restar button to reset a date to "today"--> start counting from zero!

SamT
09-17-2015, 01:40 PM
I have never used PowerPoint, but I did look at the UserForm code and this version effectively sets the Variable. Note! There is no TextBox code used.. VBA does not recognize dd.mm.yyyy as a good date format.

Private Sub CommandButton1_Click()

If Not IsDate(TextBox1.Value) Then GoTo BadDateMsg

startDateTime = CDate(TextBox1.Value)
Unload Me
Exit Sub

BadDateMsg:
MsgBox "You must enter a date" 'must be a date in any format that VBA recognises.
TextBox1.Value = ""
TextBox1.SetFocus

End Sub


Private Sub UserForm_Initialize()
''The form message always shows a date two weeks before NOW() in a good Format for VBA.
Dim DateStr As String
DateStr = Format(DateAdd(ww, -2, Now), "d-mmm-yyyy")
Label1.Caption = "Please enter a date in format: " & DateStr
End Sub

Paul_Hossler
09-20-2015, 08:09 AM
I didn't go through all the code, but try changing the textbox event handler




Private Sub TextBox1_AfterUpdate()
startDateTime = CDate(UserForm1.TextBox1.Text)
End Sub


The Date format is subject to Locale settings (long story) so when I in the US enter 12/25/2015 it is properly converted to a date value in the startDateTIme variable