Access 子表单中的字段名称填充了 vba 记录集

Posted

技术标签:

【中文标题】Access 子表单中的字段名称填充了 vba 记录集【英文标题】:Field names in Access subform populated with vba recordset 【发布时间】:2013-11-26 16:10:54 【问题描述】:

似乎最简单的事情通常是最难以捉摸的。

在 Access 2007 中,我在表单 (frmInventorySheet) 中有一个子表单 (frmfacilityInventory)。在主窗体中进行一些选择后,用户单击一个按钮,我在 vba 中创建一个空白记录集,并在后台运行一些查询并将新记录添加到该记录集中。完成后,新记录集将显示在子表单中 - 或者我希望它显示。

我的字段都显示#ERROR。

新的 vba 记录集包含数据,我什至在文本框中显示完整记录以确保其他一切正常。子窗体中返回的记录数是正确的,但仍然显示#ERROR。

查看我的子表单,在 vba 代码完成构建新记录集并将子表单的源设置为新记录集之前,它没有设置“记录源”。该子表单的第一个字段为“设施”。此字段(在表单级别)的控制源也设置为“设施”。我的记录集有一个“设施”列。那么为什么我会在 Facility 列中看到 #ERROR?

    'Create blank recordset
Dim rsPMList As ADODB.Recordset
Set rsPMList = New ADODB.Recordset
Dim Facility, SQLstr As String
Facility = txtcboecho

'Create fields in blank recordset
With rsPMList.Fields
    .Append "PMID", adNumeric, 4, adFldKeyColumn
    .Append "Facility", adChar, 7, adFldUpdatable
    .Append "Device", adChar, 4, adFldUpdatable
    .Append "BarcodeID", adChar, 7, adFldUpdatable
    .Append "Name", adVarChar, 50, adFldUpdatable
    .Append "Address", adChar, 15, adFldUpdatable
    .Append "Location", adVarChar, 75, adFldUpdatable
End With

'Open blank recordset
rsPMList.Open

'Create query for "Building Controllers"
Dim rsBCList As ADODB.Recordset
Set rsBCList = New ADODB.Recordset

SQLstr = "SELECT tblFacility.FacCode AS Facility, 'BC' AS Device, "
SQLstr = SQLstr & "tblBC.BCName AS Name, tblBC.IPAddress AS Address, "
SQLstr = SQLstr & "FROM tblFacility INNER JOIN tblBC "
SQLstr = SQLstr & "ON tblFacility.FacilityID = tblBC.Facility "
SQLstr = SQLstr & "WHERE tblFacility.FacCode = '" & Facility & "' "
SQLstr = SQLstr & "AND tblBC.ParentBC Is Null"
rsBCList.Open SQLstr, CurrentProject.Connection, adOpenKeyset, adLockOptimistic

'Move records from query to blank recordset
Do While rsBCList.EOF <> True
    rsPMList.AddNew
        rsPMList.Fields("Facility") = rsBCList.Fields("Facility")
        rsPMList.Fields("Device") = rsBCList.Fields("Device")
        If IsNull(rsBCList.Fields("BarcodeID")) Then
            rsPMList.Fields("BarcodeID") = ""
        Else
            rsPMList.Fields("BarcodeID") = rsBCList.Fields("BarcodeID")
        End If
        rsPMList.Fields("Name") = rsBCList.Fields("Name")
        rsPMList.Fields("Address") = rsBCList.Fields("Address")
        rsPMList.Fields("Location") = rsBCList.Fields("Location")
    rsPMList.Update
rsBCList.MoveNext
Loop

'These lines shows that query data was successfully moved
'to the blank recordset.  Two text fields display two fields.
rsPMList.MoveFirst
txtShow1 = rsPMList.Fields("BarcodeID")
rsPMList.MoveNext
txtshow2 = rsPMList.Fields("BarcodeID")
rsBCList.Close
Set rsBCList = Nothing

'Set the record source of the subform to the newly created recordset
Set Me.frmFacilityInventory.Form.Recordset = rsPMList

rsPMList.Close
Set rsPMList = Nothing

【问题讨论】:

【参考方案1】:

Open 之前设置记录集的CursorLocationLockType 属性。

此记录集的简化版本适用于我的 Access 2007 表单。

With rsPMList
    .Fields.Append "PMID", adInteger, , adFldKeyColumn ' changed from adNumeric, 4
    .Fields.Append "Facility", adChar, 7, adFldUpdatable
    .Fields.Append "Device", adChar, 4, adFldUpdatable
    .CursorLocation = adUseClient
    .LockType = adLockPessimistic
    .Open
End With

【讨论】:

谢谢 HansUp。现在像魅力一样奔跑。您促使我更深入地研究打开记录集的属性,而不是随心所欲地使用默认值并摸不着头脑。谢谢!

以上是关于Access 子表单中的字段名称填充了 vba 记录集的主要内容,如果未能解决你的问题,请参考以下文章

在 VBA Access 中动态填充其他子表单的子表单

Access VBA:更改子表单的记录源

MS Access 保留子表单中的 ID 字段,填充父表单中的 ID

Access VBA:关闭表单会导致表中的重复条目

使用 Access VBA 使用循环填充 Word 表单

Access VBA:在连续子窗体中编辑 RecordSet