Scriptorium

us fr nl




VBA : Databases: Retrieving data from an Oracle database to Excel using Oracle Objects for Office (OO4O)   nl


Options: Save as PDF | Save attached file | Toggle line numbers

Details:

Type: sub
Added By: Rembo
Short Description:
Oracle databases are widely used. To improve connectivity with coding environments like VB and VBA they created Oracle Objects for Office (OO4O). There are advantages using OO4O rather than ADO (with and OLEDB driver) or ODBC. First and foremost its simply fast, if coded properly. Secondly it is very easy to code. Once you get to know the object model of OO4O you'll find that's its real easy to navigate between records and to create routines to work with an Oracle database. The object model is very complete. 
Notes:
You have to have some knowledge of SQL and in particular the Oracle flavor. You'll also need the login details for your Oracle database and, depending on what you have to do with the database, you'll need to appropriate rights. 

Note that the routine below uses late binding. Some believe that early binding will increase performance a little (less overhead) but I haven't tested that myself. The biggest performance gain can be made by using the correct connection settings. If you do use early binding you will have to set a reference to Oracle InProc Server x.x Type Library, where x.x is the highest version number available on your system.

You'll have to change the values of DBSID, DBUSER, DBPASS to match your Oracle configuration. You'll also need to change YourTable into the table name that you want to retrieve the records from.

Note that Excel 2003 and earlier only have place for 65536 rows of data. If your Oracle table has more data it will result in an error. 
Added: Nov 11 2008 at 4:30 PM
Modified: Jan 21 2010 at 10:47 PM
Related URLs


Usage:

Adjust the variables for your configuration and run the subroutine.


Code:

Formatted | Unformatted
  1. 'Rembo created this routine. You can find me at http://scriptorium.serve-it.nl
  2. '
  3. 'Data Base objects for use with late binding.
  4. Dim oSession As Object
  5. Dim oDBase As Object
  6. Dim oDynaset As Object
  7.  
  8. Global Const ORADB_DEFAULT = &H0& 'The default OpenDatabase method. Note that on newer Oracle versions you
  9. 'can also use the "Oracle Mode (No Refetch)" (=&H4&) mode to boost performance
  10. 'See: http://download.oracle.com/docs/html/B10952_01/o4o00364.htm
  11. Global Const ORADYN_NOCACHE = &H8& 'For fast retrieval of data
  12. 'See: http://download.oracle.com/docs/html/B10952_01/o4o00318.htm
  13. Global Const DBSID = "orcl" 'The Oracle System ID, this uniquely identifies the Oracle database
  14. Global Const DBUSER = "scott" 'The Oracle login name
  15. Global Const DBPASS = "tiger" 'The Oracle password
  16.  
  17.  
  18. Sub FetchOracleData()
  19. 'This sub retrieves all records from YourTable and puts them in a worksheet
  20. '
  21. 'The SQL routine:
  22. 'select * from YourTable
  23.  
  24. Dim sSQL As String
  25. Dim y As Long, x As Long
  26.  
  27. sSQL = "select * from YourTable"
  28. Set oSession = CreateObject("oracleinprocserver.xorasession")
  29. Set oDBase = oSession.OpenDatabase(DBSID, DBUSER & "/" & DBPASS, ORADB_DEFAULT)
  30. Set oDynaset = oDBase.DBCreateDynaset(sSQL, ORADYN_NOCACHE)
  31.  
  32. 'Turn off screen updating to increase performance
  33. Application.ScreenUpdating = False
  34.  
  35. 'Insert field names on row 1 of worksheet 1
  36. For x = 0 To oDynaset.Fields.Count - 1
  37. Worksheets(1).Cells(1, x + 1).Value = oDynaset.Fields(x).Name
  38. Next x
  39.  
  40. 'Insert records (if they exist) on row 2 and further of worksheet 1
  41. If oDynaset.RecordCount > 0 Then
  42. oDynaset.MoveFirst
  43. For y = 0 To oDynaset.RecordCount - 1
  44. For x = 0 To oDynaset.Fields.Count - 1
  45. Worksheet(1).Cells(y + 2, x + 1).Value = oDynaset.Fields(x).Value
  46. Next x
  47. oDynaset.MoveNext
  48. Next y
  49. End If
  50.  
  51. 'Turn screen updating back on
  52. Application.ScreenUpdating = True
  53.  
  54. End Sub



User comments :

Add a new comment   Back to Top
Atom Feed
Contact | About This Application | Scriptorium Website