Subforms question..Code related..

A

Alex

hi anyone.
short question.
I have:

str1 ="nameofform"
str2 ="nameofcontrol"
All strings!

How using this two string i can assign a value to the
control that belongs to that form?
Like:
Forms!frmMyform!mycontrol = "somevalue"

The way:
Forms!str1!str2 = "somevalue" - does not work...

the values for str1 and str2 are changing, so i need to
use them for universal use of my procedure.
 
A

Alex

How about if form - is actually subform,
and control is actually a control on subform?

There is only collection of form...so this:
Forms(str1).Controls(str2) won't work, because there is
no such a form with the subform's name...
I might do this:
Forms(str1). where str 1 - the name of the form where the
subform is located, then... .Controls
("somecontrol.Forms.subformcontrol") but it doesn't work
either..because there is no such control on that form....

So ..anybody?
 
J

John Spencer (MVP)

Why don't you just pass a reference to the control. Then you don't need the
strings at all and the control will get updated on a form or on a subform

Public Sub SetControl(cntlAny as Control)

cntlAny = "SomeValue"
End Sub

Call it with

SetControl Me.ControlName

or

SetControl Forms!Formname!ControlName

or

SetControl Forms!FormName!SubFormControl.Form!ControlOnSubForm

If for some reason, you need the name of the control

cntlAny.Name
will give you the control's name and you can use the parent property to get the
name of the form (or subform.Form) that has the control.

cntlAny.Parent.Name (If I recall correctly)
 
A

Alex

I thought..i would give more details about the problem...

I have a form, form has a subform, the subform has a
control..
if i use the structure:
a=Forms(strFormName).Controls(strControlName).value with
the form and the control on it, where strFormName and
strControlName - strings describing the form's and
control's name - it works fine...BUT...

What if i want to get to subform and subform's control?
What structure should i use?
a=Forms(strFormName).Controls(strControlName).value -
this won't work,
because the subform's name is actual Control's Name.

But i need to get to that control on that subform...

Any hints will be appreciated..
 
A

Alex

Thanks for your answer..but I am got confused..

Why do i need to use this structures:
SetControl Me.ControlName
SetControl Forms!Formname!ControlName
SetControl Forms!FormName!SubFormControl.Form!

including the function SetControl just to do this:
Me.ControlName=somevalue
Forms!Formname!ControlName=somevalue
Forms!FormName!SubFormControl.Form!control=somevalue
???

What i am looking for is:
1. I have function - this function takes two arguments:
a) strFormname
b) strFormcontrol
it returns the values to the form using this structure:
a=Forms(strFormName).Controls(strFormcontrol)

Problem:
What if i use the subform with a field on it?
1. My function will take three arguments:
a) strFromname
b) strFormcontrol - at the same time a subform's name
c) strSubFormcontrol - control on subform

it returns the value to the form using what structure?
a=Forms(strFormName).Controls(strFormcontrol).Forms!???

The problem

I am doing this because i would love to use my function
everywhere..regadless what control has called it..from a
form or subform...

Thanks for your time and help...
 
A

Alex Dybenko

try:
a=Forms(strFormName).Controls(strSubFormcontrol).Form.Controls(strFormcontro
l)

where strSubFormcontrol is a name of subform control where you have your
subform and strFormControl control
 
J

John Spencer (MVP)

Because you are specifically referring to the control, you must specify the
entire path to the control.

HOWEVER; if you pass a reference to a control and not the parts of the "path" to
the control then the control contains the entire path. So you don't have to
worry about "building" the "path" to the control.

Since you have not posted your code it is hard to say whether or not my
suggestion would work or not. I did post a small bit of sample code, that you
might be able to adapt.

Think of it this way. I hand you a glass and say fill this glass. You don't
have to ask me which glass, on what shelf, and in the front or back of the
shelf. You have the glass in your hand.

On the other hand, if I say fill a glass. You are going to ask me on what
shelf? Is it in the front or the back? Is it the third one from the front? OH!
By the way, in which cabinet? Is it in this room?

So if you can just pass a reference to the control.
Thanks for your answer..but I am got confused..

Why do i need to use this structures:
SetControl Me.ControlName
SetControl Forms!Formname!ControlName
SetControl Forms!FormName!SubFormControl.Form!

including the function SetControl just to do this:
Me.ControlName=somevalue
Forms!Formname!ControlName=somevalue
Forms!FormName!SubFormControl.Form!control=somevalue
???

What i am looking for is:
1. I have function - this function takes two arguments:
a) strFormname
b) strFormcontrol
it returns the values to the form using this structure:
a=Forms(strFormName).Controls(strFormcontrol)

Problem:
What if i use the subform with a field on it?
1. My function will take three arguments:
a) strFromname
b) strFormcontrol - at the same time a subform's name
c) strSubFormcontrol - control on subform

it returns the value to the form using what structure?
a=Forms(strFormName).Controls(strFormcontrol).Forms!???

The problem

I am doing this because i would love to use my function
everywhere..regadless what control has called it..from a
form or subform...

Thanks for your time and help...


SNIP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top