Solved

Calling a sql 2008 stored procedure from Ms Access 2010

Posted on 2014-12-05
6
480 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
[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
6 Comments
 
LVL 66

Assisted Solution

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

Accepted Solution

by:
pcelba earned 250 total points
ID: 40483413
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 85
ID: 40483775
Also be sure that you have permissions for the SP and all the tables and such involved in that SP.
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 

Author Comment

by:downehouse
ID: 40484458
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 42

Expert Comment

by:pcelba
ID: 40484493
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
ID: 40511400
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

627 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