Solved

How to execute a SQL Server 2005 Stored Procedure and get a return value in VB6?

Posted on 2013-01-16
5
639 Views
Last Modified: 2013-01-17
Hello everyone,
                    I have an application in Visual Basic 6 and I need to execute a stored procedure and get a return value.  How can I do this? what is the code that I can use.

Visual Basic 6 no Visual Basic.NET

Thank you
0
Comment
Question by:iscivanomar
5 Comments
 
LVL 13

Expert Comment

by:AielloJ
ID: 38784715
iscivanomar,

It's been awhile, but this should work for you:

CREATE PROCEDURE my_proc
            @param1 varchar(10),
            @param2 int
:
:
:
:
Public Shared Function my_function(ByVal myVB_Param As String) As String

    'create connection
    Using cnSQL As SqlConnection = New SqlConnection("MyConnectionString")
        Using cmdSP As New SqlCommand("my_proc", cnSQL)
            cmdSP.CommandType = System.Data.CommandType.StoredProcedure

            'declare and add  parameter
            cmdSP.Parameters.Add("@param1", System.Data.SqlDbType.NVarChar, 50).Value = "ABC"
            cmdSP.Parameters.Add("@param2", System.Data.SqlDbType.Integer, 0).Value = 0

            Dim dtTable As New DataTable

            'execute command
            Try
                cnSQL.Open()
                dtTable.Load(cmdSP.ExecuteReader())
                cnSQL.Close()
            Catch ex As SqlException
                'do something here
            End Try

            'return string
            Return dtTable.Rows(0)(0)
        End Using
    End Using

End Function

Best regards,

AielloJ
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 150 total points
ID: 38784723
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 38784725
Looks like @AielloJ missed the

Visual Basic 6 no Visual Basic.NET
0
 
LVL 9

Assisted Solution

by:shorvath
shorvath earned 100 total points
ID: 38787347
Absolutely the easiest way to work with VB6 and Stored Procedures is the ADO Stored Procedure Add-IN from Microsoft.  This Add-In integrates with the VB6 IDE and all you have to do is select the server and stored procedure and it writes all of the code for you.  

Although you can't get it from Microsoft anymore, I did manage to find it for you at:

http://www.vbrad.com/article.aspx?id=103

Just click the download link and install as per instructions.

After you see how easy this is, you can then get a little bit more advanced.
This link shows you how to customize the add-in for you own purposes.  I remember creating server specific custom add-ins using this method and it saved me hundreds of hours of coding over the years.

http://msdn.microsoft.com/en-us/magazine/cc301907.aspx
0
 

Author Closing Comment

by:iscivanomar
ID: 38788984
Thank you for your help,
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
OfficeMate Freezes on login or does not load after login credentials are input.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.

743 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

11 Experts available now in Live!

Get 1:1 Help Now