Question : Calling Stored Procedure from AfterInsert

I want to call a SProc with the forms afterinsert() event.  I found the code below and it gives an error saying: Run time error '-2147217865
The Microsoft Jet Database Engine cannot find the input table or query 'NewCaseInsertDocs'.
Make sure it exists and that it's name is spelled correctly.

I double checked the name, and its correct.  

whats up?
Code Snippet:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
Private Sub Form_AfterInsert()
 
  Dim cmd As ADODB.Command
  Dim rs As ADODB.Recordset
 
  Set cmd = New ADODB.Command
  Set cmd.ActiveConnection = CurrentProject.Connection
 
  cmd.CommandText = "NewCaseInsertDocs"
  cmd.CommandType = adCmdStoredProc
 
  'Set the parameters
 
  Set rs = New ADODB.Recordset
  Set rs = cmd.Execute
  
 
 
 
End Sub

Answer : Calling Stored Procedure from AfterInsert

Yeah - this was my first hangout... not around here so much these days :-)

So you're just using an Access MDB application with linked tables?
Though the linked tables may seem to offer implied connectivity to the SQL database, they do so no more than the explicit connection to each individual table (based on that linked table's ODBC connection string).
Your CurrentProject.Connection object still points to the currently running instance of your Access FE application.
(Don't let the fact that it's an ADO object confuse the issue for you - ADO works perfectly well against Access/Jet too ;-)

 Dim cnn As ADODB.Connection
 Dim cmd As ADODB.Command
 Dim rs As ADODB.Recordset

 Set cnn = New ADODB.Connection
 cnn.Open "Your Connection String"

 Set cmd = New ADODB.Command
 Set cmd.ActiveConnection = cnn

 cmd.CommandText = "NewCaseInsertDocs"
 cmd.CommandType = adCmdStoredProc

 'Set the parameters

 Set rs = New ADODB.Recordset
 rs.Open cmd,  , adOpenKeyset, adLockOptimistic
 

Where "Your Connection String" is obviously formed based on your own setup - similar even to your linked table connectionstring if you like.  You'll have built them often enough in your ASP.NET apps no doubt.

Note the change to adOpenKeyset - as that's the cursor type you'd get back anyway with the given code.  (Not static).
If all you wanted was a forward only, read only recordset - then the original code you had would be fine.
i.e. Set rs = cmd.Execute
Note also that with such an execution - there's no need to instantiate the recordset first.

Random Solutions  
 
programming4us programming4us