could not add a record because it is required or linked

Falcon88

Registered User.
Local time
Today, 16:36
Joined
Nov 4, 2014
Messages
318
hiiiiii all dears

i have main form and a subform
i use a code in the sub form in BeforeInsert event as :
Code:
Dim frm as Form         
Set frm = Me.Parent         
If IsNull(frm.[ID]) Then             
Cancel = True             
MsgBox "Create a record in the main form first.", vbExclamation, "Required data."             
frm.[ID].SetFocus         
End If
to prevent user to add non-related sub record .
this works good if the primary key (ID) is autonumber only.
but if the primary key (ID) is a number or text it not works good ?

please help .
 
This question is for clarity only: You say you get the "required record" message when the parent table's PK field is not an autonumber. The question is whether your "BeforeInsert" event is the parent form's event or the sub-form's event.

I cannot tell you with absolute certainty, but I have seen evidence that the presence of an autonumber causes the PK field to be filled at a different time than a non-autonumber PK field. I suspect that you are running afoul of that difference.

Also, though it is trickier to type and also is a matter of style, you could make the frm.[ID] reference be Me.Parent.[ID] - but note this only works on sub-forms and would fail (regardless of how it is coded) if you ever tried this on a form that IS a parent and is not a sub-form.

You could take the guesswork out of the problem in several ways, including not allowing the sub-form to be visible unless/until you key in your (non-autonumber) value to the ID field and, in that field's .LostFocus routine, go back to enable the option to show the sub-form. I'm vaguely remembering that for "normal" Access, you avoid relational integrity issues with child forms because Access does an implicit SAVE on a parent record linked to a child form that has just been given focus. However, your code would prevent that child form from gaining focus. At some point the timing becomes too complex.

Final note: If you REALLY want the child form to not allow entry until the parent is defined, just declare relational integrity between the related fields in the tables or queries that drive the parent and child forms. Then trap the error in the form if you violate RI. To be honest, while I'm sure it is well defined, I don't recall the exact order of events for this case, and I think that event order is what is tripping up your process.
 
Falcon,

What exactly is the requirement in simple, plain English?
You have chosen a Form/subform as HOW to implement "something" in Access.
It is the something that is unclear (to me).

Typical use of Form/Subform in Access is with Tables that have a 1 to Many relationship.
Example:
A Student takes Many Courses (form/subform linkfield StudentID)
An Order contains Many Products/OrderLine Items

You can not add a record to the table on the Many side if there is not a related record in the 1 side table.
 

Users who are viewing this thread

Back
Top Bottom