Solved

MS Query. Need to load .commandtext with variable but fails.

Posted on 2015-02-24
3
121 Views
Last Modified: 2016-02-11
I have a macro where I run a MS Query several times but I want to run a different SQL Stored Procedure each time.
I am trying to load the .commandtext with data from a variable. This way I just need to change the variable and execute the same query instead of running 5 different queries.

I load  the variable with the correct  stored procedure to run in the .commandtext, but the query fails with syntax error.
ODBCCommand = "Array(" & Chr(34) & "exec MyStoredProc" & Chr(34) & ")"

This is the subroutine to run the MS Query.
Sub GetMyInfo()
With ActiveSheet.ListObjects.Add(SourceType:=0, Source:= _
"ODBC;DSN=MyDBName;UID=username;Trusted_Connection=Yes;APP=Microsoft Office 2010;WSID=VirtualXP-PC" _
, Destination:=ActiveCell).QueryTable
'.CommandText = Array("exec MyStoredProc") <This will work but I have it commented out to use variable instead.
 .CommandText = ODBCCommand      <Even though variable is correct, query fails with "SQL Syntax error"
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = True
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.PreserveColumnInfo = True
.Refresh BackgroundQuery:=False  <<This is where it fails with "SQL Syntax error"
End With
End Sub
0
Comment
Question by:briandobo
3 Comments
 
LVL 12

Expert Comment

by:Jeff Darling
ID: 40628519
You can build the array yourself like this:

Dim myExecSQL
Dim myODBCCommand
myExecSQL = "exec MyStoredProc"
myODBCCommand = Array(myExecSQL)

Open in new window

0
 
LVL 33

Accepted Solution

by:
Norie earned 500 total points
ID: 40628565
What happens if you just use this?
ODBCCommand = "exec MyStoredProc"

Open in new window

0
 

Author Comment

by:briandobo
ID: 40628622
WOW, sure thought I tried the:
ODBCCommand = "exec MyStoredProc"

But it worked for me.
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
The viewer will learn how to create two correlated normally distributed random variables in Excel, use a normal distribution to simulate the return on different levels of investment in each of the two funds over a period of ten years, and, create a …
This Micro Tutorial will demonstrate in Microsoft Excel how to add style and sexy appeal to horizontal bar charts.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now