如何最有效地调用用作源的形式宣布为公共子程序对象从主窗体子窗体控件?窗体、子程序、有效地、控件

2023-09-09 21:21:54 作者:对你的偏爱↗

我有一个表格MyForm1与模块具有方法。

I have a form MyForm1 with a module having the method.

Public Sub CreateSQL(ProductID as variant) 

    Me.Recordsource = " SELECT * FROM ProductOrders " _ 
                      " WHERE ProductID = " & Nz(ProductID ,0)    

    Me.Requery

End Sub

我用这个作为一个子窗体,以一个名为形式MyMainForm。

I use this as a subform to a form named MyMainForm.

当我更改MyMainForm控制成功执行以下线的值:

When I change the value of a control on MyMainForm it successfully executes the following line:

Me.Subform1.Form.CreateSQL ProductID:=Me.cboProductID 

我的问题:

为什么成员没有后,我键入显示的智能感知列表中列出 Me.Subform1.Form。

有没有让他们上市的一种方式?

Is there a way of getting them to be listed?

有没有办法,可以让我访问子窗体控件源对象属性(即Me.Subform1.form)?

Is there a property that will let me access the "Form_MyForm1" class of the form referenced in the subform control "Source object property" (ie the Me.Subform1.form ) ?

这就像我需要能够这样写:

It's like I need to be able to write:

Me.Subform1.Form_MyForm1.CreateSQL   ProductID:=Me.cboProductID

有没有这样的属性已经存在?如果是的话我怎么访问它?它是在属性集合?

Does such a property already exist? If so how do I access it? Is it in the properties collection?

PS:如果您需要了解更多信息,请参阅贴在相当长的堆栈溢出问题here

PS: If you need more information please see the same questions posted in a long Stack overflow question here

哈维

推荐答案

而不是MainForm的调用方法Me.Subform1.Form.CreateSQL

Instead to the mainform calling the method Me.Subform1.Form.CreateSQL

您应该创建一个指向主窗体和响应事件子窗体对象变量,例如:

You should create an object variable in the subform that points to the main form and responds to events eg:

Dim WithEvents cvMyParentForm As Form

Property Set MyParentForm(MyParentForm As Form)

    Set cvMyParentForm = MyParentForm

End Property
Property Get MyParentForm() As Form

    Set MyParentForm = cvMyParentForm

End Property

在主窗体打开使用Form_Open事件初始化的子窗体

When the main form opens use the Form_Open event to "initialise" the subforms

Private Sub Form_Open(Cancel As Integer)

    If Me.Subform1.Form.MyParentForm Is Nothing Then
        Set Me.Subform1.Form.MyParentForm = Me
    End If

End Sub

那么你可以得到子窗体到由MainForm中提出的FORM事件作出反应。

then you can get the subform to respond to the FORM events that are raised by the mainform.

如果你需要有你在主窗体声明任何事件子窗体响应,你需要chnaeg上述code使用Form_MyMainFormname类型

If you need to have the subform respond to any events that you declare in the main form you will need to chnaeg the above code to use the Form_MyMainFormname type

Dim WithEvents cvMyParentForm As Form_MyMainFormName

Property Set MyParentForm(MyParentForm As Form_MyMainFormName)

    Set cvMyParentForm = MyParentForm

End Property
Property Get MyParentForm() As Form_MyMainFormName

    Set MyParentForm = cvMyParentForm

End Property

Private Sub cvMyParentForm_Current()

    'MsgBox "Sub form current event - does syncing"

    Me.Form.Recordset.FindFirst "ID = " & Nz(cvMyParentForm.ID, 0)
    If Me.Form.Recordset.NoMatch Then
        MsgBox "Humph"
    Else

    End If

End Sub

Private Sub cvMyParentForm_MyEvent()

    MsgBox "A user define event 'MyEvent' was fired on the  main form"

End Sub