Date Validation in textbox

rksample

New Member
Joined
Oct 13, 2010
Messages
47
Hi Friends,

I am struggling with a small code.

I have a text box in which date will be entered. I am using the following code to validate and format the date.

Code:
Private Sub txtTDate_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

    If IsDate(txtTDate.Value) Or txtTDate.Value > Date Then
        sDate = DateSerial(Year(Date), Month(Date), Day(Date))
        txtTDate.Value = format(txtTDate.Value, "dd/mm/yyyy")
        sDate = txtTDate.Value
    Else
        MsgBox ("You can't enter past date. Please modify!")
        txtTDate.SetFocus
        Exit Sub
    End If
    
End Sub

I expected it to return error if the date entered is less than Current(System's) date. This is not working. Please help me!

Regards,
Ravi.
 

Excel Facts

Which came first: VisiCalc or Lotus 1-2-3?
Dan Bricklin and Bob Frankston debuted VisiCalc in 1979 as a Visible Calculator. Lotus 1-2-3 debuted in the early 1980's, from Mitch Kapor.
<!--[if gte mso 9]><xml> <w:WordDocument> <w:View>Normal</w:View> <w:Zoom>0</w:Zoom> <w:DoNotOptimizeForBrowser/> </w:WordDocument> </xml><![endif]--> Here’s my take on it.

Private Sub CommandButton1_Click()
Select Case IsDate(Me.TextBox1)
Case False
MsgBox "Not a valid date", vbCritical
Case Else
Select Case CDate(Me.TextBox1) < Now()
Case True
MsgBox "Date must be after " & Format(Now(), "mmm/dd/yy")
Case Else
MsgBox "Ta Dar"
End Select
End Select
SendKeys "+{TAB}"
End Sub
 
Upvote 0
Thanks TLowry. It worked well. Only glitch is that Now() can't be used because that considers time as well and current date will not be accepted.

I replaced it with Date and everything is working fine. Once again thanks for your help.

Regards,
Ravi.
 
Upvote 0

Forum statistics

Threads
1,223,243
Messages
6,170,967
Members
452,371
Latest member
Frana

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