Stored Procedure from Access

  • Thread starter Thread starter scott
  • Start date Start date
S

scott

Is there any way to execute a SPROC residing on the server from an access db
on my workstation? Also, the access front-end isn't an access project, just
a regular database file.

I plan to link to the sql data tables from access, but didn't want to create
a project.

thanks for any help.
 
scott said:
Is there any way to execute a SPROC residing on the server from an
access db on my workstation? Also, the access front-end isn't an
access project, just a regular database file.

I plan to link to the sql data tables from access, but didn't want to
create a project.

Yes. You can do it with a pass-through query, or using an ADO Command
object. As pass-through query is easiest, if you don't need to supply
parameters. Does the procedure in question return records?
 
it doesn't return records, just transfers data.

can you give me some syntax? it's a hard topic to search the web for.
 
scott said:
it doesn't return records, just transfers data.

can you give me some syntax? it's a hard topic to search the web for.

For a pass-through query, you just write the SQL to execute the sproc in
the syntax of the server database. The only trick there is that you
have to compose in SQL View, set the query type to Pass-Through, and set
the query's ODBC Connect Str property to point to the server database.

To use an ADO Command object, you can use code similar to this, cribbed
from one of my applications:

'------ start of code ------
Dim cnn As ADODB.Connection
Dim cmd As ADODB.Command

Dim lngReturnCode As Long
Dim blnUpdateError As Boolean

On Error GoTo Err_Handler

' Open a connection to the back-end database and set up a
' command object to work with it.

Set cnn = New ADODB.Connection
cnn.Open fncGetConnectString()

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

' Set up and execute a call to the server to execute the appropriate
' stored procedure and get the result.

cmd.CommandText = "spInactivatePosition"
cmd.CommandType = adCmdStoredProc
cmd.Parameters.Refresh
cmd.Parameters("@PositionID") = m_lngOriginalPositionID
cmd.Parameters("@EndDate") = Me!txtEndDate
cmd.Parameters("@User") = CurrentUser()

cmd.Execute , , adExecuteNoRecords

' Get the proc's return code from the Command object.
lngReturnCode = cmd.Parameters("@return_value")

If lngReturnCode <> 0 Then
blnUpdateError = True
End If


Exit_Point:
On Error Resume Next
Set cmd = Nothing
If Not cnn Is Nothing Then
cnn.Close
Set cnn = Nothing
End If

' If the update was successful, return to the calling form.
If Not blnUpdateError Then
' Just make this form invisible so that the calling form can
' retrieve data from it.
Me.Visible = False
End If

Exit Sub
'------ end of code ------
 
My problem is that i'm linking to the sql tables. can your code be modified
so a server and login credentials be supplied?

can i use the same asp code i use within access to run a sproc?
 
scott said:
My problem is that i'm linking to the sql tables.

I don't follow you. When you use a pass-through query or execute a
stored procedure via an ADO connection to the server, it doesn't involve
linked tables at all. You have to specify a suitable connect string for
the query, of course, and for the ADO method you have to provide a
Connection object, which will have to be opened with a suitable connect
string.
can your code be
modified so a server and login credentials be supplied?

I'm not sure what you have in mind. I usually use Windows
Authentication for the SQL Server connection, but if you're not going to
do that, I believe you can pass the credentials as part of the connect
string. Are you talking about having your code prompt the user for the
connect string and credentials, and then building the connect string on
the fly?
can i use the same asp code i use within access to run a sproc?

I don't know what you mean. If you're talking about VBScript code from
an ASP application, then the ADO elements will be the same, though you'd
have to change calls to Server.CreateObject to just CreateObject (for
late binding).
 
i don't want it to prompt for credentials. i just have never used a
connection string within access to access a particular sql server.

Below is some sample asp code that runs a SPROC. I'm just trying to do
something just like this, except from within access.

What would a proper access to sql connection string look like?

ASP CODE ******************

sDSN = "Provider=SQLOLEDB.1;Persist Security Info=True;User
ID=user;Password=pwd;Initial Catalog=myDatabase;Data
Source=192.168.1.999;Use Procedure for Prepare=1;Auto Translate=True;Packet
Size=4096;"

Set objConn = CreateObject("ADODB.Connection")
objConn.Open sDSN

Set objRS = Server.CreateObject("ADODB.Recordset")

sSQL2 = "MY_SPROC"
objRS.Open sSQL2, objConn
 
That code should work as is, with no modifications, in Access.

The variable declarations should be:

Dim objConn As ADODB.Connection
Dim objRS As ADODB.Recordset
Dim sDSN As String
Dim sSQL2 As String

although you could get away with:


Dim objConn As Object
Dim objRS As Object
Dim sDSN As String
Dim sSQL2 As String
 
Douglas J Steele said:
That code should work as is, with no modifications, in Access.

Except that ...

.... must be changed to

Set objRS = CreateObject("ADODB.Recordset")
 
Back
Top