【问题标题】:Required Field's GotFocus Event Won't Set Subform's LinkChildFields Property必填字段的 GotFocus 事件不会设置子表单的 LinkChildFields 属性
【发布时间】:2015-08-20 00:32:26
【问题描述】:

我在 MS Access 2003 中创建的数据库出现问题。我创建了一个包含两个组合框(cboCategory 和 cboSubCategory)、一个文本框(txtDescription)和一个子表单(sbfExistingItems)的表单。 cboSubCategory 字段是唯一不需要的字段。我在 txtDescription 的 GotFocus 事件中添加了以下代码:

Private Sub txtDescription_GotFocus()

    Dim sql As String, child As String, master As String

    sql = "SELECT id, description, category, sub_category FROM tblItems"

    If IsNull(Me!cboCategory) Or Me!cboCategory = "" Then 
        ' leave recordsource unfiltered
    ElseIf IsNull(Me!cboSubCategory) Or Me!cboSubCategory = "" Then
        sql = sql & " WHERE [category] = '" & Me!cboCategory & "'"
        child = "category"
        master = "cboCategory"
    Else
        sql = sql & " WHERE [category] = '" & Me!cboCategory & "' AND [sub_category] = '" & Me!cboSubCategory & "'"
        child = "category;sub_category"
        master = "cboCategory;cboSubCategory"
    End If

    sql = sql & " ORDER BY [description];"

    Me!sbfExistingItems.Form.RecordSource = sql

    Me!sbfExistingItems.LinkChildFields = ""
    Me!sbfExistingItems.LinkMasterFields = ""
    Me!sbfExistingItems.LinkChildFields = child
    Me!sbfExistingItems.LinkMasterFields = master

End Sub

如果我在没有最后四行的情况下运行它,它可以正常工作(即,子窗体的 RecordSource 设置)。但是,一旦 txtDescription 获得焦点,在最后四行 with 运行它会导致运行时错误 3314(描述不能包含 Null 值),就好像我试图离开主窗体时所需的 txtDescription 字段为空。

为什么主窗体允许我编辑子窗体的 RecordSource 属性而不是它的 LinkChildFields/LinkMasterFields 属性?

【问题讨论】:

标签: ms-access vba ms-access-2003


【解决方案1】:

因为您在代码中设置了子表单的记录源,所以无需设置子/主链接。当表单打开以及记录发生更改时,您将需要使用代码设置子表单的记录源,但您不需要链接来执行此操作。

【讨论】:

  • 你是对的。我尝试这样做的原因是,当我第一次添加子表单时,我已经在 category 和 sub_category 上设置了子/主链接。删除链接不起作用,因为 Access 将它们重新设置为 id,这不是我想要的。我已经通过删除子表单并在没有子/主链接的情况下重新创建它来解决了这个问题。
猜你喜欢
  • 1970-01-01
  • 2019-04-28
  • 2011-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-01
  • 2018-01-29
  • 1970-01-01
相关资源
最近更新 更多