SubForm包含来自主窗体的参数

时间:2012-10-22 16:14:20

标签: vba ms-access ms-access-2010

我有一个包含子表单的表单,该表单由ID链接。问题是子窗体有一个组合框,需要来自行源的主窗体的参数。

表单是关于销售电话的,他们需要能够根据需要链接许多提案和合同。提案和合同列表的组合框需要与此销售电话的工厂或客户有关。

我知道我可以做一个Forms!FormName!ControlName来获得工厂列表,但这是否意味着我有设计问题?

或者当用户点击主窗体中组合框中的元素时,我应该填写一个列表吗?但后来我必须处理保存并删除自己。

谢谢

1 个答案:

答案 0 :(得分:1)

如果子表单要求主表单正常运行,则可以检查父表单。例如:

Private Sub Form_Open(Cancel As Integer)
Dim strParent As String
Dim strSubname As String

    GetParent Me, strParent, strSubname

    If strParent = "None" Then
        MsgBox "This is not a stand-alone form.", , "Form Open Error"
        Cancel = True
    End If
End Sub

Function GetParent(frm, ByRef strParent, ByRef strSubname)
Dim ctl

On Error Resume Next
    strParent = frm.Parent.Name
    If Err.Number = 2452 Then
        Err.Clear
        strParent = "None"
    Else
        For Each ctl In frm.Parent.Controls
            If ctl.ControlType = acSubform Then
                If ctl.SourceObject = frm.Name Then
                    strSubname = ctl.Name
                End If
            End If
        Next
    End If

End Function