MS-Access ODBC Connection to Oracle for SQL - oracle

I'm trying to connect to an Oracle Database with Access 2003.
I want to use an ODBC connection and I want to set it up so that the user doesn't need to enter a password.
One of the problems I am having though is my sql query uses INTERFACE.Products and Access sees the period and thinks I'm trying to opening a file. IE Interface.MDB, when thats a part of my sql query.
Option Compare Database
Function OracleConnect() As Boolean
Dim ws As Workspace
Dim db As Database
Dim LConnect As String
Dim myQuery As String
Dim myRS As Recordset
On Error GoTo Err_Execute
LConnect = "ODBC;DSN=Oracle;UID=user;PWD=password;"
'Point to the current workspace
Set ws = DBEngine.Workspaces(0)
'Connect to Oracle
Set db = ws.OpenDatabase("", False, True, LConnect)
myQuery = "Select * from INTERFACE.Products"
Set rst = db.OpenRecordset(myQuery)
rst.Close
db.Close
Exit Function
Err_Execute:
MsgBox MsgBox("Error Number: " & Err.Number & " Message: " & Err.Description)
End Function

Could you try something like:
Set rst = db.OpenRecordset(mQuery, dbOpenSnapshot, dbSQLPassThrough)
This would just send the query verbatim to the Oracle database.
In the Access documentation for OpenRecordset you'll find this note:
If you open a Recordset in a Microsoft Access workspace and you don't specify a type, OpenRecordset creates a table-type Recordset.
This may be the source of the error you're getting.
As a rule, I would suggest to always be explicit in your parameters to OpenRecordset: the default behaviour isn't always consistent and it can generate strange errors.
I'm not sure about your design, but one way to make things a bit easier for you may be to simply create linked tables in Access.
In that case, you would be able to write queries against your INTERFACE.Products Oracle table as if if were a local Products table.
It would also save you from having to manage the connections yourself.

The recordset you are creating must use a query written with Access SQL. The table must be an Access table (a table defined in the mdb). It can be a local table or linked table. An Access table cannot have a "." (dot) in its name. If you were to link the Oracle table INTERFACE.Products in Access it would be INTERFACE_Products (Access will replace illegal characters in the remote database table name with "_") unless you renamed it in Access. Thus your query should be "Select * from INTERFACE_Products"
It is possible to create a recordset using Oracle SQL, but that must be created with a Pass-Through query.

Related

Connecting Excel VBA to oracle DB using 'ODBC' (continue of 37339682)

This question is a continuation of question 37339682: Connecting Excel VBA to oracle DB using 'ODBC'
In the above question, there is a step by step guide to connect Excel to ODBC. (Oracle database) I have successful connected to the Oracle database and retrieve a table.
My questions are:
(1) How to write Excel VBA code, such that it can read SQL query from a cell, and return the SQL query result in a table. I guess I need to setup a "database object" in the Excel VBA code.
(2) If such code are to be performed in Microsoft Access. How different would it be?
Not a database object but a recordset object
Sub Example
Dim rs as New Recordset 'the new is important
Dim Conn as New Connection
Conn.connectionstring = "whatever"
'see here https://www.connectionstrings.com/microsoft-odbc-for-oracle/
Conn.open
Dim sql as string
sql = "whatever" 'could be text in cell ie sql = range("g1").text
rs.open sql,conn
if not rs.eof then 'if there's any data then
Activesheet.range("a2").copyfromrecordset rs 'puts data into cell
End If
rs.close
conn.close
end sub

vb6 oracle9i application, logical error-recordset remains empty

Currently I am working on a project that forces me to use Vb6 as a front end and Oracle 9i as a backend in my application. I need to retrieve data from a table in oracle, and display it in a VB6 form. Im using a recordset for this, but for some reason it doesnt contain any record even when a valid record is present in the table in question. Can anyone tell me what's wrong? Any help would be appreciated. My code is as follows-
Private Sub Command1_Click()
Dim sql As String
Set rs = New ADODB.Recordset
sql = "select test23.phoname from test23 where test23.ops='" + Text1.Text + "'"
rs.Open sql, con, adOpenStatic, adLockOptimistic
If rs.EOF = False Then
Form7.Show
Form7.Label2.Caption = rs.Fields("phoname")
End If
End Sub
The if statements never execute, as the recordset always seems empty. If I remove the EOF condition I get a runtime error 3021. Is there something wrong with my sql query? The table test23 is already present in my oracle database with attributes serialno, phoname and ops.
Thanks-
Ron
I have struggled through this myself some years ago. The problem could be in several areas: specificaly the connection, the Oracle drivers, the Oracle home or the query itself.
I would strongly recommend that you download the free version of TOAD which will allow you to test both your connection (I assume ODBC of some sort) and you can then execute the query manually using TOAD.
Are you getting any exceptions?
Hopefully from this you can then correct your code/connection/query.
Good luck
I would also point out that adoOpenStatic and adoLockOptimistic are mutually exclusive property values, as optimistic locking only applies to updatable recordsets and static recordsets aren't updatable (except in batch mode with adoLockBatchOptimistic). This may be confusing the Oracle OLEDB provider somehow.

error in connection string in vb6.0

