The system *should* advise the user if a record already exists, but like
Jeff says, it shouldn't wait till they hit the submit button after inputting
a truckload of data.
There are always a small number of data elements that collectively
constitute the criteria for which "pre-existence" can be determined. Once
these elements have been derived/input, the system should immediately check
to see if the record exists. In this way, the user isn't forced to waste
time inputting the remaining data elements if they can't eventually save the
record. I agree with Jeff; this would be *very* frustrating, and not a very
friendly thing for a piece of software to do.
There is the argument against system intervention, however in this case, I
think it lends itself more to data integrity, recoverability (which states
that user input should be validated as soon as possible) and task adequacy
(which implies that the system should minimise the ways in which a user can
make a mistake).
Identify those data elements that collectively constitute a record. Then in
the AfterUpdate event for each, call a private procedure to check if a
record already exists. You don't need to do that for every control on the
form; just those select few that constitute a record that can be looked for.
The code for something like that would be as follows:
Private Function RecordExists() As Boolean
Dim dbAs Database
Dim rs As DAO.Recordset
Dim sSQL As String
'Check that all the relevent controls meet the criteria
If Nz(Me.intControl1, 0) > 0 AND Len(Nz(Me.txtControl2, "")) > 0
Then
sSQL = "SELECT abc FROM MyTable WHERE NumField1 = " & _
Me.intControl1 & " AND TxtField2 = """ &
Me.txtControl2 & """"
'Check the database
Set rs = db.OpenRecordset(sSQL, dbOpenSnapshot)
If rs.AbsolutePosition > -1 Then
MsgBox "Record exists."
RecordExists = True
End If
End If
rs.Close
Set rs = Nothing
Set db = Nothing
End Function
If you want, you can kill off the MsgBox from this function, and maybe give
the user the choice of subsequent actions if it returns True. This prevents
the user from making an error, and then passes control back to them.
Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia