Access开发培训
网站公告
·Access专家课堂QQ群号:151711184    ·Access快速开发平台下载地址及教程    ·欢迎加入Access专家课堂微信群!    ·如何快速搜索本站文章|示例|资料    
您的位置: 首页 > 技术文章 > 综合其它

【译文】如何用ADO代码实现窗体记录集的绑定

时 间:2013-12-24 08:59:20
作 者:周芳   ID:24526  城市:上海
摘 要:如何用ADO代码实现窗体记录集的绑定
正 文:

来自:微软    翻译:周芳

【译文】如何用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



Access软件网官方交流QQ群 (群号:483923997)       Access源码网店

常见问答:

技术分类:

相关资源:

专栏作家

关于我们 | 服务条款 | 在线投稿 | 友情链接 | 网站统计 | 网站帮助