What's wrong with this reference to a control?

  • Thread starter Thread starter Karen
  • Start date Start date
K

Karen

From another modal form I am trying to access the value in a text box on a subform. The subform is sfrmContact, the main form is frmBusinessTab, the control is txtlastname.

I have tried:

Dim strNameSearchCriteria As String
strNameSearchCriteria = Forms!frmBusinessTab.sfrmContact!txtLastName

AND
Dim strNameSearchCriteria As String
strNameSearchCriteria = Forms!frmBusinessTab.sfrmContact.Form!Controls.txtLastName

and both come up with following error:

"Application-defined or object-defined error"

Karen
 
is sfrmContact the name of the control that contains the subform on the main form? If not, the code will not work.

This should work
frmBusinessTab.sfrmContact.Form!txtLastName
From another modal form I am trying to access the value in a text box on a subform. The subform is sfrmContact, the main form is frmBusinessTab, the control is txtlastname.

I have tried:

Dim strNameSearchCriteria As String
strNameSearchCriteria = Forms!frmBusinessTab.sfrmContact!txtLastName

AND
Dim strNameSearchCriteria As String
strNameSearchCriteria = Forms!frmBusinessTab.sfrmContact.Form!Controls.txtLastName

and both come up with following error:

"Application-defined or object-defined error"

Karen
 
Never mind, found the problem but am still puzzled.

Turns out the subform control on the main form is frmContactQuery with a data source of the form sfrmContact. When I changed the reference to:

strNameSearchCriteria = Forms!frmBusinessTab.frmContactQuery!txtLastName

it worked just fine. Guess it confuses me why the subform control doesn't take the name of the subform itself.

Karen Hagerman
University of Phoenix

Practitioner Faculty

(e-mail address removed)


From another modal form I am trying to access the value in a text box on a subform. The subform is sfrmContact, the main form is frmBusinessTab, the control is txtlastname.

I have tried:

Dim strNameSearchCriteria As String
strNameSearchCriteria = Forms!frmBusinessTab.sfrmContact!txtLastName

AND
Dim strNameSearchCriteria As String
strNameSearchCriteria = Forms!frmBusinessTab.sfrmContact.Form!Controls.txtLastName

and both come up with following error:

"Application-defined or object-defined error"

Karen
 
Karen said:
Never mind, found the problem but am still puzzled.
Turns out the subform control on the main form is frmContactQuery with a data source of >
the form sfrmContact. When I changed the reference to:
strNameSearchCriteria = Forms!frmBusinessTab.frmContactQuery!txtLastName
it worked just fine. Guess it confuses me why the subform control
doesn't take the name of > the subform itself.

You must have changed it then. When you drop a sub-form onto a main form
the name for the sub-form control will default to the same name as the form
being referenced. If that name is already in use it will substitute a name
like "Child1". There's no way I know of for Access to auto-assign a name
for the control that is merely similar to the form within, but not an exact
match.

My guess is that you changed the name of the form being referenced in the
sub-form control after it was added to the main form. The control will not
automatically track name changes to the form. It will only grab the name
when it is created initially.
 
From another modal form I am trying to access the value in a text box on a subform. The subform is sfrmContact, the main form is frmBusinessTab, the control is txtlastname.

I have tried:

Dim strNameSearchCriteria As String
strNameSearchCriteria = Forms!frmBusinessTab.sfrmContact!txtLastName

AND
Dim strNameSearchCriteria As String
strNameSearchCriteria = Forms!frmBusinessTab.sfrmContact.Form!Controls.txtLastName

The syntax uses the Name property *of the Subform control on the
mainform*, which is not necessarily the same as the name of the form
within that control. If the Subform Control is named (for example)
sbfrmContact, the proper syntax would be

Forms!frmBusinessTab!sbfrmContact.Form!txtLastName

The Form! operand specifies that you mean "the form object contained
within this subform control"; strictly speaking it's optional but I
find it clearer and more reliable to routinely type the extra five
bytes.
 
DLI, Rick and John,

Thanks. Makes more sense and yes, I did change the name of the underlying form after I had added it as subform!

Karen Hagerman
University of Phoenix

Practitioner Faculty

(e-mail address removed)


From another modal form I am trying to access the value in a text box on a subform. The subform is sfrmContact, the main form is frmBusinessTab, the control is txtlastname.

I have tried:

Dim strNameSearchCriteria As String
strNameSearchCriteria = Forms!frmBusinessTab.sfrmContact!txtLastName

AND
Dim strNameSearchCriteria As String
strNameSearchCriteria = Forms!frmBusinessTab.sfrmContact.Form!Controls.txtLastName

and both come up with following error:

"Application-defined or object-defined error"

Karen
 
Back
Top