I have a one to many relationship between tblMain!ID and tblAgeID!RecordID,
and a one to many relationship between tblAgeValue!AgeID and
tblAgeID!ValueID. That is, a many to many relationship between tblMain and
tblAgeValue, since each record in tblMain may contain more than one age value
and each age value in tblAgeValue may appear in more than one record in
tblMain.
tblMain!ID and tblAgeValue!AgeID are Autonumber primary keys. In the
junction table, tblAgeID!RecordID and tblAgeID!ValueID are long integer
primary keys.
The following code makes it down to the end but then claims that changes
would create duplicate values in index, primary key or relationship.
Private Sub cboAgeValue_NotInList(NewData As String, Response As Integer)
Dim rstMain As New ADODB.Recordset
Dim rstAgeID As New ADODB.Recordset
Dim rstAgeValue As New ADODB.Recordset
Dim intAnswer As Integer
On Error GoTo ErrorHandler
intAnswer = MsgBox("Add '" & NewData & "' to the list of age values?",
vbQuestion + vbYesNo)
If intAnswer = vbYes Then
rstAgeValue.Open "tblAgeValue", CurrentProject.Connection,
adOpenKeyset, adLockOptimistic
rstAgeID.Open "tblAgeID", CurrentProject.Connection,
adOpenKeyset, adLockOptimistic
rstMain.Open "tblMain", CurrentProject.Connection, adOpenKeyset,
adLockOptimistic
' Add age stored in NewData argument to new record in the age table.
rstAgeValue.AddNew
rstAgeValue!Value = NewData
rstAgeValue.Save
' Assign ID values to new record in the junction table.
rstAgeID.AddNew
rstAgeID!ValueID = rstAgeValue!AgeID
rstAgeID!RecordID = rstMain!ID
' Close the recordsets
rstAgeValue.Close
Response = acDataErrAdded
Else
Response = acDataErrDisplay ' Require the user to select an existing
age value
End If
Exit Sub
ErrorHandler:
MsgBox "Error # " & Err.Number & vbCrLf & vbCrLf & Err.Description
End Sub
Detailed enough?
Thanks in advance for any help,
Jim