• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 151
  • Last Modified:

Passing Variables to Multiple Forms

I have searched for and read several responses to questions and I'm still coming up a little hazy on this.  Here's the deal.  I have a project in which I will have employees updating people's records.  Imagine a list of names that can be double-clicked.  When a name on a list is double-clicked, it displays another form that shows everyone's name, address, etc -- all data we have on that person.  Now from there, I have a third form that can be activated by someone checking on a checkbox control.  When this checkbox is clicked, I want to display a third form that will have some of the variables from the FIRST form.

I can't make these values global or public because someone may have several records open at one time, and may hop from one record to the next and may not update the records in the order they were accessed.  Therefore, I need to create distinct "copies" of this variable available on the third form, and they will always have to come from their own "parent".

This is an example of how the variables would be used:

Open MainForm

Double-click Name #1, create DetailForm #1

Go back to Mainform, double-click Name #2, Create DetailForm #2

Go back to DetailForm #1, click the checkbox, display some of the variables from DetailForm #1

Go back to DetailForm #2, click the checkbox, display some of the varables from DetailForm #2

etc

Somehow I need to make sure all the "children" variables are kept with their proper "parent" variables, if that's the terminology I need to use.  I'm not asking you to write the code for me (although examples are always appreciated)!  It would be much better if you could explain the concept of what I need to do so I can learn it myself  ... I'd appreciate it very much.





0
UPRRDevelopers
Asked:
UPRRDevelopers
4 Solutions
 
GrahamSkanCommented:
You could simply have Public variables on the 'child' form.

Dim frm As New DetailedForm
frm.strParentName = "Selected Name on Mainform"
frm.Visible = True
0
 
UPRRDevelopersAuthor Commented:
Here's what's happening ... hopefully I will explain this properly.

On the Main Form, I have an 'Update Mode' button.  I must create a new form each time the update button is clicked because I need a new instance of the form with every possible update.  So I Dim one ...

Private Sub cmdUpdateMode_Click()
Dim frmWorkingPageVar As New frmWorkingPage

frmWorkingPageVar.lblWarningCaption = "You are Editing a Value"

etc etc etc

End sub

Now I have a new "dynamic" form called frmWorkingPageVar.  That's working fine.

Now the tricky part.  I am trying to launch a brand new form by clicking a checkbox on the frmWorkingPageVar form.  When I click the checkbox, I need to somehow send a variable from the frmWorkingPageVar.  This is where the error is being displayed.

When I click the checkbox, this is called:

Private Sub chkSendEmail_Click()
Dim frmEmailMessageVar As New frmEmailMessage

        Load frmEmailMessageVar
        frmEmailMessageVar.Show
       
End Sub

And this is what happens next:

Private Sub Form_Load()

    Call BuildEmailMessage

End Sub

...

Private Sub BuildEmailMessage()

    If frmWorkingPageVar.lblMgrUserID.Caption <> "" Then
        txtRecipient.Text = frmWorkingPageVar.lblMgrUserID.Caption
    Else
        txtRecipient.Text = ""
    End If

   etc etc

End Sub

I get "Object Required" run-time error 424, and the debug statement returns to highlight the first line of my "BuildEmailMessage" sub.

0
 
p_sieCommented:
You could use classes, where you store your info in.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
GrahamSkanCommented:
The problem is that the scope your frmWorkingPageVar object is local to the cmdUpdateMode_Click sub on the MainForm

You can put a Form (or an frmWorkingPage) Object variable on the email form
Public Dim frmParent as Form

Private Sub chkSendEmail_Click()
Dim frmEmailMessageVar As New frmEmailMessage

        Load frmEmailMessageVar
       Set frmEmailMessagevar.frmParent = Me
        frmEmailMessageVar.Show
       
End Sub
0
 
EDDYKTCommented:
You can use this routine to find your form


Private Function findform(ByVal formname As String) As Form
Dim frm

For Each frm In Forms
If UCase(frm.Name) = formname Then
Set findform = frm
End If
Next
End Function

To call

dim frm

set frm = findform(ucase("DetailForm"))
0
 
pamartzCommented:
What I would do is store a reference to the caller form in a private variable in the called form.

I mean:

When you click the checkbox in frmWorkinPageVar you do:

Private Sub chkSendEmail_Click()
Dim frmEmailMessageVar As New frmEmailMessage

        Load frmEmailMessageVar
        frmEmailMessageVar.DoShow Me  ' Pass current frmWorkingVar form reference
       
End Sub

In frmEmailMessageVar the code would be:

Private frmCaller as frmWorkingPage

Public Sub DoShow(inCallerForm as frmWorkingPage)

    Set frmCaller = inCallerForm
    Call BuildEmailMessage

End Sub

...

Private Sub BuildEmailMessage()

    If frmCaller.lblMgrUserID.Caption <> "" Then
        txtRecipient.Text = frmCaller.lblMgrUserID.Caption
    Else
        txtRecipient.Text = ""
    End If

   etc etc

End Sub

And don't forget to clean up when you leave:

Private Sub Form_Unload
Set frmCaller = Nothing
End Sub

HTH,
 Pedro.
0
 
UPRRDevelopersAuthor Commented:
Thanks for the quick responses.  Any of these look like they would help me.  I'll split the points 100 each for all of your detailed responses.
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now