Solved

Calling a sql 2008 stored procedure from Ms Access 2010

Posted on 2014-12-05
6
473 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
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 41

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 84
ID: 40483775
Also be sure that you have permissions for the SP and all the tables and such involved in that SP.
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 

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 41

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
The viewer will learn how to use a discrete random variable to simulate the return on an investment over a period of years, create a Monte Carlo simulation using the discrete random variable, and create a graph to represent the possible returns over…
The viewer will learn how to create a normally distributed random variable in Excel, use a normal distribution to simulate the return on an investment over a period of years, Create a Monte Carlo simulation using a normal random variable, and calcul…

786 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