0

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
8
  • Does this answer your question? Retrieve output parameter (identity column) from stored procedure with vb.net oledbcommand Commented Dec 14, 2022 at 16:16
  • @Larnu only partially. I would like to read both output parameters, not just one. Commented Dec 14, 2022 at 16:27
  • Then add both output parameters to your code; the solution is no different. Commented Dec 14, 2022 at 16:28
  • 4
    @Larnu Suggesting VB.Net answers for a question asking about VBA is not helpful. Those are using SqlClient or ADO.Net, neither are available in VBA. Commented Dec 14, 2022 at 17:31
  • 1
    The Using syntax does not exist in VBA. Solution for DAO and ADODB. Commented Dec 14, 2022 at 19:18

2 Answers 2

3

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
Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

Yup, that's the "explicit" option I've referenced where you manually declare the types/sizes/etc. Works as well, just requires a bit more fiddling.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.