473,508 Members | 2,367 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Table validation rule and form conflicting

64 New Member
Hi there.

I have made a database which has a table with validation rules attached to each of the fields in order to prevent "bad data". For example the forename must be included in the table so I have used a Is Not Null validation rule.

I also have a form used for data entry into the table which works fine. When data is entered into the form and the add new records button is clicked, my custom message also appears as intended. However, if the user then goes into a field as deletes what they have written and then tries to move away from the field, they get caught in a loop of sorts.

Example: "John" is entered into Forename field, User then clicks add new record, message box appears saying error not all fields completed, user then goes back and deletes "John" from the Forename field and tries to click off, message box appears stating the validation text of the table and user cannot move away until something is entered into the field.

I know this is a minor detail which can be avoided by not deleting the text from the field and just moving off of the form but it is annoying knowing it now exists.

Any help will be most appreciated!
Nov 6 '14 #1
13 1773
twinnyfo
3,653 Recognized Expert Moderator Specialist
You must check to see if the form is "dirty". If it is, then cancel any updates. There are other ways to do this, but this is perhaps the easiest and most straightforward.
Nov 6 '14 #2
DavidAustin
64 New Member
Thank you for the quick response, how would I go about checking it it is "dirty". I'm pretty new to Access and VBA.
Nov 7 '14 #3
twinnyfo
3,653 Recognized Expert Moderator Specialist
The general check would be within an If...Then statement:

Expand|Select|Wrap|Line Numbers
  1. If Me.Dirty Then
  2.    (perform your required codes)
  3. End If
It just depends on when you want this code to fire and what you want to do when it does fire. Again, lots of different options.
Nov 7 '14 #4
DavidAustin
64 New Member
I would want it to fire when the user tries to navigate away from the textbox after it being filled and then deleted.

Is there some sort of code to stop the validation text message box from appearing and allowing the user to navigate to a new control/away from the form?
Nov 10 '14 #5
twinnyfo
3,653 Recognized Expert Moderator Specialist
Place the code in the AfterUpdate Event of the concerned Text boxes. Check for Null. You may have to deactivate the Validation Rules in the Table if you want to do it programmatically (just so you don't accidentally get duplicate error messages).
Nov 12 '14 #6
DavidAustin
64 New Member
After trying it out the past couple of days, it seems the only way forward is to remove the rules from the table.

How would I go about putting them into the data entry form so that the rules are tested when an "add new" button is pressed?

I have 6 fields which need different validation rules:

Forename - needs to be included - previously "Is Not Null" in table
Surname - needs to be included - previously "Is Not Null" in table
Date of birth - needs to be included and a valid date in past
Reference number - not mandatory but if included must be 10 digits
Box number - needs to be included
File number - not mandatory
Nov 14 '14 #7
twinnyfo
3,653 Recognized Expert Moderator Specialist
When the Add New button is pressed, evaluate those six text boxes with those same rules in the buttons OnClick event. For example:

Expand|Select|Wrap|Line Numbers
  1. Private Sub cmdAddNew_Click()
  2.     Dim strPrompt As String
  3.     strPrompt = ""
  4.     If IsNull(Me.txtForename) Then
  5.         strPrompt = "You must include the Forename"
  6.     End If
  7.     If IsNull(Me.txtSurname) Then
  8.         If strPrompt = "" Then
  9.             strPrompt = "You must include the Surname"
  10.         Else
  11.             strPrompt = strPrompt & "; " & _
  12.                 "You must include the Surname"
  13.         End If
  14.     End If
  15.     .... etc.
  16.     If strPrompt = "" Then
  17.         'Go do your other stuff
  18.     Else
  19.         MsgBox strPrompt, vbOkOnly, "Please fix errors"
  20.     End
  21. End Sub
Nov 14 '14 #8
DavidAustin
64 New Member
Thank you so much for your help. Can I just walkthrough the code for peace of mind as I'm pretty new to this so I can make sure I know what is going on.

So the strPrompt is a variable which is being changed according to the lack of values in the textboxes. It tests the first textbox to see if the value is null and if it is changes strPrompt to a message. This is then repeated for each of the textboxes and their rules. Finally the variable is tested to see if there are any unfilled boxes (aka if strPrompt is not still "") and informs the user with an error message.
Nov 17 '14 #9
twinnyfo
3,653 Recognized Expert Moderator Specialist
You've got it exactly right. Hope this hepps!
Nov 17 '14 #10
DavidAustin
64 New Member
So I have managed to use your code to implement the rules in the data entry form (thanks again!) but I have a new semi-related issue...

I've also got a form where users can edit existing records if mistakes have been made. I've managed to implement that the Forename and Surname must be filled in but I have got stuck because for a record to be "complete" either the Date of Birth or reference number must be included.

So a record can have both DoB and ref number or just DoB or just ref number but shouldn't work if it does not contain neither of them. Any ideas how I could write this? I managed it in the add form using the following (even though it's probably not the most efficent way of doing it, it seemed to make sense!):

