从同一个ado记录集记录中的字段填充ms访问表单中的字段

时间:2013-07-15 23:25:25

标签: vba ms-access ado recordset

我在Access中有一个表单,我尝试使用ADO Recordset中的字段在其Current事件中填充。我正在使用Sql Server作为数据库,并使用记录集尝试使用记录集中的内容填充到表单上的相应字段。现在它的工作原理如下:

Private Sub Form_Open(Cancel As Integer)
On Error GoTo Err_FormOpen

    Set rst As new ADODB.Recordset
    With rst
        .ActiveConnection = CurrentProject.AccessConnection
        .CursorType = adOpenKeyset
        .LockType = adLockOptimistic
        .Source = "SELECT * FROM [Outage Summary] ORDER BY OutageSummaryID"
        .Open
    End With
    Set Me.Recordset = rst

End Sub


Private Sub Form_Current()
    OutageSummaryID.Value = Me.Recordset!OutageSummaryID
    Dispatcher.Value = Me.Recordset!Dispatcher
    StartDateTime.Value = Me.Recordset!StartDateTime
    Location.Value = Me.Recordset!Location
    CityRelated.Value = Me.Recordset!CityRelated
    Scheduled.Value = Me.Recordset!Scheduled
    CustomerEquip.Value = Me.Recordset!CustomerEquip
    DispatchBusHrs.Value = Me.Recordset!DispatchBusHrs
    TotalCount.Value = Me.Recordset!TotalCount
    Substation.Value = Me.Recordset!Substation
    CompletionDateTime.Value = Me.Recordset!CompletionDateTime
    CustCallDateTime.Value = Me.Recordset!CustCallDateTime
    FirstRespDateTime.Value = Me.Recordset!FirstRespDateTime
    Feeder.Value = Me.Recordset!Feeder
    SwitchingSchedule.Value = Me.Recordset!SwitchingSchedule
    Cause.Value = Me.Recordset!Cause
    ActionTaken.Value = Me.Recordset!ActionTaken
    Me.ME.Value = Me.Recordset!ME
    MI.Value = Me.Recordset!MI
End Sub

但我希望当前的子程序能够像这样工作:

Dim fld As ADODB.Field
Dim nam As String
For Each fld In Me.Recordset.Fields
    Debug.Print fld
    nam = fld.Name
    Me.Controls(nam).Value = fld.Value
Next

使用代码,我收到一个错误“记录集不可更新” 谢谢你的帮助!

1 个答案:

答案 0 :(得分:0)

这是因为您没有绑定到记录集。如果您存储查询并将其作为记录源附加到表单会更好,这样它就会将它绑定到表单。然后在设计模式下设置每个字段的记录源,不需要代码,它将根据您对SQL Server的权限进行更新。