(Solved) – Can you use ADODB to connect Excel to Azure SQL Server DW?

  • by
(solved)-–-can-you-use-adodb-to-connect-excel-to-azure-sql-server-dw?

I’m trying to run a simple query from Excel to newer version of SQL Server. I have done this many times in the past, but it was always to a standard SQL Server DB. Now, I’m working with a new animal, which is Azure SQL Server Data Warehouse. I am using very generic VBA code to connect to the DW.

Sub TryMe()

'Initializes variables
Dim cnn As New ADODB.Connection
Dim rst As New ADODB.Recordset
Dim ConnectionString As String
Dim StrQuery As String

pswd = "my_pwd"
user = "my_used_ID"
dbName = "my_DB_name"
server = "server_name.database.Windows.net"

'Setup the connection string for accessing MS SQL database
   'Make sure to change:
       '1: PASSWORD
       '2: USERNAME
       '3: REMOTE_IP_ADDRESS
       '4: DATABASE
    ConnectionString = "Provider=SQLOLEDB;Password=pswd;User ID=user;Data Source=server;Use Encryption for Data=False;Initial Catalog=dbname"

    'Opens connection to the database
    cnn.Open ConnectionString
    'Timeout error in seconds for executing the entire query; this will run for 15 minutes before VBA timesout, but your database might timeout before this value
    cnn.CommandTimeout = 900

    'This is your actual MS SQL query that you need to run; you should check this query first using a more robust SQL editor (such as HeidiSQL) to ensure your query is valid
    StrQuery = "SELECT TOP 10 * FROM myBigTable"

    'Performs the actual query
    rst.Open StrQuery, cnn
    'Dumps all the results from the StrQuery into cell A2 of the first sheet in the active workbook
    Sheets(1).Range("A2").CopyFromRecordset rst
End Sub

It should be a pretty straightforward thing, but I keep getting an error message that says: ‘SQL Server does not exist or access is denied’.

The error occurs on this line:

cnn.Open ConnectionString

I set a reference to Microsoft ActiveX Data Objects 2.0 Library.

Has anyone tried this and actually gotten this to work?

Leave a Reply

Your email address will not be published. Required fields are marked *