While you can add a new control to an existing form at run-time, I can't think of how you could add a new control to the form you are already in (i.e., in which the code is currently executing). The form would first have to be toggled to design view, which closes the form, and while the code continues to run in the form's code module, the CreateControl method can't then recieve the form's name as an argument.
I've included code to add a control to an existing form when it is opened from another form. Perhaps you can modify it for your purposes.
- open the form you want to add the control to (Form2) in design view and hide it.
- add a new control using the CreateControl method.
- toggle the form to normal view.
Also, to position the new control you are working in twips (1" * 1440), not inches.
==Code============================================ =======
' Form1 code module
Private Sub Command1_Click()
' used to convert inches to twips
Const twipsPerInch As Long = 1440
' Open form you want to add control to
' in design view and hide it.
DoCmd.OpenForm "Form2", acDesign, , , , acHidden
' Add new control
With CreateControl( _
FormName:="Form2", _
ControlType:=acTextBox, _
Section:=acDetail, _
Left:=1 * twipsPerInch, _
Top:=1 * twipsPerInch, _
Height:=0.25 * twipsPerInch, _
Width:=1 * twipsPerInch)
' set control properties
.Name = "txtTextBox"
End With
' toggle form to Normal view
DoCmd.OpenForm "Form2"
End Sub
HTH,
Bob
|