2017-09-25 146 views
-2
Sub aa() 

Dim conn As ADODB.Connection 

    Dim rs As ADODB.Recordset 
    Dim sConnString As String 

    ' Create the connection string. 
    sConnString = "Provider=SQLOLEDB;Data Source=INSTANCE\SQLEXPRESS;" & _ 
        "Initial Catalog=Raja;" & _ 
        "Integrated Security=SSPI;" 

    ' Create the Connection and Recordset objects. 
    Set conn = New ADODB.Connection 
    Set rs = New ADODB.Recordset 

    ' Open the connection and execute. 
    conn.Open sConnString 
    Set rs = conn.Execute("SELECT * FROM raja.dbo.saran") 

    ' Check we have data. 
    If Not rs.EOF Then 
     ' Transfer result. 
     Sheets(1).Range("A1").CopyFromRecordset rs 
    ' Close the recordset 
     rs.Close 
    Else 
     MsgBox "Error: No records returned.", vbCritical 
    End If 

    ' Clean up 
    If CBool(conn.State And adStateOpen) Then conn.Close 
    Set conn = Nothing 
    Set rs = Nothing 
End Sub 

請找到上面的代碼,但上面的代碼不工作,請問如何解決這個問題。如何使用vba將數據從SQL Server提取到Excel?

+0

如果任何交替代碼請提供否則,如果任何參考文件或參考視頻麻煩分享。 – Saran

+3

你有什麼錯誤,並在哪一行? – braX

+2

VB.NET≠VBScript≠VBA。請不要標記垃圾郵件只是因爲他們有前兩個字母的共同點。這只是VBA。 –

回答

0

有很多方法可以做到這一點!

Sub ADOExcelSQLServer() 
    ' Carl SQL Server Connection 
    ' 
    ' FOR THIS CODE TO WORK 
    ' In VBE you need to go Tools References and check Microsoft Active X Data Objects 2.x library 
    ' 

    Dim Cn As ADODB.Connection 
    Dim Server_Name As String 
    Dim Database_Name As String 
    Dim User_ID As String 
    Dim Password As String 
    Dim SQLStr As String 
    Dim rs As ADODB.Recordset 
    Set rs = New ADODB.Recordset 

    Server_Name = "NAME" ' Enter your server name here 
    Database_Name = "AdventureWorksLT2012" ' Enter your database name here 
    User_ID = "" ' enter your user ID here 
    Password = "" ' Enter your password here 
    SQLStr = "SELECT * FROM [SalesLT].[Customer]" ' Enter your SQL here 

    Set Cn = New ADODB.Connection 
    Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _ 
    ";Uid=" & User_ID & ";Pwd=" & Password & ";" 

    rs.Open SQLStr, Cn, adOpenStatic 
    ' Dump to spreadsheet 
    With Worksheets("sheet1").Range("a1:z500") ' Enter your sheet name and range here 
     .ClearContents 
     .CopyFromRecordset rs 
    End With 
    '   Tidy up 
    rs.Close 
    Set rs = Nothing 
    Cn.Close 
    Set Cn = Nothing 
End Sub 

OR。 。 。

Sub ADOExcelSQLServer() 

    Dim Cn As ADODB.Connection 
    Dim Server_Name As String 
    Dim Database_Name As String 
    Dim User_ID As String 
    Dim Password As String 
    Dim SQLStr As String 
    Dim rs As ADODB.Recordset 
    Set rs = New ADODB.Recordset 

    Server_Name = "Server_Name" ' Enter your server name here 
    Database_Name = "Northwind" ' Enter your database name here 
    User_ID = "" ' enter your user ID here 
    Password = "" ' Enter your password here 
    SQLStr = "SELECT * FROM Orders" ' Enter your SQL here 

    Set Cn = New ADODB.Connection 
    Cn.Open "Driver={SQL Server};Server=" & Server_Name & ";Database=" & Database_Name & _ 
    ";Uid=" & User_ID & ";Pwd=" & Password & ";" 

    rs.Open SQLStr, Cn, adOpenStatic 

    With Worksheets("Sheet1").Range("A2:Z500") 
     .ClearContents 
     .CopyFromRecordset rs 
    End With 

    rs.Close 
    Set rs = Nothing 
    Cn.Close 
    Set Cn = Nothing 
End Sub 

或者。 。 。

Sub TestMacro() 

' Create a connection object. 
Dim cnPubs As ADODB.Connection 
Set cnPubs = New ADODB.Connection 

' Provide the connection string. 
Dim strConn As String 

'Use the SQL Server OLE DB Provider. 
strConn = "PROVIDER=SQLOLEDB;" 

'Connect to the Pubs database on the local server. 
strConn = strConn & "DATA SOURCE=(local);INITIAL CATALOG=NORTHWIND.MDF;" 

'Use an integrated login. 
strConn = strConn & " INTEGRATED SECURITY=sspi;" 

'Now open the connection. 
cnPubs.Open strConn 

' Create a recordset object. 
Dim rsPubs As ADODB.Recordset 
Set rsPubs = New ADODB.Recordset 

With rsPubs 
    ' Assign the Connection object. 
    .ActiveConnection = cnPubs 
    ' Extract the required records. 
    .Open "SELECT * FROM Categories" 
    ' Copy the records into cell A1 on Sheet1. 
    Sheet1.Range("A1").CopyFromRecordset rsPubs 

    ' Tidy up 
    .Close 
End With 

cnPubs.Close 
Set rsPubs = Nothing 
Set cnPubs = Nothing 

End Sub 

此外,請檢查下面的鏈接。

https://www.excel-sql-server.com/excel-import-to-sql-server-using-distributed-queries.htm#Introduction

https://www.excel-sql-server.com/excel-sql-server-import-export-using-vba.htm#Introduction

相關問題