Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
How to Run Stored Procedure
David Britz 
    
3 years ago
I'm having the hardest time trying to run VBA to execute a stored procedure that returns a value.

Here is the code execution that runs fine in SQL Server:
DECLARE @return_value int, @newWOID int EXEC @return_value = [dbo].[spAddNewWorkOrder] @takenBy = 99, @newWOID = @newWOID OUTPUT

This is the ExecuteScalar that I'm using:

Public Function ExecScalar(sSQL As String, Optional strConnString As String = "") As Variant



    ' Find out if the attempt to connect worked.
    Dim cnn As ADODB.Connection
    Dim rs As ADODB.Recordset
    
    Set cnn = New ADODB.Connection
    
    cnn.Open
    If cnn.State = adStateOpen Then
        'Dim rs As New ADODB.Recordset
        Set rs = cnn.Execute(sSQL)
        If Not rs.EOF Then
            ExecScalar = rs(0) 'This procedure assumes that in an execscalar situation, the stored procedure is only returning one value in one field
        Else
            ExecScalar = 0
        End If
        rs.Close
        Set rs = Nothing
    Else
        MsgBox "ExecScalar Error;" & sSQL
    End If



    ' Close the connection.
    'cnn.Close
End Function


After the line "If not rs.EOF then" the procedure kicks back out to where it's called, and the return value is 0. Am I doing something wrong in how I call the stored procedure from Access?
Gregory Clancey  @Reply  
    
3 years ago
I own that I know nothing (yet) about the SQL Server syntax, but are you looking to do something that, perhaps, VBA can best accomplish with an Action Query? The VBA method for encoding and executing such an action is among the simplest code to write. Much less complicated (I'm thinking) than your more advanced Server code.
Kevin Yip  @Reply  
     
3 years ago
Where are the connection parameters?  You create a new connection, so it should have a connection string or arguments specified.  E.g.:

     Dim cnn As ADODB.Connection
     cnn.Open "databasename", "uid", "pwd"

Other than that, your code should work. If it doesn't work, the problem lies elsewhere.  As an alternative, try using a passthrough query to run the stored procedure (see picture below).  Set the connection string in the property sheet as shown, and set "Return records" to Yes if you expect a return.
Kevin Yip  @Reply  
     
3 years ago

David Britz OP  @Reply  
    
3 years ago
Thank you, I'll try the connection information but I used the ADO connection string for that and then put the SQL code in the execute function.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Forum.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 5/6/2026 11:21:04 AM. PLT: 0s