Access子窗体中的字段名称填充了vba记录集

时间:2013-11-26 16:10:54

标签: ms-access ms-access-2007 access-vba

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

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

我的字段都显示#ERROR。

新的vba记录集有数据,我甚至在文本框中显示完整记录,以确保其他一切正常。子表单中返回的记录数是正确的,但仍显示#ERROR。

查看我的子表单,它没有设置“Record Source”,直到vba代码完成构建新记录集,然后将子表单的源设置为新记录集。子表单的第一个字段为“Facility”。此字段的控制源(在表单级别)也设置为“Facility”。我的记录集有一个“Facility”列。那么我为什么要在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 个答案:

答案 0 :(得分:2)

CursorLocation之前设置记录集的LockTypeOpen属性。

您的记录集的简化版本与我的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