Expand|Select|Wrap|Line Numbers
  1. 'The record must contain at least a DoB or a NHS
  2. 'Case 1 - when there is no DoB or NHS
  3.     If IsNull(Me.txtDoB) And IsNull(Me.txtNHS) Then
  4.         If strPrompt = "" Then
  5.             strPrompt = "A record must contain either DoB or NHS No."
  6.         Else
  7.             strPrompt = strPrompt & "; " & _
  8.             "A record must contain either DoB or NHS No."
  9.         End If
  10.     End If
  11.  
  12. 'Case 2 - is a NHS but no DoB
  13.     If IsNull(Me.txtDoB) = True And IsNull(Me.txtNHS) = False Then
  14.         If Len(Me.txtNHS) <> 10 Then
  15.             If strPrompt = "" Then
  16.                 strPrompt = "The NHS Number is invalid"
  17.             Else
  18.                 strPrompt = strPrompt & "; " & _
  19.                 "The NHS Number is invalid"
  20.             End If
  21.         End If
  22.     End If
  23.  
  24. 'Case 3 - is a DoB but no NHS
  25.     If IsNull(Me.txtDoB) = False And IsNull(Me.txtNHS) = True Then
  26.         If Me.txtDoB >= Date Then
  27.             If strPrompt = "" Then
  28.                 strPrompt = "You must include a valid Date of Birth"
  29.             Else
  30.                 strPrompt = strPrompt & "; " & _
  31.                 "You must include a valid Date of Birth"
  32.             End If
  33.         End If
  34.     End If
  35.  
  36. 'Case 4 - when there is both a DoB and a NHS - check for valid
  37.     If IsNull(Me.txtDoB) = False And IsNull(Me.txtNHS) = False Then
  38.         If Me.txtDoB >= Date Then
  39.             If strPrompt = "" Then
  40.                 strPrompt = "You must include a valid Date of Birth"
  41.             Else
  42.                 strPrompt = strPrompt & "; " & _
  43.                 "You must include a valid Date of Birth"
  44.             End If
  45.         End If
  46.  
  47.         If Len(Me.txtNHS) <> 10 Then
  48.             If strPrompt = "" Then
  49.                 strPrompt = "The NHS Number is invalid"
  50.             Else
  51.                 strPrompt = strPrompt & "; " & _
  52.                 "The NHS Number is invalid"
  53.             End If
  54.         End If
  55.     End If
  56.  
Nov 21 '14 #11
twinnyfo
3,653 Recognized Expert Moderator Specialist
That should work. Keep in mind that data validation rules that are user created, although they may work very well, are almost always very ugly when you have many values and possibilities. There are, no doubt, slightly better ways to get your same results, but the key at this point is whether or not it works. As you sit back and reflect on how it works, then you can perhaps find ways to simplify the code.

For example, you could first just check to see if either of the two dates is null:

Expand|Select|Wrap|Line Numbers
  1. If IsNull(Me.txtDoB) Or IsNull(Me.txtNHS) Then
  2.     (Evaluate which ones or both and update the string)
  3. End If
Some of this may also be according to your preferences or if you want to have a particular layout for your code that will be easier to update in the future.
Nov 24 '14 #12
DavidAustin
64 New Member
It look a little while but I worked out why it wasn't working. I put the code into my edit form's before update event and it would fire when the two fields were null but would still allow the update to occur. Turns out that I had forgotten to include Cancel = True if the strPrompt didn't equal "".

The only final problem I have now is that if someone deletes information in the field then tries to navigate away, the message box will fire fine but the original information has been deleted. Is there a way of resetting the field to the original state of the record prior to editing just in case someone deletes the wrong field when it was correct in the first place?

EDIT: I found the Old Value property! Works perfectly now so thank you for all of your help Twinnyfo!
Nov 25 '14 #13
twinnyfo
3,653 Recognized Expert Moderator Specialist
Glad we could be of service! Keep coding!
Nov 25 '14 #14

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

Similar topics

2
4262
by: Dalan | last post by:
This ought to be simple enough, but not certain which to use. I have a few fields set to Require data to be entered; however, the message displayed by Access 97 is too generic to be of any real...
2
2674
by: Joey P | last post by:
Hi all, I am doing a project for university whereby i have to implement a simple database related to a frozen foods company. I am having some trouble though creating a validation rule for one...
0
2322
by: Steve V | last post by:
I'm using Access 2000 to build a budgeting/tracking database. Can I make a validation rule (using VBA) that checks the data as if the record has already been added? I've got 5 tables (only the...
6
3676
by: Chuck | last post by:
A97. A database has a table: tblA which has a single text field, B. It is a primary field, indexed and no duplicates. It is used as a lookup for table tblC. A form based on tblA is used to add...
3
2010
by: BrianB830 | last post by:
Hello all, I have a quick question regarding an MS Access database I'm creating. In the entity "ORDER", I have the attributes "Order Date" and "Delivery Date". I need to create a validation...
10
5684
by: gweasel | last post by:
What is the best way to apply a Validation Rule - or rather, where is the best place to put it? Is there an advantage to putting it on the field in the table vs setting the validation rule on the...
3
2224
by: Alex.Sh | last post by:
How can i do something like this: i have a form with A percentage : ______ B percentage: ______ C percentage: ______ I want that the sum of these 3 fields *will never* be more than 100...
1
2064
by: MLH | last post by:
Anyone remember if A97 append query failure would ever report data breaking validation rule when such was not the case. I have an old SQL statement - several years old now. I've encountered a case...
9
1566
by: hannoudw | last post by:
Hi i'm working on storing shoes so each article number had a size , and in my database i'm working that every article had different sizes, and since each article had max 5 sizes for example , i added...
8
2767
by: Kaloyan Krumov | last post by:
Here is the setup: table field validation rule Is Null Or Not Like "**" in Form i have textbox that automatically (inputmask) fills in the country phone code Dim mask As String mask = ""...
0
7223
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7377
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7036
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5624
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5047
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1547
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
414
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.