I am getting "You cannot add or change a record because a related record is required in table "tbl_Entrianment". The only thing that relates the two tables in question is the Entrainment_ID WHICH I CAN SEE ON MY FORM.
Let me back up.
I have a form/table Entrainment. An Entrainment can have lots of Specimens (one to many with Entrainment_ID; autonumber/primary key in tbl_Entrainment and number/foreign key in tbl_Specimen_Entrainment). On my Entrainment form is a button to view/add Specimens. It launches a filtered Specimen_Entrainment form (where Entrainment_ID of the Entrainment form = the Entrainment_ID of the Specimen form). I can add new records to the Specimen form no problem.
However, I have a "not in list" function on the Species control of the Specimen_Entrainment form. After I add a new Species and return to the Specimen_Entrainment form I get the error message. Here is the code for the not in list:
Expand|Select|Wrap|Line Numbers
- Private Sub Species_ID_NotInList(NewData As String, Response As Integer)
- Dim Result
- Dim Msg As String
- Dim CR As String
- CR = vbCr
- ' Exit this subroutine if the combo box was cleared.
- If NewData = "" Then Exit Sub
- ' Ask the user if he or she wishes to add the new customer.
- Msg = "'" & NewData & "' is not in the list." & CR & CR
- Msg = Msg & "Do you want to add it?"
- If MsgBox(Msg, vbQuestion + vbYesNo) = vbYes Then
- ' If the user chose Yes, start the Species form in data entry
- ' mode as a dialog form, passing the new species code name in
- ' NewData to the OpenForm method's OpenArgs argument. The
- ' OpenArgs argument is used in Species form's Form_Load event
- ' procedure.
- DoCmd.OpenForm "frm_Species_Freshwater", , , , acFormAdd, acDialog, NewData
- End If
- ' Look for the species code name the user created in the Specimen form.
- Result = DLookup("[Species_Code]", "tbl_Species_Freshwater", _
- "[Species_Code]='" & NewData & "'")
- If IsNull(Result) Then
- ' If the species code was not created, set the Response argument
- ' to suppress an error message and undo changes.
- Response = acDataErrContinue
- ' Display a customized message.
- MsgBox "Please try again!"
- Else
- ' If the species code was created, set the Response argument to
- ' indicate that new data is being added.
- Response = acDataErrAdded
- End If
- End Sub