Use variable in [Forms]![FormName]![FieldName].Property

oihjk

Registered User.
Local time
Today, 04:53
Joined
Feb 13, 2003
Messages
51
Is this possible to do?
For example my form name is frmBogus Field name is fldBogus. So normally I would reference its properties like this:
Code:
[Forms]![frmBogus]![fldBogus].SetFocus
Would it be possible to reference these properties with varibles?
Example:
Code:
Dim stFormName As String
Dim stFieldName As String

stFormName = "frmBogus"
stFieldName = "fldBogus"
[Forms]![stFormName]![stFieldName].SetFocus
I know this isn't possible, but you now know what I want to accomplish. Anyone know how to do it like this or know of another way to get it done?

Thanks,
Eric
 
Yes, this is possible.

Realize that:
[Forms]![frmBogus]![fldBogus]
is but one way of referring to the "fldBogus" field. The more explicit method is this:
Forms("frmBogus").Controls("fldBogus")

In order to reference a field using a variable, you can replace the pieces within (and including the) quotation marks.

So, if you use:
stFormName = "frmBogus"
stFieldName = "fldBogus"

then you can refer to the "fldBogus" field like this:
Forms(stFormName).Controls(stFieldName)

Or since the Controls collection is the default collection of a form, you can use:

Forms(stFormName)(stFieldName)
 
Beautiful. Thanks for the insight.

Eric
 

Users who are viewing this thread

Back
Top Bottom