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
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
whatwhatwhat
  • 1,991
  • 4
  • 31
  • 50
  • Does this answer your question? [Retrieve output parameter (identity column) from stored procedure with vb.net oledbcommand](https://stackoverflow.com/questions/24462957/retrieve-output-parameter-identity-column-from-stored-procedure-with-vb-net-ol) – Thom A Dec 14 '22 at 16:16
  • @Larnu only partially. I would like to read both output parameters, not just one. – whatwhatwhat Dec 14 '22 at 16:27
  • Then add both output parameters to your code; the solution is no different. – Thom A Dec 14 '22 at 16:28
  • @Larnu we're talking about Shell's answer in that link, right? – whatwhatwhat Dec 14 '22 at 16:30
  • I would look at this [answer](https://stackoverflow.com/a/24465923/2029983), which uses parameters. – Thom A Dec 14 '22 at 16:34
  • @Larnu I tried that one but it seems as though the syntax is wrong for the version of Access that I'm using. The first 3 lines show up red for me and some more further in that code. – whatwhatwhat Dec 14 '22 at 16:40
  • 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. – Erik A Dec 14 '22 at 17:31
  • 1
    The `Using` syntax does not exist in VBA. Solution for [DAO](https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/database-openrecordset-method-dao) and [ADODB](https://learn.microsoft.com/en-us/office/client-developer/access/desktop-database-reference/command-object-parameters). – Olivier Jacot-Descombes Dec 14 '22 at 19:18

2 Answers2

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
Erik A
  • 31,639
  • 12
  • 42
  • 67
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
whatwhatwhat
  • 1,991
  • 4
  • 31
  • 50
  • 1
    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. – Erik A Dec 14 '22 at 19:33