Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calling a sql 2008 stored procedure from Ms Access 2010

Posted on 2014-12-05
6
Medium Priority
?
485 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 750 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 43

Accepted Solution

by:
pcelba earned 750 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
Does Your Cloud Backup Use Blockchain Technology?

Blockchain technology has already revolutionized finance thanks to Bitcoin. Now it's disrupting other areas, including the realm of data protection. Learn how blockchain is now being used to authenticate backup files and keep them safe from hackers.

 

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 43

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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

721 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