简体   繁体   中英

How do I run a stored procedure and use its results in MS Access VBA?

I have a simple stored procedure that takes one parameter and has two output parameters. I want to call this stored procedure in Access VB and read the 2 output parameters to use them in the VB code. How do I do this?

ALTER PROCEDURE [dbo].[spTest]
    @Input1 nvarchar(100),
    @Output1 int OUTPUT,
    @Output2 varchar(100) OUTPUT
AS
BEGIN
    SET @Output1 = 30
    SET @Output2 = 'OK'

    RETURN 0
END

This is the closest thing to what I'm looking for, but when I try to paste it into Access VBA it highlights a bunch of lines in red as if the syntax was way off.

Using connection As New System.Data.SqlClient.SqlConnection(connectionstrng) 'Error here
    connection.Open() 'Error here
    Using command As New System.Data.SqlClient.SqlCommand("sp_Custom_InsertxRef", connection) 'Error here
        command.CommandType = CommandType.StoredProcedure
        command.Parameters.Add("@DocumentID", SqlDbType.Int, 4, ParameterDirection.Input).Value = epdmParDoc.ID
        command.Parameters.Add("@RevNr", SqlDbType.Int, 4, ParameterDirection.Input).Value = epdmParDoc.GetLocalVersionNo(parFolderId)
        command.Parameters.Add("@xRefDocument", SqlDbType.Int, 4, ParameterDirection.Input).Value = targetReplaceDoc.ID
        command.Parameters.Add("@xRefRevNr", SqlDbType.Int, 4, ParameterDirection.Input).Value = targetReplaceDoc.CurrentVersion
        command.Parameters.Add("@xRefProjectId", SqlDbType.Int, 4, ParameterDirection.Input).Value = parFolderId
        command.Parameters.Add("@RefCount", SqlDbType.Int, 4, ParameterDirection.Input).Value = count
        'command.Parameters.Add("@xRef", OleDbType.Integer, 4, ParameterDirection.InputOutput).Value = -1
        command.Parameters.Add("@xRef", SqlDbType.Int) 'Error here
        command.Parameters("@xRef").Direction = ParameterDirection.Output
        command.ExecuteReader() 'Error here
        xRefId = command.Parameters("@xRef").Value
    End Using 'Error here
    connection.Close() 'Error here
End Using

You can use ADO to work with stored procedures, including output parameters.

You can be explicit about parameters, but ADO can also pull them from SQL server, which is nice.

Dim conn As New ADODB.Connection
conn.ConnectionString = "Provider=SQLNCLI11;Server=localhost;Database=example;Trusted_Connection=yes;"
conn.Open 'Open connection
Dim cmd As New ADODB.Command 'Declare a command object
Set cmd.ActiveConnection = conn 'Connect it to SQL server
cmd.CommandType = adCmdStoredProc 'It's a SP
cmd.CommandText = "spTest" 'And this is its name
cmd.Parameters.Refresh 'Get parameters from SQL server, you could manually declare them as well
cmd.Parameters("@Input1").Value = "High value!" 'Now parameters can be referenced by name
cmd.Execute 'Execute the stored procedure
Debug.Print cmd.Parameters("@output").Value 'And you can get the output values

Erik's answer probably works as well, but I looked through some boilerplate code in the environment I'm working in and found this example, which works:

   Dim con As ADODB.connection, cmd As New ADODB.Command, prm As New ADODB.Parameter

   cmd.ActiveConnection = con
   cmd.CommandType = adCmdStoredProc
   cmd.CommandText = "spStoredProcedureName"

   Set prm = cmd.CreateParameter("@Input1", adVarChar, adParamInput, 10, strInput1)
   cmd.Parameters.Append prm
   Set prm = cmd.CreateParameter("@Output1", adVarChar, adParamOutput, 100)
   cmd.Parameters.Append prm
   Set prm = cmd.CreateParameter("@Output2", adVarChar, adParamOutput, 100)
   cmd.Parameters.Append prm

   cmd.Execute
   
   Select Case cmd.Parameters("@Output2")
      Case "OK"
         'run some code...
      Case Else
         'run some other code...
   End Select

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM