Indirect Outside reference of a form control?

I need to access a form control from a procedure. For example, I have the form "Client" and the control "Name" then I can fill the form control like this:

Call FillForm ("Client")

Sub FillForm (FormName As Variant)
  Forms(FormName).name = "Test string"
end Sub

This works fine. However I now need to fill in many different controls of the same form and hence would like to concatenate the control as follows:

Call FillForm ("Client","Name")

Sub FillForm (FormName,FormCtrl As Variant)
  Forms(FormName).FormCtrl = "Test string"
end Sub

Obviously this does not work. Is there a way how i can reference the control from a function (i cannot use me.controls...

Thank you MIchael
mpimAsked:
Who is Participating?
 
Anders Ebro (Microsoft MVP)Microsoft DeveloperCommented:
You can reference a control by using the syntax
Forms("formName").Controls("controlname")

Open in new window

BUT this only works for forms that are not used as subforms. So if you wanted to use a subform it might be
Forms("formname").SubFormControlName.Form.Controls("ControlName")

Open in new window

0
 
Anders Ebro (Microsoft MVP)Microsoft DeveloperCommented:
It depends on bit on the point of origin of your code calling. If you are allready on the form, you can simply pass the form OBJECT to your function.
Sub FillForm (Frm as Form)
   frm.ControlName="SomeValue"
end Sub

Open in new window

You could call it like so:
  FillForm Me
0
 
mpimAuthor Commented:
Thank you but I'm calling it from an outside function. HOwever, I have found the solution for regular forms controls:

Forms(lFormName)("xF" & lFormNumber & "L" & ctr)  = "Test string"

This works fine. I still have not found the solution for addressing in the same way BUTTONS.. Any help would very much be appreciated!
0
 
Steve JCommented:
It depends on bit on the point of origin of your code calling.
Use this syntax:
Forms("formName").Controls("controlname")

Open in new window

Yahoo mail login
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.