954,559 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to Execute Stored Procedure in VB .. HELP

Hi Friends .. need a small help can anyone help me in showing how to call a stored procedure which returns a recordset .. yea i need to pass one parameter also for getting that required data .. thanks in advance..


Sameer

koolsamjust4u
Newbie Poster
1 post since May 2009
Reputation Points: 10
Solved Threads: 0
 

Just use a recordset object as you normally would. i.e. say your SP has a name of MySP and it accepts one parameter. So you query string would be something like...

MyQueryString = "MySp " & MyValue

Then execute the query as you normally would.

Good Luck

vb5prgrmr
Posting Virtuoso
1,912 posts since Mar 2009
Reputation Points: 156
Solved Threads: 296
 

Hi Friends .. need a small help can anyone help me in showing how to call a stored procedure which returns a recordset .. yea i need to pass one parameter also for getting that required data .. thanks in advance..

Sameer


Hi Sameer,

If you are using VB6 then u need to use Command object.

Please see the below example
Dim mobjConn As ADODB.Connection
Dim mobjCmd As ADODB.Command
Dim mobjRst As ADODB.Recordset

Set mobjCmd = New ADODB.Command

mobjCmd.CommandType = adCmdStoredProc

mobjCmd.Parameters.Append mobjCmd.CreateParameter(" ", , , ,)

mobjCmd.CommandText = strSPName
mobjCmd.Execute

Please let me know if you have any issues

Mahesh B Sayani

maheshsayani
Light Poster
45 posts since Jul 2006
Reputation Points: 10
Solved Threads: 2
 

Correct me if I am wrong maheshsayani or anyone else, but using a command object used that way will not return a recorset....

>need a small help can anyone help me in showing how to call a stored procedure which returns a recordset

Hence, my suggestion to use a recordset object...

vb5prgrmr
Posting Virtuoso
1,912 posts since Mar 2009
Reputation Points: 156
Solved Threads: 296
 
Correct me if I am wrong maheshsayani or anyone else, but using a command object used that way will not return a recorset....

I am sure you know that the result of command.execute is record set. so may be explain your question more?

As I did not understand the PO question, I did not on which back end he talking about. does he mean a SP in SQL Server or a normal function in vb he is calling it stored procedure.

samir_ibrahim
Junior Poster
155 posts since Sep 2008
Reputation Points: 69
Solved Threads: 19
 

yup please specify waht db you are using and what connection you use, is it either DAO or ADO.

jireh
Posting Whiz
316 posts since Jul 2007
Reputation Points: 11
Solved Threads: 49
 
I am sure you know that the result of command.execute is record set. so may be explain your question more?

Yes, I do realize that if you...

Set recordset = command.Execute( RecordsAffected, Parameters, Options )

but the way maheshsayani used it...

command.Execute RecordsAffected, Parameters, Options

does not return a recordset, hence my post...

As for understanding the OP's origional Q see post #1 above. It seems pretty strait forward to me...

Good Luck

vb5prgrmr
Posting Virtuoso
1,912 posts since Mar 2009
Reputation Points: 156
Solved Threads: 296
 

'ADO is a DataEnvironment
'conGeneral is a DEConnection (to connect to the SQL Server)

'on the form dim the recordset

Dim rsRecord As New ADODB.Recordset

'then call the Stored Procedure "pro_StoredProc" with the parameter
With rsRecord
If .state = adStateOpen Then .Close
'
' set propertie of recordset (don't need if only read records)
.CursorType = adOpenDynamic
.CursorLocation = adUseClient
.LockType = adLockOptimistic
' open recordset with a numeric Parameter
.Open ("{Call pro_StoredProc(" & Parameter & ")}"), ADO.conGeneral
' open recordset with a String Parameter
.Open ("{Call pro_StoredProc('" & Parameter & "')}"), ADO.conGeneral
'then go on as usual
Do Until .EOF
'read the record
.MoveNext
Loop

End With

Tassilo
Newbie Poster
2 posts since Jun 2009
Reputation Points: 10
Solved Threads: 0
 

Hi Sameer,

If you are using VB6 then u need to use Command object.

Please see the below example Dim mobjConn As ADODB.Connection Dim mobjCmd As ADODB.Command Dim mobjRst As ADODB.Recordset

Set mobjCmd = New ADODB.Command

mobjCmd.CommandType = adCmdStoredProc

mobjCmd.Parameters.Append mobjCmd.CreateParameter(" ", , , ,)

mobjCmd.CommandText = strSPName mobjCmd.Execute

Please let me know if you have any issues

Mahesh B Sayani


Hi,
Sorry if im too late.
But please can you explain, what is "strSPName" and will the "mobjCmd" return a resultset. If yes, then how must the resultset be handled if needed?


Thank you.

RahulV
Junior Poster in Training
92 posts since Jun 2007
Reputation Points: 26
Solved Threads: 0
 

Hi Friends .. need a small help can anyone help me in showing how to call a stored procedure which returns a recordset .. yea i need to pass one parameter also for getting that required data .. thanks in advance..

Sameer


Dear sameer
the code for store procedure as below

Dim TDSCmd As ADODB.Command
Dim prm1 As ADODB.Parameter

Set TDSCmd = New ADODB.Command
Set TDSCmd.ActiveConnection = con
con.CommandTimeout = 600

TDSCmd.CommandType = adCmdStoredProc
TDSCmd.CommandText = "SAN_BOM_COST_UPDATE"
Set prm1 = New ADODB.Parameter
With prm1
.Type = adVarChar
.Direction = adParamInput
.Size = 40
.value = MACHINE_NAME
End With
TDSCmd.CommandTimeout = 300
TDSCmd.Parameters.Append prm1
TDSCmd.CommandTimeout = 300
TDSCmd.Execute

SANJAY.DESHMUKH
Newbie Poster
4 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

Dear sameer the code for call procedure in vb

SANJAY.DESHMUKH
Newbie Poster
4 posts since Aug 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You