Private Sub Command1_Click()
'首先引用Microsoft Activex Data Objects 2.x Library
Dim objcon As New ADODB.Connection
Dim objrs As New ADODB.Recordset
Dim strConn As String
strConn = "Provider=SQLOLEDB.1;User ID=sa;password=密码;Initial Catalog=数据库名称;Data Source=本机IP地址(192.168.1.136)"
objcon.Open strConn
'strConn = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;password=hNXQF222;Initial Catalog=database;Data Source=192.168.1.136"
'objcon.Open "Provider=sqloledb.1;user id=sa;password=hNXQF222;Initial Catalog=Northwind;Data Source="
objrs.Open "select * from Orders", objcon, 3, 1
'在部件中添加DataGrid控件
Set DataGrid1.DataSource = objrs
End Sub
VBA连接SQL数据库示例
本文介绍了一个使用VBA连接SQL Server数据库并展示查询结果的例子。通过建立ADO连接对象和记录集对象,实现了从指定数据库中检索订单数据,并利用DataGrid控件进行展示。
150

被折叠的 条评论
为什么被折叠?



