Variable Field(control) name

  • Thread starter Thread starter tig
  • Start date Start date
T

tig

What I'm trying to do seems like it should be simple, but I can't seem
to get the syntax right. I have a form that has several controls on it
that are linked to fields in a table. Depending on what the user is
doing I just need to populate the right field(control) with a date.


dim fieldname$

fieldname = A, B, C, D, or whatever

Forms![FormName](fieldname) = Now

Any help on this would be greatly appreciated.

TIA
 
You can use as string variable to refer to a member of the Controls()
collection like this:
Dim strName As String
strName = "DateEntered"
Forms![Form1].Controls(strName) = Now()

If you are passing a control from one procedure to another, it might be
useful to pass the control rather than its name, e.g.:
Dim txt1 As TextBox
Set txt1 = Forms("FormName").Controls("DateEntered")
Call MyFunc(txt1)
Then the function will work like this:
Function MyFunc(txt As Textbox)
Debug.Print txt.Name
Debug.Print txt.Value
Debug.Print txt.Visible
End Function

It's been a while since I saw the $ type declaration character in the
newsgroups. Looks like you've been working with BASIC for quite a while. :-)
 
Back
Top