You do not want to create a form programmatically at run
time. This would have all kinds of negative ramifications.
It is much better to pre create a form with the required
buttons and (their event procedures) in the form footer.
Add more invisible text boxes in the detail and labels in
the header than you will need for any table. Use a combo
box in the form's header section to selected the table for
the form's RecordSource.
You can then set the needed text box's and label's Visible
property, ControlSource or Caption and their Left and Width
properties. This is actually easier than creating the form
and all its controls on the fly.
If you name the text boxes and labels with a common prefix
and a sequential numeric suffix (eg txt0, txt1, txt2,... and
lbl0, lbl1, lbl2,...), the whole thing can be done in a
loop.
Here's some code to demonstrate the kind of logic involved:
Private Sub cboTables_AfterUpdate()
Dim db As Database
Dim tdf As TableDef
Dim fld As Field
Dim k As Long
Dim lngWidth As Long
Dim lngTotalWidth As Long
Set db = CurrentDb()
Set tdf = db.TableDefs(Me.cboTables)
Me.RecordSource = Me.cboTables
Me.cboTables.SetFocus
For k = 0 To Me.Section(0).Controls.Count - 1
Me("txt" & k).Visible = False
Me("lbl" & k).Visible = False
Me("txt" & k).Left = 0
Me("lbl" & k).Left = 0
Me("txt" & k).Width = 0
Me("lbl" & k).Width = 0
Next k
k = 0
For Each fld In tdf.Fields
Me("txt" & k).Visible = True
Me("lbl" & k).Visible = True
Me("txt" & k).ControlSource = fld.Name
Me("lbl" & k).Caption = fld.Name
lngWidth = fld.Properties("ColumnWidth")
If lngWidth > 4000 Then lngWidth = 4000
If lngWidth = -1 Then lngWidth = 1000
Me.InsideWidth = lngTotalWidth + lngWidth
Me("txt" & k).Left = lngTotalWidth
Me("lbl" & k).Left = lngTotalWidth
Me("txt" & k).Width = lngWidth
Me("lbl" & k).Width = lngWidth
lngTotalWidth = lngTotalWidth + lngWidth
k = k + 1
Next fld
Set tdf = Nothing
Set db = Nothing
End Sub
The combo box, cboTables, in the form's header that's used
to select the table has the RowSource query:
SELECT Name
FROM MSysObjects
WHERE Type = 1 And Left(Name ,4) <> "MSys"
ORDER BY Name
--
Marsh
MVP [MS Access]
[email protected] wrote:
I want to make this very generic - so that I don't have create a form
for
each table (and maintain them later)... any suggestions how to
programatically build this form that that all the data elements are
displayed
as you suggest?
[email protected] wrote:
I am linking tables into my mdb and need to programatically open
them
for my
user for review of the data...
I was planning on using the DoCmd.OpenTable "TableName"... method to
perform
this... the problem is, that how do I determine when the table is
closed?
(After closing the table, I need acknowledgement from the user about
the
state of the data)...
Don't display a table's sheet to user's. Create a form in
continuous view that looks like a datasheet. Then you can
use the form header/footer section's for command button's
for users to indicate what they intend for you to do. If
you don't display the form's X (close) button in the title
bar, then you can provide your own confirm button and
perform whatever actions you want.