Hi I'm trying to create a form from a table in code. the table will be changing every few months and i want to create a control for this filed on the form. any ideas
Public Sub DynamicForm()
Dim frm As Form
Dim ctlLabel As Control, ctlText As Control
Dim intDataX As Integer, intDataY As Integer
Dim intLabelX As Integer, intLabelY As Integer
' Create new form and set its record source.
Set frm = CreateForm
frm.RecordSource = "Table2"
' Set positioning values for new controls.
intLabelX = 100
intLabelY = 100
intDataX = 1000
intDataY = 100
Dim rs As Recordset
Set rs = CurrentDb.OpenRecordset("Table1"
Dim fld As Field
For Each fld In rs.Fields
' Create unbound default-size text box in detail section.
Set ctlText = CreateControl(frm.Name, acTextBox, , "", fld.Name, intDataX, intDataY)
' Create child label control for text box.
Set ctlLabel = CreateControl(frm.Name, acLabel, , ctlText.Name, fld.Name, intLabelX, intLabelY)
intLabelY = intLabelY + 300
intDataY = intDataY + 300
Next
And a GOOD start. A bit more checking on the data type to set the control type might be nice. Placing the table name as an (input) argument would help generalize the process. But -as already and redundantly noted- a really good 'start'.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.