Connecting with Oracle: Accessing Oracle via Access and Excel

Wednesday May 26th 2004 by Steve Callan
Share:

Giving your users the ability to access an Oracle database using either Access or Excel is easy to do. It takes all of three minutes to set up the link or connection. Learn how in Connecting with Oracle, Part 4.

Connecting with Oracle can be quite a challenge for new users and DBA's. Once you've learned the ins and outs of configuration files and Oracle Net, connecting via SQL*Plus or another Oracle application is a piece of cake. However, as a new Oracle DBA in a small to medium sized business where all of your users are Access and Excel wizards, should you be expected to teach all of your Office users how to become Oracle experts when you yourself barely know what to do? So far, you have been armed with the Oracle 10g 2 Day DBA course because your business has taken the plunge into the Oracle world.

Will your Access and Excel users be able to view data within an Oracle database right now, or do they have to wait for a SQL or SQL*Plus class next month? Fortunately, for you and your users, giving them the ability to access an Oracle database using either Access or Excel is easy to do. It takes all of three minutes to set up the link or connection. Moreover, even for DBA's in larger Oracle shops, knowing how to expose table data via these applications is a useful skill. Because Access and Excel can both be used to view table data within an Oracle database, the tools Oracle lacks with respect to data manipulation and analysis are more than made up for when using Access or Excel. With Access, users can also insert, delete and update data (Excel is view only).

What is the key to making this connectivity happen? Open Database Connectivity, or ODBC as it is commonly known, is your key to success. First, there is a Microsoft-Oracle connection definition step, and then Access and Excel each use similar steps to get external data. The following steps and screenshots are based on a PC using XP Professional.

Setting up the data source definition

If you expand the Start>Programs>Your Oracle Home program>Configuration and Migration Tools path, you will see a utility named Microsoft ODBC Administrator. Launch that utility to begin the process. If you have a fairly standard PC from any major vendor, it will already have definitions for dBASE, Excel and Access (note that these are all Microsoft products). Not to worry, because adding an Oracle database as a source is just a few clicks away. Shown below is a typical pre-Oracle view of the window.

Click on the Add button to add a new data source. In the window shown below, simply enter the name of your Oracle database, give it a description, enter the name of the service and provide a user ID (I added Scott's password so as not to be prompted for it down the road). If you want to test the connectivity, click on the Test Connection button (good idea).

If your connection test was successful, you will see the following:

After clicking OK on the Oracle ODBC Driver Configuration window, you will see your Oracle database listed in the ODBC Data Source Administrator window.

Now you are ready to make a link in Access.

Configuring Access to connect to Oracle

Configuring Access is just about as simple as the previous steps. In the next example, I created a new Access database and used it to read Scott's EMP table. I also updated data and added a new employee.

As a side note, Oracle has stated that the Scott schema has become a deprecated feature. It seems that future releases will be using the sample schemas (HR, SH, etc.) you see installed when using DBCA. If you want to keep the Scott schema, you can get its creation scripts from one of two places: ORACLE_HOME\rdbms\admin\utlsampl.sql and \sqlplus\demo\demobld.sql.

Start Access and create a new database. Go to File>External Data>Link Tables. In the Link window, you will have to scroll down the list shown in the Files of Type field. Select ODBC Database as shown in the screenshot.

Click for larger image

Once you have done that, you will be prompted to select a data source. Switch tab views to the Machine Data Source tab. Recognize the items in that list? You should, because it is the same as what you previously saw in the ODBC Data Source Administrator window.

After highlighting your Oracle database data source, click OK. You will be presented with a list of tables. You can control-click on tables to add more than one at a time. Shown below is the area in the list where Scott's tables reside (somewhat towards the bottom of the list).

I selected BONUS, EMP, DEPT and SALGRADE. After selecting the tables, you will be ready to access them. Highlight one (EMP, in this case) and click Open.

The results of querying the EMP table should look very familiar. In the display shown below, I changed James' salary to 952 and saved the table.

Did the save (update) operation take effect? Let's see.

SQL> select * from emp;

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN2     SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        952 LOOK HERE        30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10

14 rows selected.

A simple query in SQL*Plus confirms the update (LOOK HERE was typed in later). That was easy, but what if I had made a mistake? For example, what happens if I create a new employee, but violate a constraint? In the screenshot shown below, I attempted to insert a new employee named Linda Cole, but I used an existing empno. What does Access tell me about what happened?

The ODBC call failed,

and here is why:

After fixing my mistake, the save (insert) operation takes place, and a query in SQL*Plus confirms it.

SQL> /

     EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80        800                    20
      7499 ALLEN2     SALESMAN        7698 20-FEB-81       1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81       1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81       2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81       1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81       2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81       2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87       3000                    20
      7839 KING       PRESIDENT            17-NOV-81       5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81       1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87       1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81        952                    30
      7902 FORD       ANALYST         7566 03-DEC-81       3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82       1300                    10
***** 7935 COLE       LINDA           7839 01-MAY-04       4000                    30

15 rows selected.

Configuring Excel to Connect to Oracle


Open a new book or sheet in Excel. Follow the Data>Import External Data>Import Data path to select a data source. Highlight +Connect to New Data Source and click Open (or double-click it) to bring up the Data Connection Wizard.


Click for larger image


Select Oracle from the list and click Next.


Click for larger image


Enter the information for your Oracle database connection and click Next.


Scroll down to Scott's tables and select a single table (EMP). Click Next.

Enter a description for the table and click Finish.

Import the data into a range of cells.

The sheet now reflects the contents of Scott's EMP table.

Having a hard time remembering how to sort in Oracle (or don't know how in the first place)? If your user is even marginally adept at using Excel, sorting (and summing, formatting, etc.) is no problem.

In closing

As you can see, the steps behind setting up a PC for ODBC are very simple to perform. Likewise, the steps to access an Oracle database via Access or Excel are also fairly straightforward. Not all of the screenshots and steps were shown, but enough of them were, so if you were faced with this task tomorrow, you should feel confident that you would be able to make the connection with Oracle.

» See All Articles by Columnist Steve Callan

Share:
Home
Mobile Site | Full Site
Copyright 2017 © QuinStreet Inc. All Rights Reserved