Add control to a UserForm

Z

Zurn

Probs:

I want to make a dynamic form. Pushing the button + has to make new
textboxes on my form.

Searching the net tels me -adding control to a user form's Controls
collection is possible at design time-,I should use the Designer property
which returns me the UserForm object >>> Set Control =
object.add(ProgID,Name,Visible)

http://support.microsoft.com/?kbid=204330
This link is based on Word examples. Using adjusted code on my form does not
work:
In Module1
Sub test()
Set myCheckBox = Form!Form_test.Controls.Add("Forms.CheckBox.1")
With myCheckBox
.Name = "Check1"
...
End With
End Sub

In Form_test module
Private Sub ButtonADD_Click()
Set myCheckBox = ActiveForm.Designer.Controls.Add("Forms.CheckBox.1")
With myCheckBox
.Name = "Check1"
...
End With
End Sub

Anyone ideas?
 
G

George Nicholson

Access Forms & controls have a different object model than those used by
*anything* else (Word, Excel, VB, etc.). The object models are very similar
in most respects, but the differences are significant and this is one of
them. (UserForm and Forms.CheckBox won't be recognized by Access but would
work in Word or Excel)

To add an Access control to an Access form via code you have to use the
CreateControl method (see Access VBA Help). The form *must* be in design
view. This presents a problem if you want to add a control to the "Active"
form, since you would have to close it, reopen it, make changes, etc., which
is problematic while you are running code on the form itself (I provide no
solution here, just an outline of the problem. Maybe call it from the menu
instead?)

It might be *a lot* easier to simply add a bunch of extra (hidden) controls
to the form and unhide them "dynamically" rather than add them.

HTH & Good Luck,
 
Top