another form

Lanason

Registered User.
Local time
Today, 00:25
Joined
Sep 12, 2003
Messages
258
I have some code in a form where i am preparing a delivery note including a Customer code.

I have a form with the delivery address options for the specific Customer in another form

How do I jump across get the data and revert back to continue in the original form?
 
You can refer to the other form in code using either of these methods:

Forms!NameOfForm
or
Forms("NameOfForm")

so if the form is called DeliveryOptions and it has a textbox called Address you can read that into a string s with

s = Forms!DeliveryOptions.Address
or
s = Forms("DeliveryOptions").Address
 
You can refer to the other form in code using either of these methods:

Forms!NameOfForm
or
Forms("NameOfForm")

so if the form is called DeliveryOptions and it has a textbox called Address you can read that into a string s with

s = Forms!DeliveryOptions.Address
or
s = Forms("DeliveryOptions").Address

thanks problem is that I need to wait to have user interface to select the record in the other form, then carry on.
I know how to get data from another form automatically.
 
The other form can run code in this form in the same way:

Make a public sub in Form 1 that does the remainder of the processing and takes as arguments all the details you need off Form 2
Form 1 opens Form 2 as a popup modal.
In the close event (or equivalent, after validating the user's selection) of Form 2 call the public sub on Form 1 passing the details you're interested in.

So it's the same principle but in the opposite direction and calling a sub rather than reading a control value.


Or, the crude method is just use global variables and open Form 2 as a dailog. When it closes (or whatever) it sets the values of the global variables and Form 1 reads them when execution resumes there.
 
The other form can run code in this form in the same way:

Make a public sub in Form 1 that does the remainder of the processing and takes as arguments all the details you need off Form 2
Form 1 opens Form 2 as a popup modal.
In the close event (or equivalent, after validating the user's selection) of Form 2 call the public sub on Form 1 passing the details you're interested in.

So it's the same principle but in the opposite direction and calling a sub rather than reading a control value.


Or, the crude method is just use global variables and open Form 2 as a dailog. When it closes (or whatever) it sets the values of the global variables and Form 1 reads them when execution resumes there.

thanks - i will try tomorrow
 
The other form can run code in this form in the same way:

Make a public sub in Form 1 that does the remainder of the processing and takes as arguments all the details you need off Form 2
Form 1 opens Form 2 as a popup modal.
In the close event (or equivalent, after validating the user's selection) of Form 2 call the public sub on Form 1 passing the details you're interested in.

So it's the same principle but in the opposite direction and calling a sub rather than reading a control value.


Or, the crude method is just use global variables and open Form 2 as a dailog. When it closes (or whatever) it sets the values of the global variables and Form 1 reads them when execution resumes there.

Used the idea of transferring to the other form which works great, but how do I take variables assigned in the first form over to the second.

I use "private sub" in the code and "DIM x as String" for the variables.

Need the syntax for public / global variables or something like that - please
 
If the second form is opened as a dialog then there's two ways: global variables (set their values in the first form and read them in the second) or OpenArgs. You can combine parameters into the one parameter of the OpenArgs with a delimitter and then use the Split function in the Form Open event of the second form to get them back into their parts.

So say Form1 opens Form2 wanting to pass three variables (two strings and one integer) to it:

Code:
DoCmd.OpenForm "Form2",,,,acDialog, str1 & "|" & str2 & "|" & int1

Then in Form2's module

Code:
Private str1 As String
Private str2 As String
Private int1 As Integer

Private Sub Form_Open(Cancel As Integer)
    'Check for valid OpenArgs
    Cancel = IsNull(Me.OpenArgs)
    If Not Cancel Then 
        Dim varOpenArgs
        varOpenArgs = Split(Me.OpenArgs,"|")
        Cancel = Not IsArray(varOpenArgs)
        If Not Cancel Then
            Cancel = (UBound(varOpenArgs) <> 2)
            If Not Cancel Then
                Cancel = Not IsNumeric(varOpenArgs(2))
                If Not Cancel Then
                    Cancel = (CStr(Int(varOpenArgs(2))) <> varOpenArgs(2))
                    If Not Cancel Then
                        str1 = varOpenArgs(0)
                        str2 = varOpenArgs(1)
                        int1 = varOpenArgs(2)
                    End If
                End If
            End If
        End If
    End If
    If Cancel Then MsgBox "Necessary OpenArgs not supplied to Form2. Form will close."
End Sub

The OpenArgs method is more work but it's more encapsulated. There's no danger of another sub somewhere changing the values (even though the form's opened as a dialog, timer events on other forms could change global variable values).

If the form isn't opened as a Dialog then you can just make the variables public in Form2 and assign them values but then you still need a public sub to tell the form when they're ready to be used. It's better practice to pass the variables as arguments to that sub:

Code:
DoCmd.OpenForm "Form2",,,,acDialog
Forms("Form2").SetVariables(str1, str2, int1)

But the downside to not opening Form2 as a dialog is that Form1 needs to be told when it's been closed. If it's opened as a dialog code won't continue on Form1 until Form2 is closed.
 
Need the syntax for public / global variables or something like that - please

Global variables are declared public in a module and as such are accessible from any other module including form modules.

They're not good programming practice for various reasons that don't all completely apply in Access (they're mostly bad practice for truly object oriented and multi-threaded languages) but they can lead to problems if they're not used carefully and sparingly. It is always better practice to have private variables and pass them between modules as parameters to subs and functions. However, they are a very quick and easy fix for passing variables between forms if you can be sure no other events will interfere with their values.
 

Users who are viewing this thread

Back
Top Bottom