Solved

Calling a sql 2008 stored procedure from Ms Access 2010

Posted on 2014-12-05
6
470 Views
Last Modified: 2014-12-21
The code below stops at the execute, it says it cannot find the sp, (suggests wrong spleelling.. but it is not..) it runs fine in SQL Server Management
There is an System DSN ODBC in place(Both 32 and 64 bit). Running Win 8.1 64 bit, with MS Office - MS Access 2010 on 32 bit (long story)

This is the first time I have used sp's from MS Access so might well be a rookie mistake. Any steer in the right direction most welcome..
Private Sub cmdProc_Click()
    

    Dim rst As ADODB.Recordset
    Dim cmd As ADODB.Command
    Dim stProcName As String    'Stored Procedure name
    Dim cnt As ADODB.Connection
    'Declare variables for Stored Procedure
    Dim myVariable As Variant
    Dim myReturn As String
    
    'Set ADODB requirements
   
    Set rst = New ADODB.Recordset
    Set cmd = New ADODB.Command
    
    ' Defines the stored procedure commands
    stProcName = "Staff_Login"                'Define name of Stored Procedure to execute."
    cmd.CommandType = adCmdStoredProc           'Define the ADODB command
    cmd.ActiveConnection = CurrentProject.Connection              'Set the command connection string
    cmd.CommandText = stProcName                'Define Stored Procedure to run
    
    'Execute stored procedure and return to a recordset
    Set rst = cmd.Execute()
    'myReturn = rst.Fields("procedure_name").Value
    myReturn = rst.Fields("StaffID").Value
    
    'Call Sub-Routine_That_Uses_The_Returned_Data
    MsgBox (myReturn)
    
    'Close database connection and clean up
    If CBool(rst.State And adStateOpen) = True Then rst.Close
    Set rst = Nothing
    
    If CBool(cnt.State And adStateOpen) = True Then cnt.Close
    Set cnt = Nothing

End Sub

Open in new window

0
Comment
Question by:downehouse
6 Comments
 
LVL 65

Assisted Solution

by:Jim Horn
Jim Horn earned 250 total points
Comment Utility
I just noticed that there's a video tutorial on Executing a SQL Server Function from Within Access that may help you.
0
 
LVL 41

Accepted Solution

by:
pcelba earned 250 total points
Comment Utility
Are you sure you are in the right database? Did you try to execute some simple query?

You may try to fully qualify the SP:

"[databaseName].[dbo].[Staff_Login]"  (suppose dbo schema is used)
0
 
LVL 84
Comment Utility
Also be sure that you have permissions for the SP and all the tables and such involved in that SP.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:downehouse
Comment Utility
Hi, Video helped a bit as it confirmed the apprach is correct. Have verified the connection is good with simple query just before 'Execute'.  Fully qualifing sp makes no diffrence, I have full rights as Group Admin.
Is this just a syntax issue??
0
 
LVL 41

Expert Comment

by:pcelba
Comment Utility
Try to execute the SP as a standard command:
    'cmd.CommandType = adCmdStoredProc   'default = Text   
cmd.ActiveConnection = CurrentProject.Connection       
cmd.CommandText = "EXEC " + stProcName    

Open in new window

If you are in the right database and if you have all access rights then the SP name is not correct.
BTW, does the SP have some parameters?

You could also test your code on some system SP, e.g.  sp_who
0
 

Author Comment

by:downehouse
Comment Utility
Thanks for all the help, I decided to give up on Access and use vb.net... works fine. Sorry for delay in getting back.. been away. This call can now be closed.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Correct ordering 14 43
Help with SQL Query 23 39
Group by and order by clause 28 35
Another multiple criteria puzzle 4 18
The System Center Operations Manager 2012, known as SCOM, is a part of the Microsoft system center product that provides the user with infrastructure monitoring and application performance monitoring. SCOM monitors:   Windows or UNIX/LinuxNetwo…
The new Microsoft OS looks great, is easier than ever to upgrade to, it is even free.  So what's the catch?  If you don't change the privacy settings, Microsoft will, in accordance with the (EULA) you clicked okay to without reading, collect all the…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now