473,396 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Output Parameter returning empty string

OK, not sure if there is a way around this. I have some relatively complicated SQL code in a stored proc. The proc is working fine. When I execute this code in SSMS, it returns a record set and an output parameter (varchar(50):
Expand|Select|Wrap|Line Numbers
  1. Declare @name varchar(50)
  2. exec pra_StoredProc
  3.     @id=12,
  4.     @name=@name OUTPUT
  5. select @name
So, I need to access this data from Microsoft Access. I need the proc to return a recordset that I can work with and I also need the output parameter value. However, when I do this in VBA:
Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim myrst As New ADODB.Recordset
  3. Dim mycmd As New ADODB.Command
  4. Dim mycon As New ADODB.Connection
  5.  
  6. mycon.open connectionString
  7.  
  8. With mycmd
  9.     .ActiveConnection = mycon
  10.     .CommandText = "pra_StoredProc"
  11.     .CommandType = adCmdStoredProc
  12.     .Parameters.Append .CreateParameter("@id", adInteger, adParamInput, , 12)
  13.     .Parameters.Append .CreateParameter("@name", adVarChar, adParamOutput, 50)
  14.     Set myrst = .Execute
  15.     MsgBox .Parameters("@name")
  16. End With
  17.  
The msgbox displays an empty string. The problem I know is the line that says:
Expand|Select|Wrap|Line Numbers
  1. Set myrst = .Execute
If I change it to just:
Expand|Select|Wrap|Line Numbers
  1. .Execute
it returns the right value for the output parameter. However, I also need to work with the recordset returned by the proc, which is why I was setting the myrst object reference to mycmd.execute. Is there any way to get both the data in the recordset returned by the stored proc into an ADODB Recordset AND the output parameter's value into a string variable (besides executing the proc twice)?
Oct 5 '10 #1
4 5075
Hmmm, no takers huh? I am just going to execute the proc twice, but this makes me a little worried, as I am trying to write a procedure that can handle all kinds of these procedures and some of them are going to take 15+ seconds to execute (per execution). Not crazy about that.
Oct 8 '10 #2
MMcCarthy
14,534 Expert Mod 8TB
Can you explain more about what value is being returned by the stored proc. Is it just one record and one field and if so why are you putting it in a recordset variable.
Oct 8 '10 #3
The proc constructs a select statement and executes it. It returns the results of the select along with the scalar value in the output parameter. The last 2 statements would be something like this (the @sql variable contains the text of the select statement):
Expand|Select|Wrap|Line Numbers
  1. @name = 'The title of a graph'
  2. exec(@sql)
  3.  
The purpose of the vb code is to take the dataset returned by the proc and dump it into an excel sheet (nicely formatted) and automatically generate a corresponding chart. I want to be able to use this same code for all sorts of these same kind of procs - the dataset becoming the data for the excel range and the scalar value from the output parameter becoming the title of the graph.
Oct 9 '10 #4
MMcCarthy
14,534 Expert Mod 8TB
OK this is not the way I would have done it but I've done some research and this appears to be how it works.

Expand|Select|Wrap|Line Numbers
  1. Dim myCon As ADODB.Connection  
  2. Dim myCmd As ADODB.Command 
  3. Dim myPara1 As ADODB.Parameter
  4. Dim myPara2 As ADODB.Parameter
  5. Dim myRst As ADODB.Recordset 
  6.  
  7.     Set myCon = New ADODB.Connection
  8.     Set myCmd = New ADODB.Command 
  9.  
  10.     myCon.ConnectionString = "connectionString"
  11.     myCon.Open myCon.ConnectionString
  12.  
  13.     With mycmd
  14.         .ActiveConnection = myCon.ConnectionString
  15.         .CommandText = "pra_StoredProc" 
  16.         .CommandType = adCmdStoredProc 
  17.     End With
  18.  
  19.     'Create 2 output parameters
  20.     Set myPara1 = myCmd.CreateParameter("id", adInteger, adParamOutput, , 12)
  21.     Set mypara2 = myCmd.CreateParameter("name", adVarChar, adParamOutput, 50)
  22.  
  23.     'Append the output parameters to command object
  24.     myCmd.Parameters.Append myPara1
  25.     myCmd.Parameters.Append mypara2
  26.  
  27.     Set myRst = myCmd.Execute 
  28.  
  29.     'Add your code here
  30.  
  31.     'close the objects
  32.     myRst.Close
  33.     myCon.Close
  34.  
  35.     'reset the objects
  36.     Set myCmd = Nothing
  37.     Set myCon = Nothing
  38.     Set myPara1 = Nothing
  39.     Set mypara2 = Nothing
  40.     Set myRst = Nothing
Oct 13 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: LoopyNZ | last post by:
Hi, I need some detective work done on my code. I have a function that is meant to return a string (see below). To a point, it seems to work fine, i.e. if I break at Line X, strResult = "By";...
5
by: vivienne.netherwood | last post by:
I am developing an Access Project front end with a SQL server database. I have written a stored procedure that returns a record set and also a value via an output parameter. The procedure is as...
4
by: Jon Skeet | last post by:
I've just noticed something rather odd and disturbing. The following code displays "True": using System; class Test { public static void Main(string args) { string x = new string...
1
by: Urs Wigger | last post by:
In a Managed C++ module, I have a function returning an integer as output parameter: bool Func1( int __gc *iout) { *iout = 33; return true; } Calling Func1 from a C# application works...
0
by: Chris Fink | last post by:
Why is the below code not returning an output parameter. When I run the SQL, it is returning the value '1111', but the param1.value is empty? string sql = String.Format("SELECT LICENSEID...
21
by: M D | last post by:
You know how you assume you know until you find out you don't know. Well, I typed into a function definition "..., new String("")). I know what I want. Everyone reading this knows what I want....
8
by: Alec MacLean | last post by:
Hi, I'm using the DAAB Ent Lib (Jan 2006) for .NET 2.0, with VS 2005 Pro. My project is a Web app project (using the WAP add in). Background: I'm creating a survey system for our company, for...
1
by: John Bailo | last post by:
This is a my solution to getting an Output parameter from a SqlDataSource. I have seen a few scant articles but none of them take it all the way to a solution. Hopefully this will help some...
10
by: Mike | last post by:
Sql Server, Scope_Identity(), Ado.NET: Which is better? Using an output parameter to return Scope_Identity through ExecuteNonQuery(), or adding Select Scope_Identity() to the end of the...
8
by: Mike P | last post by:
How do you return a datareader from a stored procedure, but also return an output parameter? Here is my code, which is just returning a data reader at the moment : _conn.Open(); SqlCommand...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.