Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Stored Procedures that return a value

Posted on 2006-04-25
3
Medium Priority
?
371 Views
Last Modified: 2012-06-27
I want to Add values into a table and return the Identity. Please see my SP and VB code below.

--------------------------------SP---------------------------------------------

CREATE PROCEDURE Add_Project
                        (
                        @sProjectName as varchar(100)
                        @dStartDate as datetime
                        )
AS
INSERT Project
            (
            sProjectName
            dStartDate
            )
            VALUES
            (
            @sProjectName
            @dStartDate
            )
return SCOPE_IDENTITY()
GO

----------------------------------------VB6---------------------------------------------------------
Dim oconn As New ADODB.Connection
Dim ocmd As New ADODB.Command

   
oconn.ConnectionString = "Provider=SQLOLEDB.1;Password=4dm1n;Persist Security Info=True;User ID=sa;Initial Catalog=Audit_Master;Data Source=DICK-Nb"
   
    oconn.CursorLocation = adUseClient
   
    oconn.Open
    With ocmd
        .CommandText = "Add_Project"
        .CommandTimeout = 300
        .CommandType = adCmdStoredProc
        .ActiveConnection = oconn
        .Parameters.Append ocmd.CreateParameter("sProjectName", adVarChar, adParamInput, 100, " & Text2.Text & ")
        .Parameters.Append ocmd.CreateParameter("RETURN_VALUE", adInteger, adParamReturnValue, , Null)
    End With
   
    ocmd.Execute
   
    Debug.Print ocmd.Parameters(0).Value
   
    rst.Close
    oconn.Close



Please help me!!!!!
0
Comment
Question by:DickSwart6
  • 2
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 16532253
CREATE PROCEDURE Add_Project
                    (
                    @sProjectName as varchar(100),
                    @dStartDate as datetime
                    )
AS
INSERT Project
          (
          sProjectName
          dStartDate
          )
          VALUES
          (
          @sProjectName
          @dStartDate
          )
return SCOPE_IDENTITY()
GO

----------------------------------------VB6---------------------------------------------------------
Dim oconn As New ADODB.Connection
Dim ocmd As New ADODB.Command

   
oconn.ConnectionString = "Provider=SQLOLEDB.1;Password=4dm1n;Persist Security Info=True;User ID=sa;Initial Catalog=Audit_Master;Data Source=DICK-Nb"
   
    oconn.CursorLocation = adUseClient
   
    oconn.Open
    With ocmd
        set .ActiveConnection = oconn
        .CommandTimeout = 300
        .CommandText = "Add_Project"
        .CommandType = adCmdStoredProc
        .Parameters.Append ocmd.CreateParameter("RETURN_VALUE", adInteger, adParamReturnValue)
        .Parameters.Append ocmd.CreateParameter("sProjectName", adVarChar, adParamInput, 100, " & Text2.Text & ")
        .Parameters.Append ocmd.CreateParameter("sStartDate", adDate, adParamInput, , now)
    End With
   
    ocmd.Execute
   
    Debug.Print ocmd.Parameters("RETURN_VALUE").Value
   
    rst.Close
    oconn.Close

0
 

Author Comment

by:DickSwart6
ID: 16532274
Thanx,

Must you first create the return parameter before you create the input parameter ? and why?
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 16532494
>Must you first create the return parameter before you create the input parameter ?
yes.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

572 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