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!
13 1770
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.
Thank you for the quick response, how would I go about checking it it is "dirty". I'm pretty new to Access and VBA.
The general check would be within an If...Then statement: - If Me.Dirty Then
-
(perform your required codes)
-
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.
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?
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).
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
When the Add New button is pressed, evaluate those six text boxes with those same rules in the buttons OnClick event. For example: - Private Sub cmdAddNew_Click()
-
Dim strPrompt As String
-
strPrompt = ""
-
If IsNull(Me.txtForename) Then
-
strPrompt = "You must include the Forename"
-
End If
-
If IsNull(Me.txtSurname) Then
-
If strPrompt = "" Then
-
strPrompt = "You must include the Surname"
-
Else
-
strPrompt = strPrompt & "; " & _
-
"You must include the Surname"
-
End If
-
End If
-
.... etc.
-
If strPrompt = "" Then
-
'Go do your other stuff
-
Else
-
MsgBox strPrompt, vbOkOnly, "Please fix errors"
-
End
-
End Sub
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.
You've got it exactly right. Hope this hepps!
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!): -
'The record must contain at least a DoB or a NHS
-
'Case 1 - when there is no DoB or NHS
-
If IsNull(Me.txtDoB) And IsNull(Me.txtNHS) Then
-
If strPrompt = "" Then
-
strPrompt = "A record must contain either DoB or NHS No."
-
Else
-
strPrompt = strPrompt & "; " & _
-
"A record must contain either DoB or NHS No."
-
End If
-
End If
-
-
'Case 2 - is a NHS but no DoB
-
If IsNull(Me.txtDoB) = True And IsNull(Me.txtNHS) = False Then
-
If Len(Me.txtNHS) <> 10 Then
-
If strPrompt = "" Then
-
strPrompt = "The NHS Number is invalid"
-
Else
-
strPrompt = strPrompt & "; " & _
-
"The NHS Number is invalid"
-
End If
-
End If
-
End If
-
-
'Case 3 - is a DoB but no NHS
-
If IsNull(Me.txtDoB) = False And IsNull(Me.txtNHS) = True Then
-
If Me.txtDoB >= Date Then
-
If strPrompt = "" Then
-
strPrompt = "You must include a valid Date of Birth"
-
Else
-
strPrompt = strPrompt & "; " & _
-
"You must include a valid Date of Birth"
-
End If
-
End If
-
End If
-
-
'Case 4 - when there is both a DoB and a NHS - check for valid
-
If IsNull(Me.txtDoB) = False And IsNull(Me.txtNHS) = False Then
-
If Me.txtDoB >= Date Then
-
If strPrompt = "" Then
-
strPrompt = "You must include a valid Date of Birth"
-
Else
-
strPrompt = strPrompt & "; " & _
-
"You must include a valid Date of Birth"
-
End If
-
End If
-
-
If Len(Me.txtNHS) <> 10 Then
-
If strPrompt = "" Then
-
strPrompt = "The NHS Number is invalid"
-
Else
-
strPrompt = strPrompt & "; " & _
-
"The NHS Number is invalid"
-
End If
-
End If
-
End If
-
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: - If IsNull(Me.txtDoB) Or IsNull(Me.txtNHS) Then
-
(Evaluate which ones or both and update the string)
-
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.
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!
Glad we could be of service! Keep coding!
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 = ""...
|
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,...
|
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$) {
}
...
|
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
|
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...
|
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...
|
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...
|
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,...
|
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,...
|
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...
| |