Sub 按钮1_Click()
Dim i As Integer, j As Integer, sht As Worksheet ‘i,j为整数变量;sht 为excel工作表对象变量,指向某一工作表
‘Dim cn As New ADODB.Connection ‘定义数据链接对象 ,保存连接数据库信息;请先添加ADO引用
‘Dim rs As New ADODB.Recordset ‘定义记录集对象,保存数据表
‘工具 —〉引用 —〉Microsoft ActiveX data objects ….
‘下面两句就不需要增加引用ADO
Set cn = CreateObject(“Adodb.Connection”)
Set rs = CreateObject(“Adodb.Recordset”)
Dim strCn As String, strSQL As String ‘字符串变量
Dim strCond As String
strCn = “Provider=sqloledb;Server=R9HDET7;Database=dbname;Uid=username;Pwd=password” ‘定义数据库链接字符串