Add datasource to a form dynamically

(My first post – I’m delighted to join the community…)

I have a custom form. No datasources are assigned to the form in AOT. I’d like to add various form datasources to the form at runtime depending on certain conditions.

I’ve learned from the MSDN documentation that I can add a datasource to form if the form is created from code – I used http://msdn.microsoft.com/en-us/library/formrun.datasource.aspx as an example:

After creating the form

form = new Form() )

I can use

formBuildDataSource = form.addDataSource(dictTable.name());

to add a datasource, retrieving a FormBuildDataSource object for which I can set the table ID.

As the last lines of the example show, after creating a FormRun object, running and detaching it, I can access this datasource (as a FormDataSource object):

    // Create the run-time form. 
    formRun = new FormRun(Args); 
    formRun.run(); 
    formRun.detach(); 
 
    // Return an object for the first data source ...
    formdatasource = formRun.dataSource(1); 

So, this is fine for me: I can access a form data source as FormDataSource object if I add the datasource as FormBuildDataSource to the form beforme FormRun is instantiated.

But what about adding a datasource dynamically to an AOT-based form which is not created from code but defined in AOT and to be modified from code at startup?

Apparently it is not possible to add a form datasource to a form / FormRun during runtime, isn’t it?

I tried to add statements to the form’s code:

formBuildDataSource = this.form().addDataSource(‘MyTestDataSource’);

formBuildDataSource.table(tablenum(MyTestTable));

but no matter where I put them, either in the form’s overridden init() method after super() call or in the form’s run() method before the super() call, I’m not able to create a FormDataSource object.

If I count the form build data sources:

fbdsCount = this.form().dataSourceCount(); // → gives count=1;

If I count form data sources

fdsCount = this.dataSourceCount(); // → gives count=0

No matter where I try to access the newly added form datasource, even at the end of my (overridden) run() method,

    formdatasource = this.dataSource(1); // returns null

So there is no form datasource created, even if I added a form build data source to the form.

What am I doing wrong?

Or is it impossible to add a form datasource that way, because I do all my operations inside init() or run() which means after the FormRun has been instantiated?

Is there any means to add a form data source dynamically to a running form (FormRun instantiated) which is defined in AOT?

Any suggestions appreciated.

Many thanks in advance,

  • ruhr42

I tried it and it is possible (including the grid built at run-time).

You have to put your code to init() before super() - the kernel code in super() seems to be the place where definition of data sources is turned to actual data sources.

Martin,

many thanks for your quick reply. It really works this way! (Truly, I could have checked this by myself before :frowning: I simply couldn’t imagine that I should do something with the form or its datasources before it’s got initialized by the system in the super() call of init() … So I didn’t dare to place the code before the super() call. Anyway…)

The problem is that I’m not able to determine which of my datasources to add to the form at that point of execution time (means: before init()'s super() call). I can check my conditions only later, after init() and before run(), ore maybe for some conditions only after run(), i.e. really at the form’s runtime.

If I’ve understood it correctly, it is not possible any more to add any datasource to the form (FormRun) at that moment.

Maybe I’ve to rethink my approach, or find another workaround…

Anyhow thank you very much for your suggested solution.

  • ruhr42