【发布时间】:2019-01-24 00:59:43
【问题描述】:
我正在尝试链接我在类模块中创建的表单上的控件,但无法使其正常工作。
'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
我在一个单独的表单中创建对象并在点击事件中调用 showForm
Private Sub Command0_Click()
Dim tstForm as CustForm
set tstForm= New CustForm
tstForm.showForm
End Sub
但是当我点击在 CustForm 中创建的按钮时什么也没有发生 我尝试在 showForm 和 docmd.runcommand acCmdFormView 之后使用临时命令按钮
set btnTest = Forms(formName).Controls(btnName)
假设当表单进入表单视图时,命令按钮的实例可能会发生变化。再一次没有快乐。
但是,如果我将其添加到 CustForm 类中
Public Function init(lclBtn as CommandButton)
set btnTest = lclBtn
btnTest.OnClick = "[Event procedure]"
End Function
然后我删除 OnClick 并将此代码添加到表单的模块中
Option Explicit
Dim tester as CustForm
Private Sub Form_Open(Cancel as Integer)
Set tester = new CustForm
tester.init Me.Command0
End Sub
然后当我单击按钮时它会触发 MsgBox。 但是我最终需要能够构建一个表单工厂类,它允许我动态地为类对象构建表单并处理对象类中的事件。我宁愿不为每个类制作一堆专门构建的表单,并让表单实例化该类。我想反过来做。类构建表单。
这个可以吗?
【问题讨论】: