I am creating a data base in access 2003 and I was hoping you could help me out with a question I have.
In my table [Log] I have fields for first name, middle initial, and last name. My primary key is an autonumber, but I have several names that appear exactly the same. Some of the entries are duplicates but not all them. Can I create a validation rule that checks for unique values in the combination of the three fields? Also I wanted my validation text to read "Duplicate entry. Continue?" so that if it is two different people we can still enter the data. I know it is possible to override validation rules in a macro but I'm not sure how to do it. I also tried using the Dlookup function but I had already entered over 9000 applicants into my table before realizing my mistake, which I am currently correcting.
Thanks for your time, any suggestions will be greatly appreciated!
Wendy, I'm not exactly sure what is going on, so I created a Demo (Attachment) for you to illustrate my point. Simply Add another Record to the Employees Form with the same First Name, Last Name, and MI, then see what happens.
23 4761
Are you entering the data directly into the table or via a form?
- In the BeforeUpdate() Event of the Form, check for duplication based on the First Name, Last Name, and MI.
- If Duplication exists on the combination of those 3 Fields, see if the User wants to Save the Record anyway.
- If the User does not wish to enter the Duplicate, Cancel the Updating (Save Record) of the Form.
- Below, find Demo Code which illustrates these points. Make your own changes based on Field Names, Table Name, etc.
- Private Sub Form_BeforeUpdate(Cancel As Integer)
-
Dim strCriteria As String
-
Dim intResponse As Integer
-
-
strCriteria = "[LastName] = '" & Me![txtLastName] & "' AND [FirstName] = '" & Me![FirstName] & _
-
"' AND [MI] = '" & Me![MI] & "'"
-
-
'See if a Record exists with exactly the Same First Name, Last Name, and MI
-
If DCount("*", "Employees", strCriteria) > 0 Then
-
intResponse = MsgBox("Duplicate Entry, Continue?", vbQuestion + vbYesNo, "Duplicate Entry")
-
If intResponse = vbNo Then Cancel = True
-
End If
-
End Sub
P.S. - You could also create a Table-Level Validation Rule, but that would ' never' allow a Duplicate Record based on the 3 Fields.
ADezii's method will work for you (as is the usual case), but you'll need to expand on it a bit.
This method will get you to the point in which you can tell whether the same name exists or not. But since you have occurrences of duplicate names that are valid, you'll need some functionality that allows you to decide whether your current entry is actually a duplicate of the one(s) that currently reside in your table.
Thank you for such a quick reply. I'm new to access and would appreciate step by step instructions of where to input code if its not too much to ask! I tried entering the code into the beforeupdate event under the expression and code builder but its not working. Should I build this in a macro or Am I subsituting incorrect changes in the code?
Table = Log
Form = Log
Fields: First Name, MI, Last - Private Sub Form_BeforeUpdate(Cancel As Integer)
-
Dim strCriteria As String
-
Dim intResponse As Integer
-
-
strCriteria = "[Last] = '" & Me![txtLast] & "' AND [First Name] = '" & Me![First Name] & _
-
"' AND [MI] = '" & Me![MI] & "'"
-
-
'See if a Record exists with exactly the Same First Name, Last Name, and MI
-
If DCount("*", "Log", strCriteria) > 0 Then
-
intResponse = MsgBox("Duplicate Entry, Continue?", vbQuestion + vbYesNo, "Duplicate Entry")
-
If intResponse = vbNo Then Cancel = True
-
End If
-
End Sub
Thanks again,
WO
What is the nature of the Error that you are receiving?
Just my guess, are you putting the codes ADezii provided directly into some places that's within another event (or Sub procedure)? As from the codes you posted:
Table = Log
Form = Log
Fields: First Name, MI, Last
These are to be invalid if you didn't specify clearly what they are.
When I put the code (exactly as I have it in my last post) into an expression builder and test it by typing a duplicate name nothing happens at all. when I put it into a code builder and type even just the first name the error message says
Microsoft Can't find the Marco. The marcro (or it's macro group) doesn't exist, or the macro is new but hasn't been saved. Note that when you enter the marcogroupname.macroname syntax in an argument, you must specify the name the macro's macro group was last saved under.
When I put this code into the conditions in a macro it is truncated bc it is too long.
The
table = Log ect. only refers to the names on my table form and fields.
very lost, thanks for the help!
Wendy, I'm not exactly sure what is going on, so I created a Demo (Attachment) for you to illustrate my point. Simply Add another Record to the Employees Form with the same First Name, Last Name, and MI, then see what happens.
NeoPa 32,556
Expert Mod 16PB
Wendy, what you describe sounds like the event procedure you have created, and which is correctly in your module, is not linked in to the event, but some macro is instead. To test this take the following steps : - Open the form in Design Mode.
- Ensure the Properties Pane is showing (Alt-Enter).
- Ensure the Tab is either Event or All.
- Scroll down to the Before Update property.
- Check that it says [Event Procedure]. Exactly that. As a string. It will always be available from the drop-down to the side too if you prefer that.
- Other options may be available in the drop-down if you have macros available. Don't select one of these otherwise all the code you've just set up will be bypassed completely.
Let us know what you found and how well it's working after you changed it to say [Event Procedure] ;-)
PS ADezii's code is spot on, and should work perfectly for you.
Thanks for that it was a big help. I'm so close now but there is one flaw:
When I enter a duplicate name and press tab or enter, there is no error message, but when I delete the first name and re type it then the message appears. Not sure what the reason could be for this problem.
NeoPa 32,556
Expert Mod 16PB
I think you need to describe the situation more clearly Wendy. Precise logical thinking is not easy with ambiguous descriptions.
Also, just discovered this if I type any first name the duplicate error apears even if I type nnghagak nonsense
Sorry about that let me try to explain this a little better. I test the code in my form (Log) by typing a name that already exists, when I move to another record the beforeupdate event does not occur. However, when I delete the first name and retype it in the same record, the beforeupdate event occurs. Hope I'm not testing your patience I really appreciate the help!
Wendy, it sounds like you have the proper code, but the wrong BeforeUpdate() Event. The code must be placed in the BeforeUpdate() Event of the Form and NOT in the BeforeUpdate() Event of either the First Name, Last name, or MI Fields. Worse case scenario, can you Upload your Database? The only thing that we would need would be the Form (with code contained within), and the underlying Data Source. P.S. - Have you even looked at the Demo that I sent you?
OK I will try that out, and yes the demo is what got me this far. Again thanks for the help, I've never worked with code before so it is all very unfamiliar to me. I will work it out from here.
ADezii that was exactly my problem you are genius! Thank you Thank you a million times!
You are quite welcome, Wendy, and way to hang in there! You go girl! (LOL).
NeoPa 32,556
Expert Mod 16PB
Nice catch ADezii. That didn't occur to me unfortunately.
PS. Wendy, may I suggest you set ADezii's post #3 as Best Answer ;-)
Hello again my patient friends! I have discovered a slight problem: Entering names with an apostrophe, such as O'Sullivan, seems to disrupt the code. Any way to correct this?
Hello Wendy. I realize that you are not that proficient in programming, at least not yet (LOL). The problem that you are describing relates to the manner in which Access handle ' Quotes' in Strings, under certain circumstances. I've corrected the problem for you, assuming that a Quote may only exist in a Last Name. Look at the revised Code carefully, and try to determine what is happening when it encounters a Quote in the Last Name Field, but better yet, download the Revised Attachment. The last Employee on the Form is Robert A O'Brian. Try to duplicate this Employee exactly, and see what happens. Any questions, we are here. - Private Sub Form_BeforeUpdate(Cancel As Integer)
-
Dim strCriteria As String
-
Dim intResponse As Integer
-
-
If InStr(1, Me![txtLastName], "'") > 0 Then 'Quote in Last Name?
-
strCriteria = "[LastName] = '" & Replace(Me![txtLastName], "'", "'" & "'") & "' AND [FirstName] = '" & _
-
Me![FirstName] & "' AND [MI] = '" & Me![MI] & "'"
-
Else
-
strCriteria = "[LastName] = '" & Me![txtLastName] & "' AND [FirstName] = '" & Me![FirstName] & _
-
"' AND [MI] = '" & Me![MI] & "'"
-
End If
-
-
'See if a Record exists with exactly the Same First Name, Last Name, and MI
-
If DCount("*", "Employees", strCriteria) > 0 Then
-
intResponse = MsgBox("Duplicate Entry, Continue?", vbQuestion + vbYesNo, "Duplicate Entry")
-
If intResponse = vbNo Then Cancel = True
-
End If
-
End Sub
Not proficient doesn't really cut it lol. The revised code worked perfectly. As always very grateful.
Wo
PS where can I learn more on my own?
PS where can I learn more on my own?
Introductory Classes would be a good start. In my case, it was books, books, and more books...
NeoPa 32,556
Expert Mod 16PB SQL Injection Attack may be a little overboard for this question, but may be worth a look nevertheless.
ADezii's solution to replace all quotes with two quotes is the sensible one. Using Replace({Value}, "'", "''") should always work and causes no problems even if there is no quotes in the {Value} string.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Dalan |
last post by:
For some apparent reason I have been unable to structure a piece of
code on the BeforeUpdate event on a form that has 3 fields which I
need to set validation rules. I would appreciate anyone's...
|
by: ssmith86 |
last post by:
I have created a DAP linking to only one table. Several of the fields
on the table have validation rules. The only time my message boxe
comes up is when I am finished inputting data on the DAP...
|
by: Tom |
last post by:
How do I set up the following Validation Rules in a table:
1. Two chars - both must be digits(0-9)
2. Three characters - first character must be a letter
3. 6 characters - all must be...
|
by: Jeremy |
last post by:
I have a vb .net 2003 app that uses access 2k as a backend. The main table
has some validation rules (legacy stuff brought forward inadvertantly, and
now we're stuck with 'em).
The problem is,...
|
by: tsnyder |
last post by:
I need to set a validation rule for a field so that it looks at all
other fields to make sure that field is true.
|
by: sesling |
last post by:
I have a form that has several text boxes where the operator can enter data. I want to place some validation rules around what the operator can enter.
In one box I want the number to be at least...
|
by: JHNielson |
last post by:
I have a form with a drop-down box and 4 other text boxes Owner, Name, Date, State.
I would like to limit it so that the user can only enter data in those boxes if the drop down equals "Purchase"?...
|
by: babamc4 |
last post by:
Hi Experts,
I have just completed designing a new DB and my boss has asked me to put some complicated (I think!) validation rules on to it. My table is below:
1. Index - Autonumber
2....
|
by: jinalpatel |
last post by:
I have two tables Mainfirm and Subfirm. I have everything ready. Now I am entering validation rules like as below in my form fields to protect the tables from bad data. But when I try to enter...
|
by: DrSporkeh |
last post by:
Hai everyone, i need some help :o
basically im trying to set some validation rules for an 'employees' table.
firstly i've set it to you have to be 16 to work here under 'Date Of Birth'
...
|
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...
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
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: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
| |