Entering wrong date ie 30-2-19 (30-Feb-2019) in txtDate.text it displays as 19-Feb-1930 instead of showing error msg

SamDsouza

Board Regular
Joined
Apr 16, 2016
Messages
205
Hello

Have an issue. when entering wrong date ie 30-2-19 (30-Feb-2019) in txtDate.text it displays as 19-Feb-1930. i dont know why.
Infact the below code has the msg of Invalid Date. but it by passes and shows wrong date altogether. The coding should not display any wrong dates

Code:
Option Explicit
Dim disableEvents As Boolean
Public dDate As Date

Private Sub txtDate_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
   Worksheets("Sheet1").Columns(1).NumberFormat = "dd-mmm-yyyy"

    If txtDate.Value = vbNullString Then
       Exit Sub
    ElseIf Not IsDate(txtDate.Value) Then
       Cancel = True  
        MsgBox "Invalid date, please re-enter", vbCritical
        txtDate.Value = vbNullString
        txtDate.SetFocus
        Worksheets("Sheet1").Range("A2").Value = Format(CDate(txtDate.Value), "dd-mmm-yyyy")
     Exit Sub
End If
     dDate = DateSerial(Year(Date), Month(Date), Day(Date))
     txtDate.Value = Format(CDate(txtDate.Value), "dd-mmm-yyyy")
     Worksheets("Sheet1").Range("A2").Value = Format(CDate(txtDate.Value), "dd-mmm-yyyy")
End Sub

Private Sub UserForm_Initialize()
  Load userform1
  userform1.Show vbModeless
  With txtDate
     .Value = Worksheets("Sheet1").Range("A2").Value
     If .Value <> "" Then .Value = Format(.Value, "dd-mmm-yyyy")
  End With
 End Sub
SamD
 
Last edited:

Excel Facts

Is there a shortcut key for strikethrough?
Ctrl+S is used for Save. Ctrl+5 is used for Strikethrough. Why Ctrl+5? When you use hashmarks to count |||| is 4, strike through to mean 5.
You could add another validation to verify if the capture has 10 characters. For example:

Code:
ElseIf Not IsDate(txtDate.Value) Or Len(txtDate.Value) <> 10 Then
 
Upvote 0
Thanks DanteAmor for your suggestion for adding another validation. Whats happening is i ve to compulsory add 10 digits
What is desired is Entering dates with the flexibility
as
1. 25-01-01 result as 25-Jan-2001
2. 2-3-14 result as 02-Mar-2014
3.02-03-2014 result as 02-Mar-2014
4. 02-3-14 result as 02-Mar-2014
5. 2-03-14 result as 02-Mar-2014

only if wrong date entered by mistake then Error Msg
 
Last edited:
Upvote 0
Try the following. This is where you might have better control and validation
Code:
Private Sub txtDate_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)

If txtDate.Value = vbNullString Then
   txtDate.Value = Format(CDate(txtDate.Value), "dd-mmm-yyyy")
    Exit Sub
ElseIf Not IsDate(txtDate.Value) Or Mid(txtDate.Value, 4, 2) > 12 Then
        Cancel = True
        MsgBox "Invalid date, please re-enter", vbCritical
        Cancel = False
        txtDate.Value = vbNullString
        txtDate.SetFocus
    Exit Sub
ElseIf Not IsDate(txtDate.Value) Or Mid(txtDate.Value, 3, 1) <> "-" Then
        MsgBox "You missed the Hyphen before the Month, Kindly Renter Date", vbCritical
        'Application.EnableEvents = false
        txtDate.Value = vbNullString
        txtDate.SetFocus
        'Application.EnableEvents = True
        Exit Sub


ElseIf Not IsDate(txtDate.Value) Or Mid(txtDate.Value, 6, 1) <> "-" Then
        MsgBox "You missed the Hyphen before the Year, Kindly Renter Date", vbCritical
        'Application.EnableEvents = false
        txtDate.Value = vbNullString
        txtDate.SetFocus
        'Application.EnableEvents = True
        Exit Sub
End If
dDate = DateSerial(Year(Date), Month(Date), Day(Date))
txtDate.Value = Format(CDate(txtDate.Value), "dd-mmm-yyyy")
NimishK
 
Last edited:
Upvote 0
How is excel to know what year you mean when you just enter 2 digit year? What if the year was say 99? Is that 1999 or 2099? Your initial problem was excel trying to be helpful knowing there isnt 30 days in Feb. A 4 digit year solves that so as long as we know your rules for 2 digit years they can be altered.
 
Upvote 0
Thanks NimishK. Tried yours but then i've to put Two digit Nos for Day and Month and Four Digit for year. I think 4 digit for year is good idea rather than 2 digit year.
Also the same suggestion from Steve the Fish for 4 digit year.
Can you guys come for solution for 1 digit day and 1 digit month and 2 digit year. This is to speed up when entring the date.
 
Upvote 0
If you enter 2 digit year which year is it? Is 99 the year 1999 or 2099? Is 30 the year 1930 or 2030? This is what is causing your problem but you need to answer the question. I cant answer that for you.
 
Upvote 0
Split the string and use the components to build a valid date.
For instance if year is >=50 then century = 19 else it's 20

That should last you a while?

HTH
 
Upvote 0
Steve the Fish
If you enter 2 digit year which year is it? Is 99 the year 1999 or 2099? Is 30 the year 1930 or 2030? This is what is causing your problem but you need to answer the question. I cant answer that for you.
Yes, Agreed definately you cant answer for me. Your point is worth considering 2 digit or 4 diigt.
As users we tend to type 2 digit for Speed up. Well considering now will be 2000 onwards if 2 digit

Welshgasman
Thanks for your reply. Looking for common and lasting solution.

SamD
 
Upvote 0
What I was trying to point out that eventually the 2 digit year test is going to fail. By that time you would likely be using new software. :D
Only you know your data, so if there is a chance of someone entering 01-01-99 then which century is that.? I would hazard a guess as to 1999. If you are putting dates in as 2099 how long do you think your software is going to run?

If it were me, I would create a dedicated function that would work out the correct date, allow for -,. & / delimiters etc and call in the AfterUpdate event of the controls.
Once written and tested, that should be it.
Steve the Fish

Yes, Agreed definately you cant answer for me. Your point is worth considering 2 digit or 4 diigt.
As users we tend to type 2 digit for Speed up. Well considering now will be 2000 onwards if 2 digit

Welshgasman
Thanks for your reply. Looking for common and lasting solution.

SamD
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

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