KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to dynamically load a Subform and instantly display data
PRODUCT: 4D | VERSION: 13.4 | PLATFORM: Mac & Win
Published On: January 31, 2014

Consider, how convenient it would be to have a "generic" form that could be used for multiple purposes yet be compact, versatile, fast, easy to maintain and extensible? Too good to be true? The example below shows one way to do it.


Image 1: Page one of a two page form


In this example, the form contains a button with the object method code displayed in Code Snippet 1.

Code Snippet 1

READ WRITE([People])
ALL RECORDS([People])

OBJECT SET SUBFORM(*;"Subform_Pg2";[People];"Form1")

FORM GOTO PAGE(2)
SET TIMER(1)



Image 2: Page two of the form



The point of interest in this image is the properties of the object named "Subform_Pg2." Note that the only property set under the Sub-form theme is "Source" set to "." This object's properties are dynamically set during the execution of Code Snippet 1, using the command OBJECT SET SUBFORM.

If no further action is taken then execute FORM GOTO PAGE (2). The form would be displayed without data. This is a bye product of the fact that when the master form was loaded, nothing was done regarding page two. To get the data to display, the instructions that address the subform must be given after the subform is fully loaded. That is the role of master form's Form method.

The command SET TIMER (1) triggers 4D to call the Form method, shown in Code Snippet 2. SET TIMER (0) turns the timer off. The command EXECUTE METHOD IN SUBFORM("Subform_Pg2";"GotoObject") makes 4D address the subform and is the trigger that displays the data.

Code Snippet 2


:(Form event=On Timer)
SET TIMER(0)
EXECUTE METHOD IN SUBFORM("Subform_Pg2";"GotoObject")


The trigger action must take place "in the subform." The command EXECUTE METHOD IN SUBFORM causes the project method "GotoObject" to be executed in the context of the subform, not the master form, see Code Snippet 3 below.

Code Snippet 3

GOTO OBJECT([People]No)



Image 3



The finished product is shown in Image 4 below. The data is loaded and the focus is one specified subform object. This form can now be used to display any subform to fit any role with the addition of simple code to meet the need.


Image 4