Ways to bring data from SQL server but need input parameters for SQL to read

albertan

Board Regular
Joined
Nov 16, 2014
Messages
68
Office Version
  1. 365
I was wondering if someone can please help me on best way to use parametirized SQL query and bring it to Power Query. I have access to SQL server with known connection string. SQL is long and needs to use specific input parameters (project numbers) in it. I don't want to have them changed each time in source so I am looking for ways to use parameters to modify the data. I may have up to 20 projects I need to change.

In "Get Data" there are few ways to do it: Microsoft Query, SQL Server database, OLEDB. But I need to specify project numbers in SQL.

What I thought might help is to use Microsoft Query and then use parameters there.
I am trying to use VBA like in this video https://www.youtube.com/watch?v=HE9CIbetNnI however I'm not sure I can use variable or parameter in VBA code where SQL can refer to range and read the SQL

Any help would be great.
 

Excel Facts

Pivot Table Drill Down
Double-click any number in a pivot table to create a new report showing all detail rows that make up that number
Upvote 0
Can anybody help here? I appreciate any help on VBA on SQL Query that is using Left Outer join of two databases. The SQL itself works perfectly in SQL Query but I'd like to automate it so that I can have input parameters to be put in spreadsheet as Range and I want SQL to execute in VBA and read those parameters. As they change, I expect the data extract from SQL will change.
Here's my initial code:

Dim connStr As String
Dim myservername As String
Dim mydatabase As String
Dim myuserid As String
Dim mypasswd As String


myservername = ThisWorkbook.Sheets(1).Cells(1, 3).Value
mydatabase = ThisWorkbook.Sheets(1).Cells(1, 5).Value
myuserid = ThisWorkbook.Sheets(1).Cells(1, 1).Value
mypasswd = ThisWorkbook.Sheets(1).Cells(1, 2).Value
connStr = "Provider=MSDASQL.1;DRIVER=SQL Server;Password=" & mypasswd & ";Persist Security Info=True;User ID=" & myuserid & ";APP=Microsoft Office 2016;WSID=xxx"

Dim connection As ADODB.connection
Set connection = New ADODB.connection
connection.ConnectionString = connStr
connection.Open

' Open recordset.
Set cmd1 = New ADODB.Command
cmd1.ActiveConnection = connection

cmd1.CommandText = "SELECT mydata.*
' I'm struggling here if to add variable or input for project number

Debug.Print cmd1.CommandText
Set Results = cmd1.Execute()


If Results.EOF Then
' Recordset is empty
MsgBox "No results."
Debug.Print cmd1.CommandText
Else





' Clear the data from the active worksheet
Cells.Select
Cells.ClearContents


While Not Results.EOF


' Add column headers to the sheet
headers = Results.Fields.Count
For iCol = 1 To headers
Cells(1, iCol).Value = Results.Fields(iCol - 1).Name
Next


' Copy the resultset to the active worksheet
Cells(2, 1).CopyFromRecordset Results, 900000

'add another sheet if we're not at the end of the recordset
If Not Results.EOF Then Sheets.Add

Wend


End If
' Stop running the macro
MsgBox "Data Extraction Successfully Completed"

End Sub
 
Upvote 0

Forum statistics

Threads
1,223,954
Messages
6,175,603
Members
452,660
Latest member
Zatman

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top