Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
By Gerrit-Jan Linker Most people know you can access Oracle databases through ODBC. But did you know that you can also use Oracle Objects for OLE (OO4O) to connect to Oracle from Windows-based applications without using complex APIs or external data drivers? At SCT International, a subsidiary of SCT Corporation, we used Oracle Objects for OLE to build Oraxcel (Currently named SQL*XL), a powerful Oracle-to-Excel utility. After creating Oraxcel, we used it extensively in the internationalization of SCT's Banner system, a suite of customer-care and billing products for the utilities sector. SCT continues to use Oraxcel as the company extends its expertise into internationalizing software products at its new Language Centre.
A major challenge in this process was creating an easy way to get the extracted data to translation agencies. Since it was not possible to deploy the actual Oracle database at an agency, the project team looked at writing a Visual Basic or an Access program to accomplish the task. The program needed to present the text with some additional details, such as the forms on which the text appears, if it appears on buttons or labels, etc. To assist the translators, we wanted the program to contain standard word-processing features such as sorting, printing, cut & paste, and so on. It soon became apparent that Excel had the necessary functionality. To tailor Excel for our purposes, we decided to create a spreadsheet application designed to maintain data integrity and use Excel's useful features. We were especially interested in keeping the general Excel features such as text formatting, printing, and sorting. We created Excel macros to process the text and then update the database with the translations. The processing macros were designed to validate translated texts. For example, text on a button has a fixed length, so we created macros to make sure that the translated text was no longer than the original text. The basic translation process involved reading all system text from the software into an Oracle database and pasting all that data into Excel. The Excel file was sent to the translators, who entered the translated texts next to the original English texts. When finished, they returned the Excel file to SCT where we transferred the translated texts from Excel into the database. The system was then regenerated, using the texts in the new language.
Getting Started
To access your Oracle database using Excel, all you need is Excel Version 5 or above. Excel macros are actually written in Visual Basic. The easiest way to get started is to record a few macros and look at the generated code. Alternatively, you can write macros from scratch if you know the syntax. You can refer to Excel's Help file to read about Visual Basic programming; the syntax of OO4O is explained in its Help files.
Sub ConnectToOracle()
End Sub
Once you've obtained a reference to a database, you can submit and execute PL/SQL or SQL code by using the ExecuteSQL method. The example below shows you how to delete records from a table, using ExecuteSQL.
Dim strSQL As String
objDataBase.ExecuteSQL( strSQL )
Queries are slightly more complicated because you must deal with the returned data. The following example shows how to count the rows in the "emp" table and display the results in a dialog box (MsgBox).
Dim strSQL As String Dim strResult As String Dim strCount As String Dim OraDynaSet As Object
'Prepare the message strResult = "There are " & strCount & " employees in emp."
Excel Programming
So far, we've looked at a few key OO4O commands. When creating a spreadsheet application, it is equally important to understand the Excel object structure. The spreadsheets and workbooks can be modified through the Excel objects. The easiest way to learn about Excel programming is to record a macro and look at the generated code. The example below shows the code generated by Excel when changing the font of cell B3 to bold.
Sub Macro1()
End Sub
The Excel object structure is outlined in the Visual Basic section of Excel's help. The most important objects are shown in the table below. The definitions of the properties and methods for these objects can be found in the Help file, and you can refer to these objects from the macro code. Application The entire Microsoft Excel application. The collection of workbooks (.xls files) currently opened. An open workbook. You can refer to it either by an index or by the workbook's file name. The collection of worksheets within a workbook. The worksheets which can be referenced by an index or by the name of the sheet.
Application.Workbooks
Application.Workbooks(i) or Application.Workbooks("file.xls")
Application.Workbooks(i).Worksheets
Application.Workbooks(i).Worksheets(j) or Application.Workbooks(i).Worksheets("Sheet1")
The example below shows a mixture of Excel and OO4O code. This code retrieves two columns from an employee table called emp and puts the results into a new worksheet. For simplicity, the checks for NULL values fetched and the checks on data types have been omitted.
Dim strSQL As String Dim strResult As String Dim OraDynaSet As Object Dim i As Integer
'select the employee name and number strSQL = "select ename, empno from emp"
OraDynaSet.MoveFirst
OraDynaSet.MoveNext
Next i End If
As you can see, you can achieve Oracle connectivity from Excel quite easily. Since OO4O ships with all Oracle licenses, the software is readily available and is also distributed under Oracle's trial license from the Oracle Web site (www.oracle.com).
There are several books on the market that provide a good introduction for audiences of all skill levels. The information in the Excel Help files is a bit limited, but you can get around this limitation by simply recording a macro to automatically generate the macro code.
Coding Public objSession As Object Public objDataBase As Object ----------------------------------------------------------------------------------------------------------Sub ConnectToOracle() 'Create a reference to the OO4O dll Set objSession = CreateObject("OracleInProcServer.XOraSession") 'Create a reference to my database Set objDataBase = objSession.OpenDatabase("oracle", "scott/tiger", 0&) End Sub ----------------------------------------------------------------------------------------------------------Public Sub showmsg() Dim strSQL As String Dim strResult As String Dim strCount As String Dim OraDynaSet As Object strSQL = "select count(*) from emp" 'Retrieve the results from Oracle Set OraDynaSet = objDataBase.DBCreateDynaset(strSQL, 0&) 'Convert the result to String strCount = CStr(OraDynaSet.Fields(0).Value) 'Prepare the message strResult = "There are " & strCount & " employees in emp." 'Show the message MsgBox strResult End Sub ----------------------------------------------------------------------------------------------------------Public Sub showdata() Dim strSQL As String Dim strResult As String Dim OraDynaSet As Object Dim i As Integer 'Add a new worksheet to your current workbook Worksheets.Add
'select the employee name and number strSQL = "select ename, empno from emp" 'Retrieve the results from Oracle Set OraDynaSet = objDataBase.DBCreateDynaset(strSQL, 0&) If OraDynaSet.RecordCount > 0 Then 'There were records retrieved OraDynaSet.MoveFirst 'Loop the recordset for returned rows For i = 1 To OraDynaSet.RecordCount 'Put the results in column A and B ActiveSheet.Cells(i, 1) = OraDynaSet.Fields(0).Value ActiveSheet.Cells(i, 2) = OraDynaSet.Fields(1).Value OraDynaSet.MoveNext Next i End If End Sub -----------------------------------------------------------------------------------------------------------