passing open args to a new form class

rkmaitra

Registered User.
Local time
Today, 09:25
Joined
Jul 11, 2011
Messages
40
Hello,
I have a form to which I pass some OpenArgs and WhereCondition when I open it from another form. I am now trying to use multiple instances of the form. The code I am using is given below:

Private mcolFormInstances As New Collection

Function OpenFormInstance(FormName As String, Optional WhereCondition As String, Optional OpenArgs As String)
'Declare the form name
Dim frm As Form

gblnOpenedByAnother = True

'Set frm = New Form_Bank

Select Case FormName
Case "frmBank"
Set frm = New Form_frmBank
Case Else
Debug.Assert False
End Select


This is all fine but when I try and assign some OpenArgs to the newly created frm, it tells me that this is read-only. Is there any way of passing the OpenArgs to the form when I am setting the new class object?

Many thanks for your help,
Rudra
 
You can't set the Form instance's OpenArgs, but you can use a Public Variable in the form to pass arguments to the specific form instance.
In your module code declarations, place a statement like:
Code:
[COLOR="Navy"]Public[/COLOR] gOpenArgs [COLOR="navy"]As Variant[/COLOR]
Then, your module code could read something like:
Code:
gOpenArgs = "MyValue1"
[COLOR="navy"]Set[/COLOR] frm = [COLOR="navy"]New[/COLOR] Form_frmBank

In your form's code declaration, you can have something like:
Code:
[COLOR="Navy"]Public[/COLOR] fOpenArgs [COLOR="navy"]As Variant[/COLOR]
In your form's Load event, you can have code like the following:
Code:
[COLOR="Navy"]Private Sub[/COLOR] Form_Load()
    fOpenArgs = gOpenArgs
    gOpenArgs = [COLOR="navy"]Empty
End Sub[/COLOR]

From there, you can write the rest of your form's code to use the value in fOpenArgs.
 
What I commonly do is give the form a Public Sub Show() that takes a parameter, like ...
Code:
Public Sub Show(MyArgs As Variant)
[COLOR="Green"]   'do form initialization[/COLOR]
   DoInit MyArgs
[COLOR="Green"]   'show the form[/COLOR]
   Me.Visible = True
End Sub
... and then your consumer can do something like ...
Code:
dim frm as new form_fYourForm
frm.show Me.InitData
... and you don't need a global variable.
 
Thank you both very much. I shall try these solutions.
Rudra
 
Hello, lagbolt,

For forms, your solution works, because you have the opportunity to call a Public Sub in the form instance subsequent to its instantiation. It is important to note, however, that the same can not be done with a Report instance; all related code in the report is executed and completed directly the instance is created. In such a scenario, the global variable solution is required.
 

Users who are viewing this thread

Back
Top Bottom