Passing several parameters to subform

inoxo

Registered User.
Local time
Today, 18:07
Joined
Sep 8, 2005
Messages
42
Hi,

I would like to transmit several parameters from my main form to the subform when I open it.

Parameters could be used in the subform by onload event, for eample, to define form caption, hide or show buttons, enable or disable edit boxes, give them a color, ... It could also allow to use the same form in different application contexts, which would reduce so the development time.

More than one technique might be possible.
I used the following one :
- I put the parameter value I want to pass into myParameter
- when click on the button to open the subform :
DoCmd.OpenForm myDocument, acNormal, , , , , myParameter
- when loading the subform, retreive the parameter value in Me.OpenArgs and using it (eg disable a button)

It works fine. But I can pass only a single value with this technique and it's not enough. I tried to give an array as myParameter and fill it with my parameters values, but Access refuses an array to be in the DoCmd statement for OpenArgs option.

Any idea about passing several parameters to the subform ?
Or may be an alternative to the DoCmd technique ?

Thanks.
 
You can pass multiple parameters in the OpenArgs argument string by concantenating them together separated by a delimiter.

MyParams = "Param1" & ";" & "Param2" & ";" &Param3"

Then you can use the Split() function to separate them in the form you are opening.

Code:
Dim Args As Variant

If Not IsNull(Me.OpenArgs) Then
    '-- Form is being opened from a form passing parameters
    Args = Split(Me.OpenArgs, ";")
    Param1 = Args(0)
    Param2 = Args(1)
    Param3 = Args(2)
End If
 
That's a smart way to do it.
Thank you both of you.
 
If you need to pass a large number of items to your subform , particularly if they don't fit conveniently into a text stream, then the easy way is to use a synchronising class and the event actions.

Simply, when you click on form to open it
first the sub-form(s) have an open event and a load event
Then the form has an open and load event
and then the form becomes visible.

So add a class module ClsX which contains all the information you are likely to want to pass.



So, declare a class object of (clsX) in the main form as a module level object
Dim clsA as new clsX

in the subform declare
dim clsY as clsX at module level

and a procedure in the subform (eg ThisIsMyProcedure)

Sub ThisIsMyProcedure
set clsY=Me!Parent.clsA
end sub

then in the main form Load event
use clsA
eg ClsA.mycaption= "Nursery Rhymes"
~~ set up the data in clsA
clsA.Datarecord=myrecordset (as a recordset)
and so on.

(or explicitly set clsA by)
set clsA=clsX ("New" is in the dimension statement for mainform)


~~ and run the procedure in the subform form.eg;

Form(subformFrameName).Form. Form_MyProcedure

You now have instances of the same class object open in both the form and the subform. Whatever has been (or will be) put in the class object by one is instantly available to the other.

So if you put a caption name in property clsA.Captionname in the mainform it will be accessible by the subform's "ThisisMyProcedure" as clsX.CaptionName and so on.

Why do it like this? Well you might re-use the subform's form in a variety of different ways. You can even declare the class object originally in a completely different process (Say a process that analyses which forms and subforms and controls and records it wants to appear. The main form can take that class in (set clsA=othermodules.clsX) and pass it down to its subforms too.) You could do it by a whole variety of different OpenArgs strings or you can standardise on the one class object.

Regards
Rsss
 
Welcome to the Access World Forums rsss! Something tells me we are going to see some really interesting ideas coming from your direction. Again, welcome and Happy New Year!
 

Users who are viewing this thread

Back
Top Bottom