Force TextBox to be Date Format

BriggsMonkeyHero

New Member
Joined
Apr 28, 2017
Messages
6
https://www.mrexcel.com/forum/excel-questions/317066-force-textbox-date-format.html#post1556884

In reference to this post, I have used the code, except if someone were to just enter 06 and nothing after, it still allows the code to go through. I am looking for an error message to pop up in the case that they don't enter the full date as such: mm/dd/yyyy

Code:
Private Sub DateTextBox_Change()
    Dim Char As String
    Dim x As Date
    Dim y As Date
    Char = Right(DateTextBox.Text, 1)
    Select Case Len(DateTextBox.Text)
    Case 1 To 2, 4 To 5, 7 To 11
        If Char Like "#" Then
            If Len(DateTextBox) = 11 Then
                On Error Resume Next
                x = DateValue(DateTextBox.Text)
                y = DateSerial(Right(DateTextBox, 4), Mid(DateTextBox, 4, 2), Left(DateTextBox, 2))
                If Err = 0 And x = y Then
                    On Error GoTo 0
                    Exit Sub
                Else
                    Err.Clear
                    On Error GoTo 0
                    DateTextBox.SelStart = 0
                    DateTextBox.SelLength = Len(DateTextBox.Text)
                    MsgBox "Please enter a valid date in the form dd/mm/yyyy", vbCritical + vbOKOnly, "Error"
                    Exit Sub
                End If
            Else
                Exit Sub
            End If
        End If
    Case 3, 6
        If Char Like "/" Then Exit Sub
    End Select
    Beep
    On Error Resume Next
    DateTextBox.Text = Left(DateTextBox.Text, Len(DateTextBox.Text) - 1)
    DateTextBox.SelStart = Len(DateTextBox.Text)
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Modify this
Code:
x = DateValue(DateTextBox.Text)
To this
Code:
x = CDate(DateTextBox.Text)
and see if it works for you.
 
Upvote 0
That didn't seem to work. It still allows me to simply enter 6 into the date and move on and submit. I am trying to disallow any entry without the proper format.
 
Upvote 0
That didn't seem to work. It still allows me to simply enter 6 into the date and move on and submit. I am trying to disallow any entry without the proper format.

What is the criteria for enterihng the date? i.e. Is it the current date, a future date or a past date? Would the date be older than 1 year or extend beyond 1 year from the current date? You can set a criteria with an If statement to force the date value to be within certain limits which would prevent the user from just putting any number in.
 
Upvote 0
Maybe you could use something like this.

Code:
If Not DateTextBox.Text Like "##/##/####" Then
    MsgBox "You must enter the date in 'mm/dd/yyyy' format."
End If
 
Upvote 1

Forum statistics

Threads
1,224,820
Messages
6,181,162
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top