?
Solved

VBA Code to handle Output Parameter from Stored Procedure

Posted on 2003-03-07
3
Medium Priority
?
566 Views
Last Modified: 2008-02-01
I am using Access 2002 with an ADP project. Using VBA, I need to know how to write the code that correctly retrieves the output parameter from a SQL Server 2000 stored procedure. I have tried the following, which does not work:

Function GetSQLServerUsername() As Variant
Dim cmd As ADODB.Command
Dim prm As ADODB.Parameter

    Set cmd = New ADODB.Command
    Set prm = New ADODB.Parameter
    With cmd
        .ActiveConnection = CurrentProject.AccessConnection
        .CommandType = adCmdStoredProc
        .CommandText = "sprocGetUsername"
    End With
    With prm
        .Direction = adParamOutput
        .Name = "@Uname"
        .Type = adWChar
        .Size = 20
    End With
    cmd.Parameters.Append prm
    GetSQLServerUsername = cmd.Execute   '<<<<<<<<<<< DIES HERE

End Function

I get the following error from VBA:

Run-time error 450
Wrong number of arguments or invalid property assignment

The stored procedure I am calling is this:

CREATE PROCEDURE sprocGetUsername
     @Username nchar(20) OUTPUT
AS
     SET @Username = SUSER_SName()
GO

0
Comment
Question by:rgrimm
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 26

Accepted Solution

by:
Alan Warren earned 2000 total points
ID: 8092286
Function GetSQLServerUsername() As Variant
Dim cmd As ADODB.Command
Dim prm As ADODB.Parameter

   Set cmd = New ADODB.Command
   Set prm = New ADODB.Parameter
   With cmd
       .ActiveConnection = CurrentProject.AccessConnection
       .CommandType = adCmdStoredProc
       .CommandText = "sprocGetUsername"
   End With
   With prm
       .Direction = adParamOutput
       .Name = "@Uname"
       .Type = adWChar
       .Size = 20
   End With
   cmd.Parameters.Append prm
   GetSQLServerUsername = cmd.Execute   '<<<<<<<<<<< DIES HERE

End Function


' ok
.Parameters.Append .CreateParameter("@Uname", adWChar, adParamOutput, 20)

' use this
GetSQLServerUsername = .Parameters("@Uname").Value

also tell ado not to expect a recordset

.Execute , , adExecuteNoRecords


hth
alan@cashoz.com
http://www.cashoz.com

0
 

Author Comment

by:rgrimm
ID: 8092891
Alan, excellent! Below is the Final Solution that worked. I could not find a clearly documented way of handling an output parameter from a SQL Server stored procedure in my many books and online resources. This does it about as cleanly as can be expected.

Function GetSQLServerUsername() As Variant
Dim cmd As ADODB.Command

    Set cmd = New ADODB.Command
    With cmd
        .ActiveConnection = CurrentProject.AccessConnection
        .CommandType = adCmdStoredProc
        .CommandText = "sprocGetUsername"
        .Parameters.Append .CreateParameter("@Uname", adWChar, adParamOutput, 20)
        .Execute , , adExecuteNoRecords
    End With
    GetSQLServerUsername = cmd.Parameters("@Uname").Value

End Function
0
 
LVL 26

Expert Comment

by:Alan Warren
ID: 8149300
Thanks rgrimm!

Regards Alan

Applications Development
Cash Australia (Int) Pty Ltd
http://www.cashoz.com
MailTo: alan@cashoz.com
 
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

801 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