Making a subform visible within a form based on variable

latex88

Registered User.
Local time
Today, 15:22
Joined
Jul 10, 2003
Messages
198
I have a main form with many subforms in it. One of the subforms has many buttons. Depending which button is clicked, the caption of the button and dlookup function will determine which subform within the main form will become visible. How do I reference the subform based on a variable? The code looks something like below, except the syntax referencing the subform is not correct.

Dim strSubForm as string
strSubForm = dlookup(............................)
Me.strSubForm.visible = True
 
You can use

Me.Controls(strSubform).Form.Visible = True

Where the control name (strSubform) has to be the name of the Subform CONTROL which houses/displays the subform on the parent form and NOT the subform name unless they are named exactly the same.

The .Form part just stays as I have it shown.
 
You can use

Me.Controls(strSubform).Form.Visible = True

Where the control name (strSubform) has to be the name of the Subform CONTROL which houses/displays the subform on the parent form and NOT the subform name unless they are named exactly the same.

The .Form part just stays as I have it shown.

Thanks for the quick response, Bob, but when I incorporate your code, I get an error message of"...can't find the field 'XXX' referenced to in your expression." I know XXX was found as I debugged it using msgbox.
 
Post exactly what you used and use the screenshot here:
http://www.btabdevelopment.com/ts/ewtrhtrts
to let me know exactly what the subform name is and the subform control name too.

Name of main form: frmOrder
Name of control: sfrmCategoryMenuSelect
Dim FormType As String
strCategory = Left(Me.ActiveControl.Caption, InStr(1, Me.ActiveControl.Caption, Chr(13)) - 1)
FormType = DLookup("SecondaryForm", "tblCategory", "Category = '" & strCategory & "'")

Me(FormType).Form.Visible = True
 

Attachments

  • subform.jpg
    subform.jpg
    19.5 KB · Views: 148
Put a breakpoint and verify that your variable FormType is returning the correct value of sfrmCategoryMenuSelect.
 
I just noticed my mistake. The button that triggers the event is actually a subform within the main form, thus, "Me." didn't work. Now I replaced it with

"Forms!frmOrder.Form(FormType).Form.Visible = True" it worked.

Thanks Bob for your help.
 

Users who are viewing this thread

Back
Top Bottom