473,320 Members | 2,000 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

display user friendly message for date validation

6
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
Oct 21 '09 #1

✓ answered 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

7 3496
ajalwaysus
266 Expert 100+
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
Oct 21 '09 #2
NeoPa
32,556 Expert Mod 16PB
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!
Oct 21 '09 #3
arnold
6
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
Oct 22 '09 #4
ajalwaysus
266 Expert 100+
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
Oct 22 '09 #5
ajalwaysus
266 Expert 100+
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
Oct 22 '09 #6
NeoPa
32,556 Expert Mod 16PB
Well, I've learned something tonight. Good one AJ.
Oct 22 '09 #7
arnold
6
Dear ajalwaysus ,
Thanks and God bless you
I am much obliged to you
Oct 25 '09 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Darren | last post by:
I had to restore my server from backup. When I did, I could no longer access any asp pages on my web server. The error says HTTP 500 Internal Server Error. I have turned off friendly HTTP error...
19
by: dmiller23462 | last post by:
Hi guys....I have absolutely NO IDEA what I'm doing with Javascript but my end result is I need two text boxes to stay hidden until a particular option is selected....I've cobbled together the...
4
by: James | last post by:
Hello, I have a RequiredFieldValidator for several textbox controls on a form. Here's an example with the RequiredFieldValidator. EnableClientScript, Enabled, and Visible are set to true for...
1
by: panche | last post by:
I'm developing a fairly simple user control that has two textboxes for date/time entry (a from date/time and a to date/time). One of my requirements is that there should be no button that sets...
24
by: Rob R. Ainscough | last post by:
VS 2005 I have: ClickOnce deployment User's that hate and or don't want to use an IE Client (don't blame them) I don't see how ASPX web pages are going to survive? With .NET 2.0 and clickonce...
2
by: c676228 | last post by:
Hi, I have several user controls like email, phone,ssn etc. each has it's own validation message in its user control already. When I bring all those user controls in an aspx page, I would like to...
3
by: Pacer Dawn | last post by:
I have a web control that takes a date input. There are two types of validation I want to do on this: 1) validate that the date input is in the proper format, and display a message if otherwise...
2
by: rn5a | last post by:
The different Validation controls like RequiredFieldValidator, RangeValidator etc. have a property named Display. This property can have 3 values - Dynamic, Static & None. What's the difference...
3
by: phil67b | last post by:
Hello everybody, I have a page rech.php where I'm doing a multi-criteria research Ex. choose your car model, choose your country. After validation of my form, on the same page, the lines will be...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.