Executing SQL Stored Procedure through Excel (VBA) with multiple parameters





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







0















Thanks for your help, @FreeMan.



However, I am still lost with this.



Let me just start from scratch.



This is my query in summary ( I hope it is clear)



SELECT DISTINCT Q1,Q2,Q3

from(SELECT A,B,C) Q1

full outer join

(SELECT A,B,C

from tblA as a

join tblB as b

on a.aID = b.bID

join tblC as c

on b.bID = c.cID

where (a,b, cdate <= ?

group by c) Q2

ON Q1.A = Q2.B

WHERE Convert(DATETIME, Q1.[B], 103) <= ?

order by Q1.[A]

When I try to refresh my table, I get the following error message

"[Microsoft][ODBC SQL Server Driver]Invalid Parameter number" and then

"[Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index"


I need a method that will allow me to run this query using the parameters in a specified cell i.e. cell C3.



I hope this makes things a bit clearer I am not familiar with VBA so all the commands advised above are confusing to me.



Original Question



*[I am a newbie when it comes to VBA and so I am struggling to get my query to run with more than one parameter.
I am using this macro as but when I run it it tells me the second parameter has not been provided.



Sub RefreshQuery()

With ActiveWorkbook.Connections("MYSERVER").OLEDBConnection
.CommandText = "EXECUTE dbo.Tng_Market_Feed '" & Range("B2").Value & "'"
End With
ActiveWorkbook.Connections("MYSERVER").Refresh

End Sub


My questions are:-
1. How do I set the variables to date formats? I need a report to show me all transactions <= a certain date (variable parameter)
2. The location of the variable in the spreadsheet, cell b2, remains the same for both parameters. For example, I need a list of all transactions <= 31/12/2014 and a value/sum of bad debt provision as at the same date 31/12/2014.]*










share|improve this question

























  • You want the macro to update the spreadsheet from a db? Or you want the macro to do something in the db itself?

    – Raystafarian
    May 29 '15 at 11:26


















0















Thanks for your help, @FreeMan.



However, I am still lost with this.



Let me just start from scratch.



This is my query in summary ( I hope it is clear)



SELECT DISTINCT Q1,Q2,Q3

from(SELECT A,B,C) Q1

full outer join

(SELECT A,B,C

from tblA as a

join tblB as b

on a.aID = b.bID

join tblC as c

on b.bID = c.cID

where (a,b, cdate <= ?

group by c) Q2

ON Q1.A = Q2.B

WHERE Convert(DATETIME, Q1.[B], 103) <= ?

order by Q1.[A]

When I try to refresh my table, I get the following error message

"[Microsoft][ODBC SQL Server Driver]Invalid Parameter number" and then

"[Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index"


I need a method that will allow me to run this query using the parameters in a specified cell i.e. cell C3.



I hope this makes things a bit clearer I am not familiar with VBA so all the commands advised above are confusing to me.



Original Question



*[I am a newbie when it comes to VBA and so I am struggling to get my query to run with more than one parameter.
I am using this macro as but when I run it it tells me the second parameter has not been provided.



Sub RefreshQuery()

With ActiveWorkbook.Connections("MYSERVER").OLEDBConnection
.CommandText = "EXECUTE dbo.Tng_Market_Feed '" & Range("B2").Value & "'"
End With
ActiveWorkbook.Connections("MYSERVER").Refresh

End Sub


My questions are:-
1. How do I set the variables to date formats? I need a report to show me all transactions <= a certain date (variable parameter)
2. The location of the variable in the spreadsheet, cell b2, remains the same for both parameters. For example, I need a list of all transactions <= 31/12/2014 and a value/sum of bad debt provision as at the same date 31/12/2014.]*










share|improve this question

























  • You want the macro to update the spreadsheet from a db? Or you want the macro to do something in the db itself?

    – Raystafarian
    May 29 '15 at 11:26














0












0








0








Thanks for your help, @FreeMan.



However, I am still lost with this.



Let me just start from scratch.



This is my query in summary ( I hope it is clear)



SELECT DISTINCT Q1,Q2,Q3

from(SELECT A,B,C) Q1

full outer join

(SELECT A,B,C

from tblA as a

join tblB as b

on a.aID = b.bID

join tblC as c

on b.bID = c.cID

where (a,b, cdate <= ?

group by c) Q2

ON Q1.A = Q2.B

WHERE Convert(DATETIME, Q1.[B], 103) <= ?

order by Q1.[A]

When I try to refresh my table, I get the following error message

"[Microsoft][ODBC SQL Server Driver]Invalid Parameter number" and then

"[Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index"


I need a method that will allow me to run this query using the parameters in a specified cell i.e. cell C3.



I hope this makes things a bit clearer I am not familiar with VBA so all the commands advised above are confusing to me.



Original Question



*[I am a newbie when it comes to VBA and so I am struggling to get my query to run with more than one parameter.
I am using this macro as but when I run it it tells me the second parameter has not been provided.



Sub RefreshQuery()

With ActiveWorkbook.Connections("MYSERVER").OLEDBConnection
.CommandText = "EXECUTE dbo.Tng_Market_Feed '" & Range("B2").Value & "'"
End With
ActiveWorkbook.Connections("MYSERVER").Refresh

End Sub


My questions are:-
1. How do I set the variables to date formats? I need a report to show me all transactions <= a certain date (variable parameter)
2. The location of the variable in the spreadsheet, cell b2, remains the same for both parameters. For example, I need a list of all transactions <= 31/12/2014 and a value/sum of bad debt provision as at the same date 31/12/2014.]*










share|improve this question
















Thanks for your help, @FreeMan.



However, I am still lost with this.



Let me just start from scratch.



This is my query in summary ( I hope it is clear)



SELECT DISTINCT Q1,Q2,Q3

from(SELECT A,B,C) Q1

full outer join

(SELECT A,B,C

from tblA as a

join tblB as b

on a.aID = b.bID

join tblC as c

on b.bID = c.cID

where (a,b, cdate <= ?

group by c) Q2

ON Q1.A = Q2.B

WHERE Convert(DATETIME, Q1.[B], 103) <= ?

order by Q1.[A]

When I try to refresh my table, I get the following error message

"[Microsoft][ODBC SQL Server Driver]Invalid Parameter number" and then

"[Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index"


I need a method that will allow me to run this query using the parameters in a specified cell i.e. cell C3.



I hope this makes things a bit clearer I am not familiar with VBA so all the commands advised above are confusing to me.



Original Question



*[I am a newbie when it comes to VBA and so I am struggling to get my query to run with more than one parameter.
I am using this macro as but when I run it it tells me the second parameter has not been provided.



Sub RefreshQuery()

With ActiveWorkbook.Connections("MYSERVER").OLEDBConnection
.CommandText = "EXECUTE dbo.Tng_Market_Feed '" & Range("B2").Value & "'"
End With
ActiveWorkbook.Connections("MYSERVER").Refresh

End Sub


My questions are:-
1. How do I set the variables to date formats? I need a report to show me all transactions <= a certain date (variable parameter)
2. The location of the variable in the spreadsheet, cell b2, remains the same for both parameters. For example, I need a list of all transactions <= 31/12/2014 and a value/sum of bad debt provision as at the same date 31/12/2014.]*







vba sql dynamic parameters






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jul 15 '15 at 11:58









td512

4,62821437




4,62821437










asked May 28 '15 at 16:01









KikiKiki

111




111













  • You want the macro to update the spreadsheet from a db? Or you want the macro to do something in the db itself?

    – Raystafarian
    May 29 '15 at 11:26



















  • You want the macro to update the spreadsheet from a db? Or you want the macro to do something in the db itself?

    – Raystafarian
    May 29 '15 at 11:26

















You want the macro to update the spreadsheet from a db? Or you want the macro to do something in the db itself?

– Raystafarian
May 29 '15 at 11:26





You want the macro to update the spreadsheet from a db? Or you want the macro to do something in the db itself?

– Raystafarian
May 29 '15 at 11:26










1 Answer
1






active

oldest

votes


















0














Here's a quick sample of creating and setting parameters from within your code:



Sub CreateParam()

Dim oQuery As QueryTable
Dim oParam As Parameter

Set oQuery = Sheet3.QueryTables(1)

oQuery.CommandText = Replace(oQuery.CommandText, "='Berlin'", "=?")

Set oParam = oQuery.Parameters.Add("CityParam")

oParam.SetParam xlRange, Sheet3.Range("J1")
oParam.RefreshOnChange = True

oQuery.Refresh

End Sub


code quickly cribbed from dicks-clicks.com



Basically you'll need to execute the .Parameter.Add() line for each parameter in your stored procedure, and you'll be good to go. Note that you don't need to write a whole Sub() to do it, you can just include the Set oParam = oQuery.Parameters.Add() line in your existing code.



You may want to dig into the the options a bit, because you can specify the type of data being sent, and can also set up an OUT parameter, as well, to accept data back from the query.






share|improve this answer
























  • Thanks for your response, FreeMan. Apologies, but I seem to have omitted the fact that I am trying to create a macro that will execute a stored procedure within SQL which has more than one parameter.

    – Kiki
    May 29 '15 at 10:57













  • @kiki - I got that part... Read the first paragraph below the code. Especially the first sentence.

    – FreeMan
    May 29 '15 at 12:03












Your Answer








StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f920812%2fexecuting-sql-stored-procedure-through-excel-vba-with-multiple-parameters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Here's a quick sample of creating and setting parameters from within your code:



Sub CreateParam()

Dim oQuery As QueryTable
Dim oParam As Parameter

Set oQuery = Sheet3.QueryTables(1)

oQuery.CommandText = Replace(oQuery.CommandText, "='Berlin'", "=?")

Set oParam = oQuery.Parameters.Add("CityParam")

oParam.SetParam xlRange, Sheet3.Range("J1")
oParam.RefreshOnChange = True

oQuery.Refresh

End Sub


code quickly cribbed from dicks-clicks.com



Basically you'll need to execute the .Parameter.Add() line for each parameter in your stored procedure, and you'll be good to go. Note that you don't need to write a whole Sub() to do it, you can just include the Set oParam = oQuery.Parameters.Add() line in your existing code.



You may want to dig into the the options a bit, because you can specify the type of data being sent, and can also set up an OUT parameter, as well, to accept data back from the query.






share|improve this answer
























  • Thanks for your response, FreeMan. Apologies, but I seem to have omitted the fact that I am trying to create a macro that will execute a stored procedure within SQL which has more than one parameter.

    – Kiki
    May 29 '15 at 10:57













  • @kiki - I got that part... Read the first paragraph below the code. Especially the first sentence.

    – FreeMan
    May 29 '15 at 12:03
















0














Here's a quick sample of creating and setting parameters from within your code:



Sub CreateParam()

Dim oQuery As QueryTable
Dim oParam As Parameter

Set oQuery = Sheet3.QueryTables(1)

oQuery.CommandText = Replace(oQuery.CommandText, "='Berlin'", "=?")

Set oParam = oQuery.Parameters.Add("CityParam")

oParam.SetParam xlRange, Sheet3.Range("J1")
oParam.RefreshOnChange = True

oQuery.Refresh

End Sub


code quickly cribbed from dicks-clicks.com



Basically you'll need to execute the .Parameter.Add() line for each parameter in your stored procedure, and you'll be good to go. Note that you don't need to write a whole Sub() to do it, you can just include the Set oParam = oQuery.Parameters.Add() line in your existing code.



You may want to dig into the the options a bit, because you can specify the type of data being sent, and can also set up an OUT parameter, as well, to accept data back from the query.






share|improve this answer
























  • Thanks for your response, FreeMan. Apologies, but I seem to have omitted the fact that I am trying to create a macro that will execute a stored procedure within SQL which has more than one parameter.

    – Kiki
    May 29 '15 at 10:57













  • @kiki - I got that part... Read the first paragraph below the code. Especially the first sentence.

    – FreeMan
    May 29 '15 at 12:03














0












0








0







Here's a quick sample of creating and setting parameters from within your code:



Sub CreateParam()

Dim oQuery As QueryTable
Dim oParam As Parameter

Set oQuery = Sheet3.QueryTables(1)

oQuery.CommandText = Replace(oQuery.CommandText, "='Berlin'", "=?")

Set oParam = oQuery.Parameters.Add("CityParam")

oParam.SetParam xlRange, Sheet3.Range("J1")
oParam.RefreshOnChange = True

oQuery.Refresh

End Sub


code quickly cribbed from dicks-clicks.com



Basically you'll need to execute the .Parameter.Add() line for each parameter in your stored procedure, and you'll be good to go. Note that you don't need to write a whole Sub() to do it, you can just include the Set oParam = oQuery.Parameters.Add() line in your existing code.



You may want to dig into the the options a bit, because you can specify the type of data being sent, and can also set up an OUT parameter, as well, to accept data back from the query.






share|improve this answer













Here's a quick sample of creating and setting parameters from within your code:



Sub CreateParam()

Dim oQuery As QueryTable
Dim oParam As Parameter

Set oQuery = Sheet3.QueryTables(1)

oQuery.CommandText = Replace(oQuery.CommandText, "='Berlin'", "=?")

Set oParam = oQuery.Parameters.Add("CityParam")

oParam.SetParam xlRange, Sheet3.Range("J1")
oParam.RefreshOnChange = True

oQuery.Refresh

End Sub


code quickly cribbed from dicks-clicks.com



Basically you'll need to execute the .Parameter.Add() line for each parameter in your stored procedure, and you'll be good to go. Note that you don't need to write a whole Sub() to do it, you can just include the Set oParam = oQuery.Parameters.Add() line in your existing code.



You may want to dig into the the options a bit, because you can specify the type of data being sent, and can also set up an OUT parameter, as well, to accept data back from the query.







share|improve this answer












share|improve this answer



share|improve this answer










answered May 28 '15 at 16:22









FreeManFreeMan

238211




238211













  • Thanks for your response, FreeMan. Apologies, but I seem to have omitted the fact that I am trying to create a macro that will execute a stored procedure within SQL which has more than one parameter.

    – Kiki
    May 29 '15 at 10:57













  • @kiki - I got that part... Read the first paragraph below the code. Especially the first sentence.

    – FreeMan
    May 29 '15 at 12:03



















  • Thanks for your response, FreeMan. Apologies, but I seem to have omitted the fact that I am trying to create a macro that will execute a stored procedure within SQL which has more than one parameter.

    – Kiki
    May 29 '15 at 10:57













  • @kiki - I got that part... Read the first paragraph below the code. Especially the first sentence.

    – FreeMan
    May 29 '15 at 12:03

















Thanks for your response, FreeMan. Apologies, but I seem to have omitted the fact that I am trying to create a macro that will execute a stored procedure within SQL which has more than one parameter.

– Kiki
May 29 '15 at 10:57







Thanks for your response, FreeMan. Apologies, but I seem to have omitted the fact that I am trying to create a macro that will execute a stored procedure within SQL which has more than one parameter.

– Kiki
May 29 '15 at 10:57















@kiki - I got that part... Read the first paragraph below the code. Especially the first sentence.

– FreeMan
May 29 '15 at 12:03





@kiki - I got that part... Read the first paragraph below the code. Especially the first sentence.

– FreeMan
May 29 '15 at 12:03


















draft saved

draft discarded




















































Thanks for contributing an answer to Super User!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f920812%2fexecuting-sql-stored-procedure-through-excel-vba-with-multiple-parameters%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?