Link to home
Start Free TrialLog in
Avatar of PJUN
PJUN

asked on

Open a form and set properties where the form name is stored as a variable

I have 4 forms.   3 of the forms display information. They are called: Form_Info_A,  Form_Info_B,  Form_Info_C

The fourth form is used to select which of the other 3 is opened.  It is called:   Form_Select

On "Form_Select" I have a TextBox control named "MySelection" in which the user will enter the name of one of the three information forms.

On "Form_Select" I also have a Command Button named "cmdPrint" that will:
1) Open the selected form (A, B or C)
2) Change some properties of the selected form
3) Print the selected form
4) Close the selected form

In the OnClick event procedure behind the Command Button I can Open, Print and Close the selected form using a variable which contains the name of the selected form as follows:

'****************************************
Dim stDocName as String
stDocName = Me.MySelection
DoCmd.OpenForm stDocName, acNormal

' ... I need to "SET PROPERTIES" code here

DoCmd.PrintOut
DoCmd.Close
'****************************************

MY PROBLEM is that I want to set some of the properties of the chosen form before it is printed (eg Width, Height, etc) For example I want to change the color of one of the controls named "MyControl".   If I HARDCODE the name of the selected form, and place the following code between the OPEN and PRINT commands then the properties can be set:

Forms!Form_Info_B!MyControl.BackColor = 0

This code works

HOWEVER, I do not want to hardcode one of the form names, I want to use the variable that contains the selected name.

I have tried the following but IT DOES NOT WORK

Forms!STDOCNAME!MyControl.BackColor = 0

Can somebody please tel me how to set the properties using the variable as the form name?

Thank You



ASKER CERTIFIED SOLUTION
Avatar of LordThlan
LordThlan
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of gnicoli
gnicoli

Thee correct syntax is
Forms(stDocName).myControl.BackColor = 0

The use of Forms!Form_name!ControlName require the name of the form and not a variable representing it.

--
Giorgio