excel中VBA编程,如何连接SQL数据库中的视图查询表?将表格中搜索出来的数据直接复制到excel中。
SubRefresh()DimrsAsADODB.RecordsetDimSqlAsStringDimiAsIntegerDimstrSQLAsString,connAs...
Sub Refresh()
Dim rs As ADODB.Recordset
Dim Sql As String
Dim i As Integer
Dim strSQL As String, conn As ADODB.Connection
Sql = "Provider=sqloledb;""server=sec0000229\Techonology;""Database=secop;""Uid=sa;""Pwd=P@ssw0rd;"
strSQL = "select * from View_Rprintdata"
rs.Open strSQL, conn
Sheets("datasource").Select
Range("A6:P4500").Select
Selection.ClearContents
Range("G3").Select
'
' Call ConnectDB(Sql, rs, conn)
If Not rs.BOF Then rs.MoveFirst
i = 6
While Not rs.EOF
Cells(i, 4) = rs!OrderNum
Cells(i, 5) = rs!CompressorNum
Cells(i, 6) = rs!OrderDate
Cells(i, 7) = rs!Customer
Cells(i, 8) = rs!CompType
Cells(i, 9) = rs!SalesNum
Cells(i, 10) = rs!MethodName
Cells(i, 11) = rs!TestCondition
Cells(i, 12) = rs!TestStatus
Cells(i, 15) = rs!PanelName
Cells(i, 16) = rs!Refreigerant
rs.MoveNext
i = i + 1
Wend
rs.Close
以上代码测试提示对象边和和块变量没有设置。 展开
Dim rs As ADODB.Recordset
Dim Sql As String
Dim i As Integer
Dim strSQL As String, conn As ADODB.Connection
Sql = "Provider=sqloledb;""server=sec0000229\Techonology;""Database=secop;""Uid=sa;""Pwd=P@ssw0rd;"
strSQL = "select * from View_Rprintdata"
rs.Open strSQL, conn
Sheets("datasource").Select
Range("A6:P4500").Select
Selection.ClearContents
Range("G3").Select
'
' Call ConnectDB(Sql, rs, conn)
If Not rs.BOF Then rs.MoveFirst
i = 6
While Not rs.EOF
Cells(i, 4) = rs!OrderNum
Cells(i, 5) = rs!CompressorNum
Cells(i, 6) = rs!OrderDate
Cells(i, 7) = rs!Customer
Cells(i, 8) = rs!CompType
Cells(i, 9) = rs!SalesNum
Cells(i, 10) = rs!MethodName
Cells(i, 11) = rs!TestCondition
Cells(i, 12) = rs!TestStatus
Cells(i, 15) = rs!PanelName
Cells(i, 16) = rs!Refreigerant
rs.MoveNext
i = i + 1
Wend
rs.Close
以上代码测试提示对象边和和块变量没有设置。 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询