Stuart Sanders
stuart at pacific.net.hk
Fri Feb 21 07:10:00 CST 2003
You could try something like this:
Function CreateNewControl()
Dim frm As Form
Dim lngLine As Long
Dim strFormName As String
Dim ctl As Control
Dim strCtlName As String
strFormName = "Form1"
strCtlName = "cmdTest"
DoCmd.OpenForm strFormName, acDesign, , , , acHidden
Set frm = Forms(strFormName)
Set ctl = CreateControl(strFormName, acCommandButton, acDetail,
vbNullString, _
vbNullString, 1440, 720, 1440, 720)
With ctl
'set control properties
.Name = strCtlName
End With
lngLine = frm.Module.CreateEventProc("Click", ctl.Name)
With frm.Module
.InsertLines lngLine + 1, _
"' Purpose: insert new event procedure test"
.InsertLines lngLine + 2, _
"On Error Resume Next"
.InsertLines lngLine + 3, vbTab & _
"msgbox ""Hello World"""
End With
DoCmd.Save acForm, strFormName
DoCmd.Close acForm, strFormName
End Function
In order to test this, you will need a blank form called Form1. Then
simply run the code. It should open the form in design mode, add the
command button and on_click code, and then close the form again. You
can open the form and test the button.
In your real app, it should be fairly simple to modify this (assuming
you know where the control should go) and then iterate through your
forms using any criteria to distinguish the correct forms.
Stuart