Connection to Oracle DB

dgrimm

Board Regular
Joined
Sep 17, 2007
Messages
159
I am looking for a generic vba code to connect to an oracle db. I do not know any details at this time. Any help would be greatly appreciated.

Thanks in advance.
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Hi you can use Oracle inProcess server , see the code below,

Code:
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 database
Set objDataBase = objSession.OpenDatabase("DATABASENAME", "USERNAME/PASSWORD", 0&)
'Note: to get DATABASENAME execute the query select * from global_name
Dim OraDynaSet As Object

'Write the query. Here i am selecting only 2 columns
strSQL = "SELECT NAME,SALARY FROM EMPLOYEE"

Set OraDynaSet = objDataBase.DBCreateDynaset(strSQL, 0&)

'To know the number of rows returned by the query
RowCount = OraDynaSet.RecordCount
        
        'If there are records retrieved
        OraDynaSet1.MoveFirst
        
        'Loop the recordset for returned rows
        For i = 1 To OraDynaSet1.RecordCount
            'Put the results in different columns in Sheet1
            'Since i am selecting only two columns two field value will be returned
            Sheet1.Cells(i, 1) = OraDynaSet.Fields(0).Value
            Sheet1.Cells(i, 2) = OraDynaSet.Fields(1).Value
        OraDynaSet1.MoveNext
        Next i
End Sub


Thanks,
Ogo
 
Upvote 0
Sorry, I have made a mistake in code, i wrote OraDynaSet1 instead of
OraDynaSet in two places

Thanks,

Ogo
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,142
Members
452,892
Latest member
JUSTOUTOFMYREACH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top