设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 2296|回复: 9
打印 上一主题 下一主题

[ADO/DAO] 无源ADO如何显视表的全部内容[已解决]

[复制链接]
1#
发表于 2011-2-5 18:47:45 | 显示全部楼层
下面的示例演示如何打开您自己的 ADO 连接 Microsoft SQL Server 数据库以及将窗体绑定到它:
打开示例数据库 Northwind.mdb。
在设计视图中打开客户窗体。
清除窗体,以取消绑定窗体的 记录源 属性。
Set the OnOpen property of the form to the following event procedure:
Private Sub Form_Open(Cancel As Integer)
   Dim cn As ADODB.Connection
   Dim rs As ADODB.Recordset
         
   'Create a new ADO Connection object
   Set cn = New ADODB.Connection

   'Use the Access 10 and SQL Server OLEDB providers to
   'open the Connection
   'You will need to replace MySQLServer with the name
   'of a valid SQL Server
   With cn
      .Provider = "Microsoft.Access.OLEDB.10.0"
      .Properties("Data Provider").Value = "SQLOLEDB"
      .Properties("Data Source").Value = "MySQLServer"
      .Properties("User ID").Value = "sa"
      .Properties("Password").Value = ""
      .Properties("Initial Catalog").Value = "NorthwindCS"
      .Open
   End With

   '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

将下面的代码添加到窗体的卸载事件:
Private Sub Form_Unload(Cancel As Integer)
   'Close the ADO connection we opened
   Dim cn As ADODB.Connection
   Set cn = Me.Recordset.ActiveConnection
   cn.Close
   Set cn = Nothing
End Sub

2#
发表于 2011-2-5 19:04:26 | 显示全部楼层
详情请看微软的文章
http://support.microsoft.com/kb/281998/zh-cn
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-6-4 20:55 , Processed in 0.094010 second(s), 25 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表