Open Form Setting Linking to Another Form (1 Viewer)

Weekleyba

Registered User.
Local time
Today, 14:15
Joined
Oct 10, 2013
Messages
586
I have a form (F_ContractInfo) with primary key of ContractInfoID.
On the form is a button (ModRequestButton), that I want to open another form (SF_ModRequest) but have it linked to the F_ContractInfo via the ContractInfoID.

SF_ModRequest has record source set to T_ModRequest that has a field for ContractInfoID.

On the SF_ContractInfo
Code:
Private Sub ModRequestButton_Click()
DoCmd.OpenForm _
  FormName:="SF_ModRequest", _
  DataMode:=acFormAdd, _
  WindowMode:=acDialog, _
  OpenArgs:=Forms![F_Contract]![SF_ContractInfo].[Form].[ContractInfoID]
 
End Sub

On the SF_ModRequest
Code:
Private Sub Form_BeforeUpdate(Cancel As Integer)
  Me![ContractInfoID] = Me.OpenArgs
 
End Sub

I'm able to open the SF_ModRequest, and the ContractInfoID is set correctly.
When I close the form and hit the button again, it wants to add a new record.

How do I correct that?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:15
Joined
Oct 29, 2018
Messages
21,469
If you don't want to add a new record, don't use acFormAdd.
 

Weekleyba

Registered User.
Local time
Today, 14:15
Joined
Oct 10, 2013
Messages
586
Using acFormEdit doesn't work for me either.
I want to add a new record if there is no record there but, open the existing record if one is there.
How do I do that?
 

Weekleyba

Registered User.
Local time
Today, 14:15
Joined
Oct 10, 2013
Messages
586
I think I got it.
I had to add filter it first by way of an On Load event.

Code:
Private Sub Form_Load()
  Me.Filter = "ContractInfoID=" & Me.OpenArgs
  Me.FilterOn = True
 
End Sub

Is that the best way to do it?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:15
Joined
Oct 29, 2018
Messages
21,469
Using acFormEdit doesn't work for me either.
I want to add a new record if there is no record there but, open the existing record if one is there.
How do I do that?
I think I got it.
I had to add filter it first by way of an On Load event.

Code:
Private Sub Form_Load()
  Me.Filter = "ContractInfoID=" & Me.OpenArgs
  Me.FilterOn = True

End Sub

Is that the best way to do it?
I think the best way is to check if the record exist and then edit it, or add a new one if it doesn't. Assigning values in the Open event could lead to empty records.
 

Users who are viewing this thread

Top Bottom