来自:微软 翻译:周芳
【译文】如何用ADO代码实现窗体记录集的绑定
将Microsoft Access 窗体绑定到一个记录集,你必须设置窗体的记录集属性为ADO记录集对象。这个窗体绑定到一个ADO记录集,更新时必须满足两个基本要求。基本要求是:
. 必须通过ADO更新
. 记录集必须包含一个或多个字段的惟一索引,比如一个表的主键。
VBA代码 :
Private Sub Form_Open(Cancel As Integer)
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
'使用ADO连接
Set cn = CurrentProject.AccessConnection
'为ADO创建一个类实例
'设置它的属性
Set rs = New ADODB.Recordset
With rs
Set .ActiveConnection = cn
.Source = "Select * FROM Customers"
.LockType = adLockOptimistic
.CursorType = adOpenKeyset
.Open
End With
'用ADO给窗体的属性赋值
Set Me.Recordset = rs
Set rs = Nothing
Set cn = Nothing
End Sub
【原文】How to: Bind a Form to an ADO Recordset
To bind a Microsoft Access form to a recordset, you must set the form's Recordset property to an open ADO Recordset object. A form must meet two general requirements for the form to be updatable when it is bound to an ADO recordset. The general requirements are:
•The underlying ADO recordset must be updatable via ADO.
•The recordset must contain one or more fields that are uniquely indexed, such as a table's primary key.
VBA:
Private Sub Form_Open(Cancel As Integer)
Dim cn As ADODB.Connection
Dim rs As ADODB.Recordset
'Use the ADO connection that Access uses
Set cn = CurrentProject.AccessConnection
'Create an instance of the ADO Recordset class,
'and set its properties
Set rs = New ADODB.Recordset
With rs
Set .ActiveConnection = cn
.Source = "Select * FROM Customers"
.LockType = adLockOptimistic
.CursorType = adOpenKeyset
.Open
End With
'Set the form's Recordset property to the ADO recordset
Set Me.Recordset = rs
Set rs = Nothing
Set cn = Nothing
End Sub