I have a simple database structured like this (non relevant tables and
fields omitted)
Members table
memberID
memberFname
memberLname
memberNokID
memberDob
Next of Kin table
nokID
nokFname
nokLname
nokAddress
nokTown
nokCounty
memberID and nokID are primary keys and autonumber for the tables.
memberNokID is a foriegn key in the members table for the nokID in the
next of kin table.
I then have a single form where users enter data for the member and
the next of kin (also there are school, class, doctor etc but not
relevant to this issue)
What I am trying to achieve is when a user types in the address line
it checks the nextofkin table to determine if the address is already
in the database and if so populate the values from that address into
the other fields. I can do this with some code for the beforeupdate
event:
Extract >>
strAddress = [nokAddress]
strLookup = DLookup("nokAddress", "nextofkin", "nokAddress='" &
strAddress_ & "'")
I can test this for a match and populate the other fields on the form
ok with
If (Not IsNull(strTown)) Then Me![nokTown] = strTown
Of course what i really need to do is to insert the nokID of the
existing record into the memberNokID value for the members table, that
way the same address only appears once in the table. (no duplication)
When I try the code
If (Not IsNull(intNOK)) Then Me![memberNokID] = intNOKID
I get error 3341 and a message stating the current field must match
the join key nokID.
I am stumped and have got into a mess trying to fix this one! Could it
be something to to with positioning the current (focussed?) field or
am I going about this the completely wrong way. I DONT want to use a
combo box with a noinlist event, but a way to automatically test if
the Next of Kin is already listed and use the listing as a reference
in the members table to avoid duplication in the next of kin table.
It is possible that > 1 member have the same next of kin and also that
there are > 1 last names for next of kin which are the same. The only
unique thing is the address line.
Full code I'm using is:
Private Sub nokAddress_BeforeUpdate(Cancel As Integer)
Dim strLookup, strAddress, strTown, strCounty, strPostcode As String
Dim intNOKID As Integer
Dim updRecord As Byte
strAddress = [nokAddress]
strLookup = DLookup("nokAddress", "nextofkin", "nokAddress='" &
strAddress & "'")
If strAddress = strLookup Then
updRecord = MsgBox("A member next of Kin at this address is
already listed. Use the existing data?", vbOKCancel, "Record
Modification")
If updRecord = vbCancel Then
Cancel = True
End If
intNOKID = DLookup("nokId", "nextofkin", "nokAddress='" &
strAddress & "'")
strTown = DLookup("nokTown", "nextofkin", "nokAddress='" &
strAddress & "'")
strCounty = DLookup("nokCounty", "nextofkin", "nokAddress='" &
strAddress & "'")
strPostcode = DLookup("nokPostcode", "nextofkin",
"nokAddress='" & strAddress & "'")
If (Not IsNull(intNOK)) Then Me![memberNokID] = intNOKID
If (Not IsNull(strTown)) Then Me![nokTown] = strTown
If (Not IsNull(strCounty)) Then Me![nokCounty] = strCounty
If (Not IsNull(strPostcode)) Then Me![nokPostcode] =
strPostcode
End If
End Sub
Thanks in advance for any suggestions.
Andy Proctor