Accessing Form Objects

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

If I have a form 'FormA', say, then I can access a control 'ControlA', on
that form, as FormA.ControlA and I can find the name of the form as
FormA.Name. If, however, FormA.Name is provided to me by a piece of code I
cannot immediately see how to use this form name string, to access
FormA.Control. A clumsy method is to use a Case statement to Set Form
appropriately, according to the value of FormA.Name, but there must be a
simpler way.

In other words, if stFormName = FormA.Name and I have stFormName, how do I
get FormA?
 
Dim objForm as Form
Dim strFormName as String
strFormName = "FormA"
Set objForm = Forms(strFormName)
objForm.ControlA = "some value"
 
Many thanks. I have spent hours looking for the right syntax. You have
saved me a lot more searching.
 
It might be worth pointing out that that won't work if FormA isn't already
open: the Forms collection only contains open forms. In that case, you need
to use OpenForm first.
 
My apologies. I did not spot that I had another reply until a few minutes
ago, so it did not get rated earlier.

It is the more arcane aspects of VBA that the 1000+ page 'manuals' so often
fail to mention but at least the form naming convention is now clear. Many
thanks.
 
Back
Top