Basic question

TommyQ

New member
Local time
Today, 11:11
Joined
Jul 26, 2011
Messages
2
Hi Folks,

I'm currently writing a Sub to deal with all my database reference code. If I was to normally refer to a form object like a textbox using a Sub I would normally type the follow :

Forms.FRMHomePage.txtFirstname = "Laoistom"

What I want to do is have variables passed into the SUB that will fill in the "FRMHomePage" and "txtFirstname" part of the statement above. However, I get errors everytime I try this. Is this a restriction of VBA or is there something i'm not seeing here.

Cheers,
Laoistom.
 
Code:
Try this

Dim myForm As Object
Set myForm = [Forms]![FRMHomePage]   ' note the ! not .

Dim myField as string
myfield = "txtFirstname"


myForm.Controls(myField).Value =  = "Laoistom"
 
If you are refering to form of the module, you can refer to it using "Me"
Code:
Me.ConrolName
If you are refering to a form that is currently opend, you can refer to it like this:
Code:
Application.Forms(FormName)' FormName is a string varible
If you are refering to a form that is currently closed, you can refer to it using AllForms, just note, you cant put any of its Controls in focus...:
Code:
AllForms(FormName)
A control would be refered to by using the Controls Collection of the form:
Code:
Application.Forms(FormName).Controls(ControlName) ' FormName and ControlName are string varibles
 
Thanks Guy,

The last Suggestion from Marlan worked a treat. To be honest I feel a little embarrassed having to ask it was just wrecking my head altogether. I'm at this long enough but i've never passed form and control variables into a Sub or Function before.

Thanks again guys,
Laoistom
 

Users who are viewing this thread

Back
Top Bottom