MS Access VBA - 需要帮助从另一个表单引用多实例表单上的控件

问题描述

我有多个表单实例。我想单击 Form1(多实例)上的按钮以打开 Form2(单实例)。然后,我将单击 Form2 上的一个按钮,该按钮应该运行一些以重新查询 Form1 上的控件结束的代码。我在尝试引用该表单上的控件时遇到问题。根据我的研究,这可以通过将 for reference 从 Form1 传递给 Form2 来实现,以便在代码运行时,Form2 知道要重新查询哪个控件。

如果对这种情况有更好理解的好心人可以详细说明下面的线程,我认为这正是我所需要的:

Access 2007 / VBA - Multiple Instances of Form,Update controls on specific instance from Module

解决方法

你可以使用窗口的句柄:

在 Form1 中:

Private Sub ButtonForm2_Click()

    ' Pass the handle of the current instance of Form1 to Form2.
    DoCmd.OpenForm "Form2",Me.Hwnd

End Sub

在 Form2 中:

Private Sub ButtonForm1_Click()

    Dim Form1   As Form
    Dim Handle  As Long
    
    Handle = Nz(Me.OpenArgs,0)
    
    ' Find the calling instance of Form1.
    For Each Form1 In Forms
        If Form1.Hwnd = Handle Then
            Exit For
        End If
    Next
    
    If Not Form1 Is Nothing Then
        ' Do something.
        Form1!Textbox1.Value = Now
    End If
    
    DoCmd.Close acForm,Me.Name
    
End Sub