必填字段的 GotFocus 事件不会设置子表单的 LinkChildFields 属性
Required Field's GotFocus Event Won't Set Subform's LinkChildFields Property
我在 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 得到设置)。但是 运行 将它 与 最后四行结果 运行-time 错误 3314(描述不能包含 Null 值)一旦 txtDescription 获得焦点,就好像我试图在所需的 txtDescription 字段为空时离开主窗体。
为什么主窗体允许我编辑子窗体的 RecordSource 属性 而不是它的 LinkChildFields/LinkMasterFields 属性?
因为您在代码中设置了子表单的记录源,所以不需要设置 child/master 链接。当表单打开时以及记录更改时,您将需要使用代码来设置子表单的记录源,但您不需要链接来执行此操作。
我在 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 得到设置)。但是 运行 将它 与 最后四行结果 运行-time 错误 3314(描述不能包含 Null 值)一旦 txtDescription 获得焦点,就好像我试图在所需的 txtDescription 字段为空时离开主窗体。
为什么主窗体允许我编辑子窗体的 RecordSource 属性 而不是它的 LinkChildFields/LinkMasterFields 属性?
因为您在代码中设置了子表单的记录源,所以不需要设置 child/master 链接。当表单打开时以及记录更改时,您将需要使用代码来设置子表单的记录源,但您不需要链接来执行此操作。