I am trying to link controls on a form that I have created in a Class Module and am having trouble getting it to work properly.
'Class Name is CustForm
Option Explicit
Private WithEvents btnTest as CommandButton
Public Function showForm()
Dim tempForm as Form
Dim formName as String
Set tempForm = CreateForm
formName = tempForm.Name
Set btnTest = CreateControl(formName,
acCommandButton,acDetail,,,300,300,1000,500)
Dim btnName As String
btnName = btnTest.Name
Docmd.RunCommand acCmdFormView
End Function
Private Sub btnTest_Click()
MsgBox "Test"
End Sub
In a separate form I create the object and call the showForm in a click event
Private Sub Command0_Click()
Dim tstForm as CustForm
set tstForm= New CustForm
tstForm.showForm
End Sub
But nothing happens when I click the button created in the CustForm I have tried using a temporary CommandButton in the showForm and after the docmd.runcommand acCmdFormView
set btnTest = Forms(formName).Controls(btnName)
under the assumption that maybe the instance of the command button changes when the form goes to Form View. Again no joy.
However if I add this to the CustForm Class
Public Function init(lclBtn as CommandButton)
set btnTest = lclBtn
btnTest.OnClick = "[Event procedure]"
End Function
And then I delete the OnClick and add this code to the form's module
Option Explicit
Dim tester as CustForm
Private Sub Form_Open(Cancel as Integer)
Set tester = new CustForm
tester.init Me.Command0
End Sub
Then it fires the MsgBox when I click the button. But I need to be able to eventually build a form factory class that allows me to build forms for class objects dynamically and handle the events in the object class. I would rather not make a bunch of purpose built forms for each class and have the form instantiate the class. I want to do it the other way around. Class builds the form.
Can this be done?