Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Calling a sql 2008 stored procedure from Ms Access 2010

Posted on 2014-12-05
6
Medium Priority
?
488 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 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

Technology Partners: 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!

Question has a verified solution.

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

What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
Ready to get certified? Check out some courses that help you prepare for third-party exams.
Viewers will learn how the fundamental information of how to create a table.
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

782 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