Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


Calling SQL Server Stored Procedures using DAO

Posted on 1998-08-06
Medium Priority
Last Modified: 2008-02-01
I'm trying to call a stored procedure using DAO on a SQL server.  I've seen and tried examples for RDO but i don't know what that is and i can't get it to work either.

I have no problems sending queries using OpenRecordset and getting the results that way, so i know i'm connecting properly.

I need a detailed description of how to connect to a SQL server database, call a stored procedure and have it return results to me.  i also need to be able to send data to the stored procedure that updates records and doesnt return results.  and even more if the stored procedure is made to send an error message if something happens i need it to be able to send that back to me rather then the 'ODBC -- connection failed' error.

Question by:b0b080698
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
  • 4
  • 3

Expert Comment

ID: 1468198
Hi there,

You say that standard queries work ok?  Well, rather than going through all the routine of explaining how to connect i'll try to give you some ideas as to why your stored procedures aren't working - if you're still having problems after checking these things then i'll be only too happy to explain connection issues, including RDO if necessary.

You can call a stored procedure in exactly the same way as you'd execute a normal query - for example, if you had a stored procedures called MyQuery you would simply do the following:

             Set MyData = db.OpenRecordset("MyQuery",dbOpenSnapShot,dbSQLPassThrough)

The above line will return a read-only recordset although you could change it to a read/write data set by simply using dbOpenDynaSet instead of dbOpenSnapShot.  We ALWAYS use dbSQLPassThrough at the place where I work when executing any queries on SQL Server.

Finally you'll need to make sure you've got priviledges to execute the stored procedure.  These rights will need allocating to the username you use to access the database tables.

Hope this gives you some ideas, let me know if you want to know anything else - i'm quite new to SQL Server myself but my colleague is something of an expert on it....


Author Comment

ID: 1468199
thanx.  that seems to have worked.  but i need a little more power then just retreiving record sets.  how do i make it so that i can send values to the the stored procedure?  how can i make it so that if the stored procedure dies i get the stored procedure error rather then just the 'ODBC -- connection failed error.'?

i think i'll ammend my question up top.

Author Comment

ID: 1468200
Adjusted points to 150
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 1468201
Edited text of question

Accepted Solution

peterwest earned 450 total points
ID: 1468202
Hi again,

Right then - the first issue - how to pass parameters to the stored procedure!!

Just do the following:

      CREATE PROCEDURE [ProcedureName] @MyParam varchar(20) AS

The above example accepts a string parameter of a maximum of 20 characters in length - you'd execute this procedure as described before but simply add the string parameter in quotes after the procedure name.  You can add further parameters by simply putting a comma after the first param and then specifying the next param name and data type.

Don't forget that you'll need to reassign rights for the procedure once you've recreated it - I usually use the command:

        GRANT EXECUTE ON [ProcedureName] TO [UserName]

On the issue of error reporting from stored procedures; well, depending on what version of VB you are using there are a number of ways you can determine if a stored procedure has failed and if so why....

Both VB4 and 5 produce a collection of errors when a DAO error occurs.  To allocate the description of one of the errors to a variable you can just do the following:

         MyError = Errors(0).Description

And simply increase the index by 1 to view all the different errors, typically the last error is the one displayed by VB and also the least informative.  I tested this method with a stored procedure and it did indeed return the error that I expected.

Anyway, I hope that is of help to you - anything else you want to know then just let me know.



Author Comment

ID: 1468203

i got the error thing figured out.  i wrote a stored procedure that purpously produced an error.  i didnt have an error handler so the only thing i ever got was 'ODBC -- connection failed' which isnt really what happend because the connection worked fine.  it executed the procedure got the error and kicked out.  oh well.  i think i'll be fine from here.  thanx!  :P

Expert Comment

ID: 1468204
No problem - if you've got any further problems that you can always drop me a line (peterjwest@yahoo.com)



Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

618 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