Monday, March 16, 2009

Using Microsoft Access as Oracle Client

To connect to Oracle as you described above, you'll have to first create an ODBC connection to your Oracle database using the {Microsoft ODBC for Oracle} driver.

To do this, go to the "Data Sources ODBC" icon under the Control Panel and create a new Data Source using the {Microsoft ODBC for Oracle} driver.















Set up your ODBC connection.

In this example, we've setup the Data Source with a name of AAAA, with a user name of BBBB, and an Oracle server called CCCC. You'll need to configure the ODBC connection with your own settings.










Next, open your Access database, click on the Modules tab and create a new Module.

Paste in the following code:

Function OracleConnect() As Boolean

Dim ws As Workspace
Dim db As Database
Dim LConnect As String

On Error GoTo Err_Execute

'Use {Microsoft ODBC for Oracle} ODBC connection
LConnect = "ODBC;DSN=AAAA;UID=BBBB;PWD=DDDD;SERVER=CCCC"

'Point to the current workspace
Set ws = DBEngine.Workspaces(0)

'Connect to Oracle
Set db = ws.OpenDatabase("", False, True, LConnect)

db.Close

OracleConnect = True

Exit Function

Err_Execute:
MsgBox "Connecting to Oracle failed."
OracleConnect = False

End Function



Please note that you'll need to customize the following line of code:

LConnect = "ODBC;DSN=AAAA;UID=BBBB;PWD=DDDD;SERVER=CCCC"

So that:

AAAA is the name of the ODBC Data Source that you set up.
BBBB is the user name that you will use to log into Oracle.
CCCC is the name of your Oracle server.
DDDD is the password that you will use to log into Oracle



If after trying this example, you receive a "not defined" error on the "Dim db as Database" declaration, you will need to follow some additional instructions.

No comments:

Post a Comment