How to open a user form via programming, giving the form a parameter while doing so


Question: I want to open a user form from a command that resides in another user form. Moreover, while doing so, I want to send a parameter to the user form which will be opened by this command. How can I achieve this?

Answer: Let’s show this in an example where we will open the choice form of a catalog giving the choice form a parameter too so the data of the choice form gets filtered with this parameter.

For demonstration, we use a simple infobase where we have three catalogs: Cars, Bodies and Engines. As seen in the screenshot below, The Bodies catalog has a tabular section where the compatible engines with a given body are stored. A Car is consisted of a Body and a compatible Engine. So, when we create a Car model (Extravagance Sedan Lux T300) and choose the body to be used in this car (Luxury sedan), as the next step, when attempting to choose the engine of the Car, we want the Engines catalog Choice form get filtered so that only the compatible engines with the chosen Body will be shown in the Engines Choice form’s list.

image-20230606225107-1.png

We set up the buttons of the Engine item of the Cars catalog item form as shown on the screenshot below so only choice and open buttons show up, and not the choice history list. Afterwards, we write the code shown on the below screenshot to the StartChoice event of the Engine form item. We use the OpenForm() method of the platform. While an alternate way would be using the GetForm() method of the platform to get the form and afterwards open it, we suggest you do not use this method, as it is considered a legacy method. So, OpenForm() method is the way to go.     

image-20230606225114-2.png

Then on the ChoiceForm of the Engines catalog, we setup a filter for the “List” dynamic list of the form using the parameter (Body) we had given to this form from the Cars catalog item form. In the meanwhile, we could have explicitly defined ChoiceForm parameters, like the TestParameter if we wanted to.

image-20230606225121-3.png

Please take note that there are other more suitable ways to filter a choice form, such as using “Choice parameters links”, the method shown in this article was chosen for demonstration purposes (how to open a form from another module while giving the form a parameter) only.

Another note: The parameters you give to a form will be available only during the OnCreateAtServer and OnOpen events of the form, for the proceeding events, the parameters of the form are not available. So, if you will need the values the form parameters for the proceeding form events, you can create Attributes of the form corresponding to the parameters of the form and fill out those Attributes of the form from the parameters of the form values during the OnCreateAtServer or OnOpen events, so that the values of the parameters will be saved in form Attributes, which are available anytime while the form is open.  

The sample .dt file you can download here:

faq_2023_06_1st.dt

Icon/Social/001 Icon/Social/006 Icon/Social/005 Icon/Social/004 Icon/Social/002