Using Strings variable to call form controls (1 Viewer)

dungstar

Registered User.
Local time
Today, 08:14
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
 

jjturner

Registered User.
Local time
Today, 08:14
Joined
Sep 1, 2002
Messages
386
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
 

dungstar

Registered User.
Local time
Today, 08:14
Joined
Mar 13, 2002
Messages
72
Thanks a lot, John!
 

jjturner

Registered User.
Local time
Today, 08:14
Joined
Sep 1, 2002
Messages
386
Glad to help. Hope everything works out for you!
 

Users who are viewing this thread

Top Bottom