Adding (and removing) controls at runtime

  • Thread starter Thread starter Mike Morton
  • Start date Start date
M

Mike Morton

Hello

I want to create a dynamic form, which will create and remove fields from a
form based on the data accessed. The field names, data types and data
source names are stored in a 'configuration' table, which links to a key
name in the main data table.

This setup currently works by having all the fields permanently on the
form - but invisible, showing only when required. This, however, doesn't
give me the ability to reorder their tabindex, which can be frustrating for
the user.

I have a Records table :
RecordID
RecordType ** LINK
Field1
Field2
Field3
etc.


and a configuration table:
RecordType ** LINK
Field1Visible
Field1Order
Field1Top
Field1Left
Field2Visible
Field2Order
Field2Top
Field2Left
Field3Visible
Field3Order
Field3Top
Field3Left
etc.

Can I use the information in the configuration table to add (or remove)
fields from the form at runtime, as the record pointer in in the main table
changes ?

Many thanks (in advance)

Michael Morton
 
You can CreateControl only in form design view. This means you cannot use
the Current event of the form to create controls.

Additionally, if you use Design View, it will prevent you from ever creating
an MDE from the database--something that is highly desirable in any sersious
application.

As you are doing, it is possible to use the Open event of the form to bind
it to a table/query, assign the ControlSource of the desired controls and
show them. I haven't tested if you can reassign the TabOrder, but I don't
see a need to do that anyway, since you can set the ControlSource as you
wish, and even disable the TabStop if needed.

If you need different controls in every record, there is an very high
probably that you have a non-normalized design, and you could solve the
problem better by creating a related table where each of your current
attributes becomes a record in the subform, such that each record can have
whatever related issue that apply to it instead of the monster form where
everything is being reorganised for every record.

If that is a foreign concept, this article describes the thinking behind a
totally generic data model:
http://www.databaseanswers.org/data_models/father_of_all_models/index.htm
 
Allen

Thanks for the infomation.

The database is pretty much normalised - the main data contains fields which
give the user administrators the ability to customise the data recorded for
different situations. There's 5 chars, 5 double, 10 combos boxes (with
another table to populate them), 3 dates etc., all of which can be 'named'
and shown on the form as required by the user group.

I don't think you can reorder the tab stops at run time - that would solve
all of my problems, to be honest, but I'm pretty sure that it's not
'do-able'.

Michael Morton
 
Please note that even if you do you design view to create and delete controls
programmatically, you form will in time become unusable. There is a lifetime
limit to the number of controls a form can have. I can't remember the exact
number, but it is about 750. What I mean by lifetime is that when you add a
control the count increases by 1, but deleting a control does not reduce the
count. Therefore, each time your user used the form, it would add to the
count. How long it would last would depend on the number of controls added
and how often the form is used.
 
Back
Top