Connecting Tech Pros Worldwide Help | Site Map

display user friendly message for date validation

Newbie
 
Join Date: Apr 2007
Posts: 6
#1: 4 Weeks Ago
Dear friends,

I am using the following code in the afterupdate event of my textbox:
Expand|Select|Wrap|Line Numbers
  1.  If Not IsDate([MyTextBox]) Then
  2.      MsgBox "Invalid date!"
  3. Else
  4.      'do nothing, date is OK
  5. End If
My problem is that if the user entered false date such as 15-15-2009
The Ms Access error message is displayed "The value you entered is not valid for this field" instead of my own message which is "invalid date!"
How this problem can be solved?
Any help will be highly appreciated
best answer - posted by ajalwaysus
Disregard my last post. I tested this myself and found myself surprised and a little annoyed at Microsoft, it appears that a form validation happens before the field validation which means that you need to catch this on the Form_Error, sorry for the confusion, but my logic is still correct about before and after updates, the difference is that Form_Error happens before either.

Here is the code you will need to properly catch this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Error(DataErr As Integer, Response As Integer)
  2.     Select Case DataErr
  3.         Case 2113
  4.             If Me.ActiveControl.Name = "MyTextBox" Then
  5.                 MsgBox "Invalid date!"
  6.                 Response = acDataErrContinue
  7.             End If
  8.     End Select
  9. End Sub
This code triggers on Form_Error, it then checks the error number, which in this case is 2113 which means an invalid entry in a field, then it will check that we are on "MyTextBox", if it is it will give your error message and then continue on.

Also, please make sure the before and after update subs are gone so that you aren't replicating effort.

If you have any questions don't hesitate to ask.

-AJ
Expert
 
Join Date: Jul 2009
Location: KY
Posts: 244
#2: 4 Weeks Ago

re: display user friendly message for date validation


If this is a bound field to a date field in your table, then the reason you are getting the error is because you are allowing the update to the table to take place and then checking it afterupdate, so you are getting access's error because it supersedes your check. If you want to catch this before it tries to save to your table you need to do this on beforeupdate.

Like this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub MyTextBox_BeforeUpdate(Cancel As Integer)
  2.   If Not IsDate([MyTextBox]) Then 
  3.     MsgBox "Invalid date!"
  4.     Cancel = True
  5.   Else
  6.     'do nothing, date is OK
  7.    End If
  8. End Sub
Let me know if this works for you, or if you need me to clarify the issue further.

-AJ
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,672
#3: 4 Weeks Ago

re: display user friendly message for date validation


AJ is Spot On (of course). I would simply add that the Else part of the If is not necessary in this instance, and should probably be omitted.

Welcome to Bytes!
Newbie
 
Join Date: Apr 2007
Posts: 6
#4: 4 Weeks Ago

re: display user friendly message for date validation


Thanks ajalwaysus for your hard effort.
But I'm still having the same problem

Remark : regarding your point whether my text box is bound field to a date field in my table the answer is yes
Expert
 
Join Date: Jul 2009
Location: KY
Posts: 244
#5: 4 Weeks Ago

re: display user friendly message for date validation


I need to see the code, as this should work without issue. Please copy and paste your code as it appears in your DB to this thread and let me take a look, also make sure you take out the afterupdate sub as it is no longer needed.

-AJ
Expert
 
Join Date: Jul 2009
Location: KY
Posts: 244
#6: 4 Weeks Ago

re: display user friendly message for date validation


Disregard my last post. I tested this myself and found myself surprised and a little annoyed at Microsoft, it appears that a form validation happens before the field validation which means that you need to catch this on the Form_Error, sorry for the confusion, but my logic is still correct about before and after updates, the difference is that Form_Error happens before either.

Here is the code you will need to properly catch this:
Expand|Select|Wrap|Line Numbers
  1. Private Sub Form_Error(DataErr As Integer, Response As Integer)
  2.     Select Case DataErr
  3.         Case 2113
  4.             If Me.ActiveControl.Name = "MyTextBox" Then
  5.                 MsgBox "Invalid date!"
  6.                 Response = acDataErrContinue
  7.             End If
  8.     End Select
  9. End Sub
This code triggers on Form_Error, it then checks the error number, which in this case is 2113 which means an invalid entry in a field, then it will check that we are on "MyTextBox", if it is it will give your error message and then continue on.

Also, please make sure the before and after update subs are gone so that you aren't replicating effort.

If you have any questions don't hesitate to ask.

-AJ
NeoPa's Avatar
Administrator
 
Join Date: Oct 2006
Location: London - UK
Posts: 15,672
#7: 4 Weeks Ago

re: display user friendly message for date validation


Well, I've learned something tonight. Good one AJ.
Newbie
 
Join Date: Apr 2007
Posts: 6
#8: 3 Weeks Ago

re: display user friendly message for date validation


Dear ajalwaysus ,
Thanks and God bless you
I am much obliged to you
Reply


Similar Microsoft Access / VBA bytes