views:

212

answers:

1

I have an Access 2003 database that connects to a SQL Server 2008 box via ODBC. The tables from SQL Server are connected as linked tables in Access. I have a stored procedure on the SQL Server that I am trying to execute via ADO code. The problem I have is that Access cannot seem to find the procedure. What do I have to do within Access to be able to execute this stored procedure? Some facts ...

The stored procedure in question accepts one parameter which is an integer. The stored procedure returns a recordset which I am hoping to use as the datasource for a ListBox.

Here is my ADO code in Access ...

Private Sub LoadUserCaseList(userID As Integer)

  Dim cmd As ADODB.Command

  Set cmd = New ADODB.Command
  cmd.ActiveConnection = CurrentProject.Connection
  cmd.CommandType = adCmdStoredProc
  cmd.CommandText = "uspGetUserCaseSummaryList"

  Dim par As New ADODB.Parameter
  Set par = cmd.CreateParameter("userID", adInteger)
  cmd.Parameters.Append par
  cmd.Parameters("userID") = userID

  Dim rs As ADODB.Recordset
  Set rs = cmd.Execute()
  lstUserCases.Recordset = rs

End Sub

The error I get is "the microsoft jet database engine cannot find the input table or query "uspGetUserCaseSummaryList".

+2  A: 

CurrentProject.Connection is the connection to your Access database. You can verify that by doing this in the Immediate window:

Debug.Print CurrentProject.Connection

You need to create a new ADODB.Connection object with a connection string which points to your SQL Server instance. Have your ADODB.Command object use that connection.

Edit: You can eliminate the ADODB.Command object, and use the Execute method of the connection to return records from your stored procedure. This example uses a stored procedure which expects 3 parameters.

Private Sub GetCenterCodes()
    Dim cnn As ADODB.Connection
    Dim rs As ADODB.Recordset

    Set cnn = New ADODB.Connection
    cnn.ConnectionString = "Provider=SQLOLEDB;Data Source=VM2003\sqlexpress;" _
        & "User ID=foo;Password=bar;Initial Catalog=Inventory"
    cnn.Open
    Set rs = New ADODB.Recordset
    Set rs = cnn.Execute("EXEC uspGetCenterCodes 14, 14, 501")
    Debug.Print rs(0), rs(1), rs(2)
    rs.Close
    Set rs = Nothing
    cnn.Close
    Set cnn = Nothing
End Sub

You can find a connection string example which matches your needs at ConnectionStrings.com

HansUp
Excellent answer! Thank you very much. One question. How would you suggest storing the connection string info to the SQL Server so that changes only need to be made in one place? One thing I like about the linked tables is I have a macro named "Refresh Connection" that calls a subroutine that resets the ODBC connection info for all the tables (switch between test and production environments). It would be great if I could do the same thing for the ADO connection information without having users go into the code.
webworm
Assuming you have connection properties stored in a table, you could create separate records for the test and prod connection properties. Have your Refresh Connection macro mark the appropriate record as active. Then create a function, GetConnectionString, which reads the active connection record and builds your connection string. Then the code above could be changed to cnn.ConnectionString = GetConnectionString. BTW, you may prefer to use a trusted connection so you can avoid dealing with User ID and Password.
HansUp
Thank you again. Great solution.
webworm
It might be more convenient to store the entire connection string as one field in your connections table. Then your GetConnectionString function could just retrieve the entire stored string.
HansUp
I was thinking that I might have a single table that is kept local to the MDB file and in that table I would have several connections strings. One for Production, one for Testing and then I would create two different macros, "Connect to Production" and "Connect to Test". Each macro would mark as active the appropriate connection string where it could then be read by any code that needs to make a connection.
webworm
Gotcha. I was also thinking the connection strings must be in a local table so you can still get at them if your links to the server get hosed. I'm not proficient with macros, but if you attack this with VBA code, I would be interested to see it. Best Wishes.
HansUp
In fact all I was using the macro for was to execute the VBA code :) Thanks again for your insight and help.
webworm