Using Strings variable to call form controls

dungstar

Registered User.
Local time
Today, 07:44
Joined
Mar 13, 2002
Messages
72
What I am trying to do is assign values to form controls via a public sub procedure called on by a form.

So in testing this is what I have done:

The first msgbox returns the formname.
The second msgbox returns the the value in cboPosition
The third msgbox I am attempting to get the same result as the second msgbox, but it returns an error message "form 'frmNewSample!cboPosition' not found"


Public Sub PosUpdate(FormName As String)
'FormName = frmNewSample
MsgBox FormName
MsgBox Forms!frmNewSample!cboPosition.Value
MsgBox Forms(FormName & "!cboPosition").Value
End Sub
 
This is a simple syntax error. In the third Msgbox call you're concatenating the form name with a control name and using that combined string as the search index within the Forms collection. Obviously there's no form by that name when it searches. You need to reference the Controls collection of the Form as well.

Try this:

MsgBox Forms(FormName).Controls("cboPosition").value

Regards,
John
 
Thanks a lot, John!
 

Users who are viewing this thread

Back
Top Bottom