西安飞宜宾:Excel VBA ADO 问题 - VB / VBA

来源:百度文库 编辑:九乡新闻网 时间:2024/05/02 13:48:15
Private Sub Button3_Click()
  Dim conn As New ADODB.Connection
  Dim res As New ADODB.Recordset
  Dim str As String
  conn.ConnectionString = "Server=10.102.120.197; Driver=SQL Server; " & _
  "Database=DBCentre;"
  On Error Resume Next
  conn.Open
  On Error GoTo 0
  'If conn.State = adStateOpen Then
  ' MsgBox "到DBCentre的连接成功!", vbInformation, "连接数据库"
  'Else
  ' MsgBox "连接失败!", vbInformation, "连接数据库"
  'End If
  Dim i, j As Integer
  j = 1
  str = "select * from 1"

  res.Open str, conn
  Worksheets("sheet2").unproted
  Worksheets("sheet2").Cells.ClearContents

  While Not res.EOF
  For i = 1 To res.Fields.Count - 1
  Sheet2.Cells(j, i).Rows.Value = res.Fields(i).Value
  Next i
  j = j + 1
  res.MoveNext
  Wend
  MsgBox "读取完毕", vbInformation, "文件读取情况"
  res.Clone
  conn.Close
  Worksheet("sheet1").Activate
End Sub

这段代码是我最近才写的.但是一直提示"子过程或函数未定义"请问代码有什么问题吗?