views:

538

answers:

1

I have a large VB program that connects to Oracle database.

strCn = "Driver={Microsoft ODBC for Oracle};" & _
        "SERVER=PSPROD;"

Set Cn = New ADODB.Connection
Cn.ConnectionString = strCn
Cn.CursorLocation = adUseNone
Cn.Open

There are many users of my program so I have a table that contains each user's login name and their access rights to the various tables. I create a recordset of all users when the program is started and then select USERNAME and GRANTED_ROLE from the record set where USERNAME and PASSWORD are found. I use a "Set role 'GRANTED_ROLE' identified by 'password'" statment and Cn.Execute statement to set up the user's access rights. This is all done in a Module.

On a form, I want to call a Stored Procedure that will SELECT, INSERT and UPDATE information into another schema's tables. I am able to call and run the stored procedure when I create a new connection to the database with this code:

Dim cmd5040 As ADODB.Command Dim conn5040 As ADODB.Connection Dim param5040 As ADODB.Parameter

Set conn5040 = New ADODB.Connection conn5040 = "Driver={Microsoft ODBC for Oracle};" & _ "SERVER=PSPROD; UID=XXXXXXX; PWD=XXXXXXXX" conn5040.Open

Set cmd5040 = New ADODB.Command

With cmd5040 .ActiveConnection = conn5040 .CommandType = adCmdStoredProc .CommandText = "S4115040_IMPORT_NEWBIDITEMSPES.S4115040_CheckTime"

.Parameters.Append .CreateParameter(, adInteger, adParamInputOutput, 5)
.Parameters.Append .CreateParameter(, adVarChar, adParamInputOutput, 400)

End With

cmd5040(0) = 0 cmd5040(1) = "" cmd5040.CommandTimeout = 300

cmd5040.Execute conn5040.Close

However, I get the error message "-2147217900 [Microsoft][ODCB driver for Oracle]Syntax error or access violation" when I attempt to use the same connection ('Cn') when the program first started. My code is:

Dim cmd5040 As ADODB.Command Dim param5040 As ADODB.Parameter

Set cmd5040 = New ADODB.Command

With cmd5040 .ActiveConnection = Cn .CommandType = adCmdStoredProc .CommandText = "S4115040_IMPORT_NEWBIDITEMSPES.S4115040_CheckTime"

.Parameters.Append .CreateParameter(, adInteger, adParamInputOutput, 5)
.Parameters.Append .CreateParameter(, adVarChar, adParamInputOutput, 400)

End With

cmd5040(0) = 0 cmd5040(1) = ""

cmd5040.Execute

I have worked with my DBA. She has given me direct grants and direct execute privliges and I am still get the error message.

What am I doing wrong? Should I be able to use the original connection to run a stored procedure? Or must I create a second connection?

A: 

edit: on reviewing your code, I notice that the original connection Cn specifies the driver and the server name whereas the second connection conn5040 specifies the driver, server name, user and password.

Therefore, it may be that the stored procedure you are calling requires a user and password which the original cn connection does not specify


Original answer:

Make sure that the variable cn is still in scope when you try to use it. If it is declared in a module then it should be declared outside of any Sub or Function and, if other modules should be able to access it, it should be declared as Public

Option Explicit

Public cn as ADODB.Connection

Sub foo()
...

Presuming that cn is still in scope, you could examine the State property of the object which cn references to see if the Connection is still open.

If (cn.State = adStateClosed) Then
    ' we have a problem
    ...
barrowc
The Cn connection is declared as Public Just prior to calling the stored procedure I checked the state of Cn and it is open.
Jan
Edited the answer - the connection strings for the two connections are different and that may be the issue.
barrowc
You are correct. Cn makes its connection some how in a two-step process looking up the user name and their roles to grant privileges to the tables. The second connection makes the direct connection in one process using a 'generic' user name rather than the individual's user name.Boss has consented to use the second connection. The use of the second connection will not be often and the length of time it will be open will be very short.Thank you for your attention to my question!!
Jan