Linking Forms (1 Viewer)

tucker61

Registered User.
Local time
Today, 07:15
Joined
Jan 13, 2008
Messages
321
Hi guys, i need some help,

I have a main form (FrmMain) with a button to open another Form (FrmInspection)
form main has a autonumber field (TBJobID) which i want to transfer over to the Frminspection form. so that when i open the inspection form it only shows the records for that job number

So on FRMinspection - in the before update event i have the code
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
On Error GoTo Handler
If Me.NewRecord Then
        tbjobid = Forms!FrmMain.Job_ID
Else
    Call AuditChanges("tbJobID", "EDIT", Me)
End If
End Sub

Noticed tonight on my database that some of the inspections on the FRMinspection Form have all been logged against the first job in the database, and not the one they should of been linked to, I can only assume that the code is not running at the right time.

Do i need to move the code to a different trigger.
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:15
Joined
Oct 29, 2018
Messages
21,357
...so that when i open the inspection form it only shows the records for that job number
You can just use the WhereCondition argument to limit the records on the second form. For example:
Code:
DoCmd.OpenForm "FormName", , , "FK_Field=" & Me.PK_Field
Hope that helps...
 

tucker61

Registered User.
Local time
Today, 07:15
Joined
Jan 13, 2008
Messages
321
You can just use the WhereCondition argument to limit the records on the second form. For example:
Code:
DoCmd.OpenForm "FormName", , , "FK_Field=" & Me.PK_Field
Hope that helps...
I could use that, but i am trying to work out why some inspections are are being assigned against the first job.

Example - this week out of 147 inspections, 8 of the have been logged against the first job number in the database,
 

theDBguy

I’m here to help
Staff member
Local time
Today, 07:15
Joined
Oct 29, 2018
Messages
21,357
I could use that, but i am trying to work out why some inspections are are being assigned against the first job.

Example - this week out of 147 inspections, 8 of the have been logged against the first job number in the database,
Not sure I can tell you why without seeing it in action. Sorry.
 

Galaxiom

Super Moderator
Staff member
Local time
Tomorrow, 01:15
Joined
Jan 20, 2009
Messages
12,849
This is often done without code using a subform but the conventional embedded subform structure is not essential.

LinkMasterFields and LinkChildFields used in subform controls iwill work across remote forms. Use the full reference (Forms!frmMain.controlname) in in the LinkChildFields of the subformcontrol hosting frmInspection as its SourceObject inside another form.

The only difference between this and a traditional subform structure is that the remote subform must be requeried in the OnCurrent event of the frmMain.

The form hosting the subformcontrol for frmInspection can be made to look just like an ordinary form by setting the properties of the subformcontrol to hide the Border, Selectors etc.
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 10:15
Joined
Feb 19, 2002
Messages
42,970
As Galaxiom said, Master/Child links ONLY work for subforms. When you open a form, it is not a subform. Therefore, it is not linked to the form that opened it. Two ways to do this. One is to refer to the opening form by name, and the other is to use the OpenArgs of the OpenForm method to pass in the RecID.

THEN in the BeforeInsert event of the popup form use either 1 or 2

1. Me.RecID = Forms!callingform!RecID
2. Me.RecID = Me.OpenArgs

Using the BeforeInsert event is critical. You want this to happen for every new record but ONLY for new records so the BeforeInsert event is the place to put the code. It runs whenever a new record is created immedately after the first character is typed and it only runs once.
 

Users who are viewing this thread

Top Bottom