【译文】如何用ADO代码实现窗体记录集的绑定-周芳
Access软件网QQ交流学习群(群号码198465573),欢迎您的加入!
首页 >技术文章> 综合其它


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

发表时间:2013/12/24 8:59:20 评论(1) 浏览(10254)  评论 | 加入收藏 | 复制
   
摘 要:如何用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群(群号:198465573)
 
 相关文章
【译文】如何:提示用户在窗体中编辑数据前对数据的有效性确认  【周芳  2013/11/5】
【译文】如何使用自定义条件删除记录  【周芳  2013/11/19】
【译文】如何允许用户为未绑定的组合框添加项目  【周芳  2013/11/26】
【译文】如何在子窗体或者子报表中调用程序  【周芳  2013/12/10】
【译文】为什么使用Access作为开发工具  【周芳(译)  2013/12/17】
常见问答
技术分类
相关资源
文章搜索
关于作者

周芳

文章分类

文章存档

友情链接