Vba 从同名ado记录集记录中的字段填充ms access表单中的字段
我在Access中有一个表单,我试图在当前事件期间使用ADO记录集中的字段填充该表单。我使用Sql Server作为数据库,并使用记录集尝试使用记录集中的内容填充表单上的相应字段。现在它是这样工作的:Vba 从同名ado记录集记录中的字段填充ms access表单中的字段,vba,ms-access,ado,recordset,Vba,Ms Access,Ado,Recordset,我在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 = Curr
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
但我希望当前子例程的工作方式如下:
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
由于代码的存在,我会发现一个错误“记录集不可更新”
谢谢你的帮助 这是因为您没有绑定到记录集。最好是存储查询并将其作为记录源附加到表单,这样它就可以将其绑定到表单。然后,在设计模式下设置每个字段的记录源,不需要任何代码,它将根据您对SQL Server的权限进行更新。我没有将其作为记录源附加,因为我有一个按钮,他们必须按下以保存所做的更改。其目的是使用一个存储过程来更新表,而不是执行一些其他任务。您仍然可以像我说的那样在表上设置触发器以进行插入或更新,这样可以调用一个存储过程来完成额外的工作。进入聊天功能,我们可以在其中进行交谈,这样我们就不会阻塞它带着评论。只要点击顶部的聊天按钮,你就会看到你有一个邀请。