0

我在 Access 中有一个表单,我试图在当前事件期间使用 ADO 记录集中的字段填充它。我正在为数据库使用 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

就目前的代码而言,我收到一个错误“记录集不可更新”感谢您的帮助!

4

1 回答 1

0

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

于 2013-07-15T23:30:05.440 回答