Dim rs As New ADODB.Recordset
strConnectionString = "Provider=SQLOLEDB.1;Integrated Security=True;Initial Catalog=Hospital_ABC;Data Source=ITEM-47791\SQLEXPRESS;Persyst Security Info=False;Command Properties='Command Time Out=45'"
Set connexion = New ADODB.Connection
connexion.ConnectionString = strConnectionString
connexion.ConnectionTimeout = 30
connexion.Open strConnectionString
rs.Open "insert into student_info(name,rollNo,age, class,address,assigned_teacher)values('name',rollno,age,'cls','add','assteac')", connexion
i am using this code to insert value inn my table ,,,but i got error as--run time error( multiple step OLEDB operation generated error, check each OLEDB status value, if available)
please check it what wrong in thos code .....
Look at this:
http://support.microsoft.com/kb/269495
It says:
The following are two possible causes
of this error:
In the registry, under the key for an OLE DB provider's CLSID, there may
be an entry named OLEDB_SERVICES. If
the OLE DB provider that is used to
make the ADO connection does not have
the OLEDB_SERVICES entry, and ADO
tries to set up a property that is not
supported by the provider, the error
occurs. For more information about
this registry entry, see the
"Resolution" section.
If OLEDB_SERVICES entry exists but there is a problem in the ADO
connection string, the error occurs.
Do not use this in connection string
Persyst Security Info=False;Command Properties='Command Time Out=45'
try this:
rs.Open "insert into student_info(name,rollNo,age,class,address,assigned_teacher) values('n',1,1,'c','a','a')", connexion
For me, the error usually happens when a data is too long for the field. You can further troubleshoot this by updating one field at a time to find the one that is broken (Update name, then update name and number, then name and number and age, etc).

assign table values to recordset and update with another recordset using vb6

I want to assign database table columns to recordset in vb6. And i want update them with the values that i have in my another recordset. Is that possible?
How to assign the data from table to recordset?
An ADODB recordset is not a mirror of a database table. The recordset contains whatever you want it to based on the query you provide it. So to load the data from the database into the recordset you need to execute a query. This can be done in two ways.
Use the ADODB.Connection.Execute method and set your recordset to the result.
Dim con as New ADODB.Connection
Dim rs as ADODB.Recordset
con.ConnectionString = "some connection string"
con.Open
Set rs = con.Execute("SELECT * FROM table")
Create an ADODB.Recordset object, specify the connection and then call the Open method passing it a query.
Dim con as New ADODB.Connection
Dim rs as New ADODB.Recordset
con.ConnectionString = "some connection string"
con.Open
Set rs.ActiveConnection = con
rs.Open "SELECT * FROM table"
The query can be as simple or complex as you want it be. The query could ask for fields from multiple tables and the recordset will still contain the results of the query, however you won't be able to tell which table the fields are from.
A fabricated ADODB Recordset object is a fine container object because it has some great methods built in: Filter, Sort, GetRows, GetString, Clone, etc plus support for paging, serializing as XML, etc. For details see "Adding Fields to a Recordset" in this MSDN article.
But if you are working with database data, why not just execute a query?

Call a parameterized Oracle query from ADODB in Classic ASP

I’m currently working on a classic ASP project talking to an Oracle database. I’m trying to find a way to safely call an Oracle PL/SQL script and passing parameters with ADO. The currently solution builds the SQL script by hand with embedded variables like this:
strSQL = "SELECT field1, etc FROM my_table WHERE (field = '" & filter_value & "')"
This, of course, is ugly and insecure, and open to abuse.
The code that I have so far (purloined from various non classic asp based web sites) looks like this:
dim strSQL, oConn, oCommand, oParam
set oConn = server.createobject("ADODB.Connection")
oConn.Open myConnString
strSQL = "SELECT field1, etc FROM my_table WHERE (field = :filter_field)"
dim oFilteredList
set oFilteredList = Server.CreateObject("ADODB.Command")
oFilteredList.ActiveConnection = oConn
oFilteredList.CommandText = strSQL
oFilteredList.CommandType = adCmdText
oFilteredList.NamedParameters = True
set oParam = oFilteredList.CreateParameter("filter_field", adVarChar, adParamInput, 10, filter_value)
oFilteredList.Parameters.Append oParam
set rsResults = oFilteredList.Execute
This causes the error “Parameter object is improperly defined. Inconsistent or incomplete information was provided”
What is the correct method of calling Oracle / PL/SQL with named parameters from ADO? I need to use named parameters because the actual SQL code is somewhat more complex, and different parameters are used multiple times throughout the SQL command.
How do you have filter_value defined? If it's not declared as a String or if you've assigned a string longer than 10 characters (as you've indicated when creating the parameter), you'll have issues with that.
Additionally (and partly for my own reference), named parameters are not supported via OraOLEDB (i.e. ADODB).
See Oracle® Provider for OLE DB Developer's Guide 11g Release 1 (11.1) or follow the "Command Parameters" heading link on any of the previous versions (8iR3, 9i, 9iR2, 10g, 10gR2):
Command Parameters
When using Oracle ANSI SQL, parameters
in the command text are preceded by a
colon. In ODBC SQL, parameters are
indicated by a question mark (?).
OraOLEDB supports input, output, and
input and output parameters for PL/SQL
stored procedures and stored
functions. OraOLEDB supports input
parameters for SQL statements.
"Note: OraOLEDB supports only
positional binding."
That said, this should have no bearing on your query when using OraOLEDB:
oFilteredList.NamedParameters = True
I've had success running queries exactly as the rest of your example shows though on Oracle 10gR2.
You don't show your connection string, so I must assume it to be valid. Behavior can differ depending on options there, so here's what I successfully use:
`"Provider=OraOLEDB.Oracle;Data Source=TNSNAMES_ENTRY;User ID=XXXX;Password=YYYY;DistribTx=0;"`

Resources