2017-09-25 8 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。最初の2文字が共通しているため、スパムにタグを付けないでください。これはちょうど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

関連する問題