尝试使用基于子表单控件的DoCmd.OpenForm打开表单

时间:2016-02-05 17:49:29

标签: vba ms-access access-vba

我有一个带有未绑定控件的未绑定表单(Text0)。在Text0中输入RefNo。我想打开另一个表单(frmDisclosure),它有一个包含RefNo的子表单(frmRefNosList)。我打开frmDisclosure的代码是:     Private Sub Command8_Click()

Dim Refce As Variant

DoCmd.OpenForm "frmDisclosure"
Forms!frmDisclosure.FilterOn = False
Refce = "Forms!frmDisclosure!frmRefNosList.Form!RefNo"
DoCmd.OpenForm "frmDisclosure", acNormal, "", Refce & " = " & Me.Text0, , acNormal

End Sub

2 个答案:

答案 0 :(得分:1)

在我们讨论聊天之后,我嘲笑了一些希望看起来像你想要做的事情:

Link to Access file mock-up

其他可能会在稍后阅读此内容的人会做一些澄清:

  • 表单 Form1 上有一个名为 Text0 的文本框和一个按钮。
  • 按下按钮时,应该打开 Form2 子表单上。
  • Form2 然后应该打开某个记录,然后过滤掉 所有使用 Text0
  • 中提供的值的子表单上显示的记录

这是我的 Form1 模型的样子:

enter image description here

所以当我提供RefNo并点击按钮时......

enter image description here

...在该按钮的click事件上运行以下代码(带注释):

Private Sub cmdOpen_Click()

    ' check user has provided a RefNo in the textbox...
    If _
        Me.Text0 = "" Or _
        IsNull(Me.Text0) _
    Then

        ' ...if a RefNo has not been provided, give an error message
        MsgBox "Please provide a Ref No.", vbExclamation Or vbOKOnly, "Disclosure Search"

    Else

        ' ...if a RefNo has been provided, look in the table that lists an applicant's
        ' forms and return the ApplicantID (or equivalent field you're using) for the RefNo provided
        Dim varApplicantID As Variant
        varApplicantID = DLookup("ApplicantID", "tblRefNos", "RefNo=" & Me.Text0)

        ' ...check that an applicant record can be actually be found from the RefNo provided
        If _
            IsNull(varApplicantID) _
        Then

            '... if an applicant record is not found, give an error message
            MsgBox "Could not find an applicant with the RefNo provided.", vbExclamation Or vbOKOnly, "Disclosure Search"

        Else

            '... if an applicant record is found, open Form2 to that applicant's record
            DoCmd.OpenForm "Form2", , , "ApplicantID=" & varApplicantID

            '... and then filter the subform on Form2 by the RefNo provided
            Forms!Form2!frmApplicantForms_sub.Form.Filter = "RefNo=" & Me.Text0
            Forms!Form2!frmApplicantForms_sub.Form.FilterOn = True
            Forms!Form2!frmApplicantForms_sub.Form.Requery

        End If

    End If

End Sub

这会导致 Form2 打开从提供的RefNo派生的正确的申请人记录,并通过提供的RefNo过滤子表单:

enter image description here

你确切的项目布局可能与我嘲笑的不同,所以有些可能需要针对你的设置进行调整,但希望我在这里说明的原则很容易翻译。

祝你好运!

答案 1 :(得分:0)

这不是我想要的答案但是:我创建了一个查询,该查询产生与未绑定表单上的Text0对应的frmDisclosure的ID号。然后,我从未绑定的表单中使用DLookup检索该ID号,并以这种方式打开frmDisclosure。从未绑定的表格中做到这一点会更好。