passing control names as parameters

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

Guest

I'm trying to pass different controls on an open form to an Access2003
function:

function (MyControlName as ???)

Forms!MyForm!MyControlName = "whatever"

end function

Thanks
 
You can pass a control name as a string if you wish:
Function AssignToControl(strFormName As String, strControlName As
String, varValue As Variant)
Forms(strFormName).Controls(strControlName) = varValue
End Function
Then use it like this:
Call AssignToControl("Form1", "Text0", 99)

That won't work with subforms, since the subform is not open in its own
right (i.e. it is not in the Forms collection.) Consequently, it might be
better to pass a reference to the control itself instead of using the form
and control names:
Function AssignToControl(ctl As Control, varValue As Variant)
ctl = varValue
End Function
And use it like this:
Call AssignToControl(Me.Text0, 99)
 
Thanks a million, that works perfectly.

Allen Browne said:
You can pass a control name as a string if you wish:
Function AssignToControl(strFormName As String, strControlName As
String, varValue As Variant)
Forms(strFormName).Controls(strControlName) = varValue
End Function
Then use it like this:
Call AssignToControl("Form1", "Text0", 99)

That won't work with subforms, since the subform is not open in its own
right (i.e. it is not in the Forms collection.) Consequently, it might be
better to pass a reference to the control itself instead of using the form
and control names:
Function AssignToControl(ctl As Control, varValue As Variant)
ctl = varValue
End Function
And use it like this:
Call AssignToControl(Me.Text0, 99)
 
Back
Top