leopardhawk
Well-known Member
- Joined
- May 31, 2007
- Messages
- 611
- Office Version
- 2016
- Platform
- Windows
Hello forum friends, I have a problem with dates on a years-long project and I can't figure out a solution.
I have a VBA userForm where users will be entering dates in four textBoxes. I will focus only on one textBox in this plea for help and it's called DOBTextBox where the user enters their date of birth. Here is the VBA code for the DOBtextBox.
If I type 'July 7 80' in the DOBTextBox and tab-key to the next field, the DOBTextBox displays '7/7/1980', and when the OK commandbutton is pressed, it sends the date to my worksheet into the appropriate cell which displays 'July 7, 1980 because I have formatted that cell to display the date as long-date (without the *).
However, if I type 'July 8 80' in the same DOBTextBox and tab-key to the next field, the DOBTextBox displays '8/7/1980', and when the OK commandbutton is pressed, it sends the date to my worksheet into the appropriate cell which now displays 'August 7, 1980' even though I typed July 8 80 in the textbox...!!!!! It is flipping the day and month for some reason.
I don't understand why it would do this, especially when I enter the date in the text box as an actual long date... Gotta have something to do with my VBA code but I have no idea how to address this problem. Any help appreciated. Thanks!
p.s. the third IF statement was added to prevent anyone from inadvertently entering the year as three digits. The code accepts 2-digit and 4-digit dates.
I have a VBA userForm where users will be entering dates in four textBoxes. I will focus only on one textBox in this plea for help and it's called DOBTextBox where the user enters their date of birth. Here is the VBA code for the DOBtextBox.
VBA Code:
Private Sub DOBTextBox_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim dt As Date, tx As String
With DOBTextBox
If .Value = "" Then Exit Sub
If IsDate(.Value) Then
tx = .Value
dt = Format(.Value, "mm/dd/yyyy")
If (dt > Date) Or (Year(CDate(.Value)) < 1000) Then
MsgBox "Please enter the year as four digits."
Cancel = True
Else
.Value = dt
End If
Else
MsgBox "Please enter a valid date!"
Cancel = True
.Value = Empty
End If
End With
End Sub
However, if I type 'July 8 80' in the same DOBTextBox and tab-key to the next field, the DOBTextBox displays '8/7/1980', and when the OK commandbutton is pressed, it sends the date to my worksheet into the appropriate cell which now displays 'August 7, 1980' even though I typed July 8 80 in the textbox...!!!!! It is flipping the day and month for some reason.
I don't understand why it would do this, especially when I enter the date in the text box as an actual long date... Gotta have something to do with my VBA code but I have no idea how to address this problem. Any help appreciated. Thanks!
p.s. the third IF statement was added to prevent anyone from inadvertently entering the year as three digits. The code accepts 2-digit and 4-digit dates.