You could try the Form Error event. Delete the code for the patient id after update event and add the following.
Code:
Private Sub Form_Error(DataErr As Integer, Response As Integer)
Const conDuplicateKey = 3022
Dim strMsg As String
If DataErr = conDuplicateKey Then
' Response = acDataErrDisplay
Response = acDataErrContinue
strMsg = "Each patient record must have a unique " _
& "ID number. Please recheck your data."
MsgBox strMsg
Me.Undo
Me.txtPatientID.SetFocus
End If
End Sub
There is a small problem with this code. If someone entered a duplicate patient number, you don't get the error until you try and save the record. The "Undo" command clears the record, so you have to re-enter ALL of the info again.
I think the best thing to do is change the PK to an autonumber and use code (and/or index the field) in the "Patient ID" after update event to check for duplicate patient IDs.
A few Problems that I see:
There are spaces in object names (fields, tables, forms). Only the programmer should see object names, so "Proper" English is unimportant. Use the underscore or "CamelBack" to distinguish words. Short, descriptive names for objects are more important. Labels are where you can be verbose.
Example: a field named "ApptDate". The label on the form for that control (attached or unattached) could be "Appointment Date for prenatal exam".
Long names and spaces are a real pain.
Misnamed primary keys. The primary key field for the table "Appointments" is "Date ID" and is linked to other tables by the foreign key "Date ID".
I always name the primary key/foreign key pair with the name of the table.
Table "tblCustomers" would have a PK of "CustID_PK" and the FK would be "CustID_FK".
Table "tblAppointments" would have a PK of "ApptID_PK" and the FK would be "ApptID_FK".
Look up FIELDS.... I never use look up fields (look up tables are a different matter). See http://access.mvps.org/access/lookupfields.htm
Recommendations:
read the following about Primary keys:
Primary Key Tips and Techniques
http://www.fmsinc.com/free/newtips/primarykey.asp
Selecting the Right Primary Key in Microsoft Access
http://www.bluemoosetech.com/microso...ial.php?jid=54
And related to the above topics:
Autonumbers--What they are NOT and What They Are
http://www.utteraccess.com/wiki/index.php/Autonumbers
I always use an autonumber for the table PK. Then if I have to change the patient ID or use letters & numbers in the patient ID, the links to other tables are maintained. No one should see the PK/FK.
I would not use the built in search function because it also exposes the Replace function.
I spent some time in your dB. One of my conventions is to have the PK as the first field in the table, then all of the FK fields. After that are the rest of the fields, grouped by type. All of the text fields, number fields, date fields and boolean fields are grouped together. Since the order of the fields in a table is not important, grouping the fields makes it easier for me to find a specific field.
-------------
I apologize.... you posted about a specific question and I blabbed on and on.
But I think you will have more problems because of the current design. In 6 months you will look at the dB and say "What was I thinking???"
Been there, done that, got 3 tee shirts to prove it.
You wouldn't believe how bad my first dB was.