Newbie question for Access 2003

drosearup

New Member
Joined
May 9, 2011
Messages
3
My experience has been mainly SQL Server. I am working on an Access 2003 database with a simple data entry form (frmNewPatient) for inserting new records into a Patient table. Before executing the insert, I want to check that there is no matching record already in the Patient table. The form has text boxes named txtLastName, txtFirstName, and txtSSN for entering unique data for a patient. Using VBA, I want to execute a query such as:

select * from Patient
where Patient.LAST_NAME = [Forms]![frmNewPatient]![txtLastName]
and Patient.FIRST_NAME = [Forms]![frmNewPatient]![txtFirstName]
and Patient.SSN = [Forms]![frmNewPatient]![txtSSN]

Also, how to check that the result set is empty.

Thanks,

Doug
 

Excel Facts

Which Excel functions can ignore hidden rows?
The SUBTOTAL and AGGREGATE functions ignore hidden rows. AGGREGATE can also exclude error cells and more.
Instead of the Insert, you would use the Before Update Event and then you can use DCount instead:

Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
   Dim strWhere As String
 
strWhere = "[Last_Name] = " & Chr(34) & Forms!frmNewPatient.txtLastName & Chr(34) 
strWhere = strWhere & " [First_Name] = " & Chr(34) & Forms!frmNewPatient.txtFirstName & Chr(34)
strWhere = strWhere & " [SSN] = " & Chr(34) & Forms!frmNewPatient.txtSSN & Chr(34)
 
If DCount("*", "PatientTableNameHere", strWhere) > 0 Then
   MsgBox "Patient already exists.", vbExclamation, "Duplicate Entry"
   Cancel = True
End If
 
End Sub
 
Upvote 0
Just a thought. If you have a primary key on the SSN (no duplicates) column then Access will automatically find duplicate entries.
 
Upvote 0
I tried your suggested VBA code. I had to insert 'AND' between the second and third equality statements. It works great now.

Thanks
 
Upvote 0

Forum statistics

Threads
1,224,566
Messages
6,179,550
Members
452,927
Latest member
rows and columns

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top