View Full Version : Comparing Fields


Mechele
01-30-2002, 06:52 AM
Can I compare a field in a table with a field in a form? Or, Can I comparea field in a query with a field in a table?

David R
01-30-2002, 07:44 AM
Absolutely, as long as the fields are of the same/compatible types. The Expression Builder is fairly good at building these sort of things in the proper syntax.

David R
01-30-2002, 08:09 AM
Further information on comparing fields for the purposes of eliminating duplicates:

If you want the table to check the primary key before you exit the record at the end, you have a couple of options. One is simply to add a Save command into the OnExit event for the field in question. However this will whack out if you have other required fields.

Better solution: use Dcount:
Private Sub ParticipantID_BeforeUpdate(Cancel As Integer)
If (Me.NewRecord = False) Then
If (Me.ParticipantID = "" Or IsNull(Me.ParticipantID) = True) Then Me.Undo
End If

If DCount("[ParticipantID]", "tableParticipants", "[ParticipantID]= '" & Me![ParticipantID] & "'") = 1 Then
MsgBox "This is a duplicate Participant ID."
Me.Undo
Cancel = True
End If

End Sub


HTH,
David R