473,386 Members | 1,610 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,386 software developers and data experts.

Table validation rule and form conflicting

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 1770
twinnyfo
3,653 Expert Mod 2GB
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
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 Expert Mod 2GB
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
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 Expert Mod 2GB
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
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 Expert Mod 2GB
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
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 Expert Mod 2GB
You've got it exactly right. Hope this hepps!
Nov 17 '14 #10
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 Expert Mod 2GB
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
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 Expert Mod 2GB
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
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
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
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
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
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
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
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
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
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
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
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.