Variable to pass current forms new on Open New Form (1 Viewer)

Zydeceltico

Registered User.
Local time
Today, 07:41
Joined
Dec 5, 2017
Messages
843
Hi All -

I have 3 forms: Form1, Form2, and Form3.

Form1 and Form2 each have a button to open Form3.

Form1 and Form2 cannot be open at the same time.

It is not always necessary to open Form3. But sometimes it is and when it is necessary, Form3 needs to acquire the name of whichever Form (1 or 2) opened it.

How do I do this? There are actually 5+ forms that could call Form3.

I'm guessing a variable of some nature but I am really a novice with VBA so clarity and detail really helps.

I should add that when Form1 or Form2 calls Form3, the calling Form is still open and does not close until a "Save" button is clicked on Form3 at which point the calling Form and Form3 are both saved to table.

Thanks!

Tim
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 04:41
Joined
Aug 30, 2003
Messages
36,118
You can use OpenArgs to pass the name of the form (an argument of OpenForm).
 

theDBguy

I’m here to help
Staff member
Local time
Today, 04:41
Joined
Oct 29, 2018
Messages
21,357
Hi Tim. You can pass the name of the opening form through the OpenArgs parameter.
 

Zydeceltico

Registered User.
Local time
Today, 07:41
Joined
Dec 5, 2017
Messages
843
Hi Tim. You can pass the name of the opening form through the OpenArgs parameter.

Hi Guys - what does the OpenArgs statement look like?

In other words do I use Me.Name as the OpenArgs variable?
 

Zydeceltico

Registered User.
Local time
Today, 07:41
Joined
Dec 5, 2017
Messages
843
So on the Load form of the new form I'd like to put this code although I know it isn't right because I don't know how to reference it correctly - but maybe you can see what I am after:

Code:
Private Sub Form_Load()
    Dim varArgs
    varArgs = Me.OpenArgs
    Me.InspectionEvent_FK = Forms!varArgs!InspectionEvent_FK
End Sub

How do I right that correctly?

THANKS

Tim
 

Zydeceltico

Registered User.
Local time
Today, 07:41
Joined
Dec 5, 2017
Messages
843
I'm trying to use this code on the Load event but it isn't working because I think the value I'm trying to pass isn't saved in the still open calling form.

Code:
Private Sub Form_Load()
  Dim rs As DAO.Recordset
  If Not Trim(Me.OpenArgs & " ") = "" Then
    'See if record exists
    Set rs = Me.Recordset
    'MsgBox Me.OpenArgs
    rs.FindFirst "InspectionEvent_FK = " & CLng(Me.OpenArgs)
    If rs.NoMatch Then  'it does not exist so you need to create it
      DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
      Me.InspectionEvent_FK = Me.OpenArgs
    End If
  End If
End Sub


Should I put it on a different event?
 

theDBguy

I’m here to help
Staff member
Local time
Today, 04:41
Joined
Oct 29, 2018
Messages
21,357
Hi Tim. When you open the other form, you could use it this way
Code:
DoCmd.OpenForm FormName:=“FormName”, OpenArgs:=Me.Name
Then, to use it in the opened form
Code:
If Not IsNull(Me.OpenArgs) Then
    MsgBox “This form was opened by “ & Me.OpenArgs
End If
 

essaytee

Need a good one-liner.
Local time
Today, 22:41
Joined
Oct 20, 2008
Messages
512
Hi All -
How do I do this? There are actually 5+ forms that could call Form3.

I should add that when Form1 or Form2 calls Form3, the calling Form is still open and does not close until a "Save" button is clicked on Form3 at which point the calling Form and Form3 are both saved to table.

Thanks!Tim
When opening Form 3 from any of the other forms, is Form 3 opened in Modal fashion, that is, no other form can be accessed until Form 3 is closed?


Also, on Form 3, what data is required from the calling form(s) for Form 3 to accomplish it's purpose?
 

Zydeceltico

Registered User.
Local time
Today, 07:41
Joined
Dec 5, 2017
Messages
843
When opening Form 3 from any of the other forms, is Form 3 opened in Modal fashion, that is, no other form can be accessed until Form 3 is closed?


Also, on Form 3, what data is required from the calling form(s) for Form 3 to accomplish it's purpose?

Form3 is not opened in Modal form.

The calling forms all have a field called InspectionEvent_FK which is originally passed from yet another “original” form that is opened first and before any of the calling forms and that original form is still open at this juncture also. The original form has a field named InspectionEvent_PK.

Form3 needs that numeric value as an FK
 

June7

AWF VIP
Local time
Today, 03:41
Joined
Mar 9, 2014
Messages
5,423
Really doesn't explain why you need to know name of calling form.

If form really needs the FK then pass that with OpenArgs.

How are you passing FK in the other forms?
 

Zydeceltico

Registered User.
Local time
Today, 07:41
Joined
Dec 5, 2017
Messages
843
Really doesn't explain why you need to know name of calling form.

If form really needs the FK then pass that with OpenArgs.

How are you passing FK in the other forms?

Because on the original form I can select one of multiple forms all of which may have need to call and open Form3 and Form3 needs the value of InspectionEvent_FK from the calling form whichever one it is - but mayb the lightbulb just went off for me from what you wrote and I just use DoCmd.....OpenArgs on llmof the calling forms and then it doesn’t matter. Is that what you are thinking?

I’ve tried this currently but no value is passed. I read somewhere that OpenArgs only passes strings? Is that correct? If so, how do I convert the passed string back to a long number?

Thanks
 

essaytee

Need a good one-liner.
Local time
Today, 22:41
Joined
Oct 20, 2008
Messages
512
I’ve tried this currently but no value is passed. I read somewhere that OpenArgs only passes strings? Is that correct? If so, how do I convert the passed string back to a long number?

Thanks
Dim Number as long

Number = Val(SomeStringNumber)
 

essaytee

Need a good one-liner.
Local time
Today, 22:41
Joined
Oct 20, 2008
Messages
512
Form3 is not opened in Modal form.

The calling forms all have a field called InspectionEvent_FK which is originally passed from yet another “original” form that is opened first and before any of the calling forms and that original form is still open at this juncture also. The original form has a field named InspectionEvent_PK.

Form3 needs that numeric value as an FK


Ok, I think, maybe you are over-complicating things a tad. Regardless of how many forms may open the same child form (form 3), there is a master form, that opened any of the calling forms, that is always open. Why not refer back to the master form for your PK/FK references and disregard the intermediate forms altogether?
 

Zydeceltico

Registered User.
Local time
Today, 07:41
Joined
Dec 5, 2017
Messages
843
Ok, I think, maybe you are over-complicating things a tad. Regardless of how many forms may open the same child form (form 3), there is a master form, that opened any of the calling forms, that is always open. Why not refer back to the master form for your PK/FK references and disregard the intermediate forms altogether?

Duh. :) it sounds so reasonable suddenly. Lol - you’re right.

Does that look like:

Me.txtInspectionEvent_FK = Forms!frmInspectionEvent!txtInspectionEvent_PK - and placed on Form3’s On Load event?
 

June7

AWF VIP
Local time
Today, 03:41
Joined
Mar 9, 2014
Messages
5,423
Unless the form is set to always open only to new record, need code that first checks if on new record, otherwise you change value of existing record. Use Open or Current event.

If Me.NewRecord Then
 

Users who are viewing this thread

Top Bottom