Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking to see if a record already exists

Status
Not open for further replies.

BradB

MIS
Jun 21, 2001
237
0
0
US
I'm adding records to the database, and I don't want duplicate data. So, I made the Date and the Staff Name the primary key. I wanted one date per person. When a duplicate is found alert the user of the date and the Staff Name.

Are there any good examples of how to check to see if a record already exists? If the record already exists, can I get a message box to say,"10/01/01 Brad Boydston already exists!"?

BradB
 
True, but does it tell me the exact record that's at fault?

When a duplicate is found alert the user of the date and the Staff Name.
Example:

msgbox "The following record(s) are already in the database. Please remove the following records." rs.date & " " & rs.staffName
 
See if this works for you, I use it a lot:
Code:
Dim strMsg As String, strX as String
strMsg = "Patient number " & Forms!frmMain!txtPatID & " exists and cannot be added"

If Not IsNull(strX = DLookup("[PatID]", "tblDemog", "[PatID] ='" _
    & Forms!frmMain!txtPatID & "'")) Then
  MsgBox (strMsg)
  Forms!frmMain!txtPatID.SetFocus
  Forms!frmMain.Refresh
  GoTo Exit_CmdFindRec_Click
End If
It checks to see if a record exists with that ID and if it does displays a custom message box you can define. It sets the focus back to that field and exits the code....

HTH
 
Oh, that would be very different then ......Nevermind :)

Actually, go into your help file and search for FindFirst method of the recordset object. There's an example in there you could tweak to suit your needs. Tyrone Lumley
augerinn@gte.net
 
Hi, BradB!

Use BeforeUpdate procedure for checking of existing data:

private sub MyNoDuplicateField_BeforeUpdate(Cancel as Integer)
dim rst as recordset

if isnull(MyNoDuplicateField) then
msgbox "Enter ...."
cancel=true
exit sub
end if

set rst=currentdb.openrecordset("Select Field1 From MyTable Where Field1=" & MyNoDuplicateField & ";")
'MyTable >> table name
'Field1 >> field name for verification (not duplicate)
'MyNoDuplicateField >> form control name which you want to add into table (data source to Field1
'Use ' for text type fields (...Where Field1='" & MyNoDuplicateField & "';") or # for date type fields (...Where Field1=#" & MyNoDuplicateField & "#;")

if not rst.eof then
'Recordset contains new data
mcgbox "Record already exist..."
cancel=true
endif
rst.close
set rst=nothing
end sub

Aivars
 
If you want one record per StaffName, why did you make the primary key a segmentation of Date+StaffName?

If you make the primary just StaffName, then this would automatically stop the duplicates and when a StaffName record was retrieved the date could simply be changed.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top