VBA Oracle connection

Stevenn

Active Member
Joined
Feb 8, 2012
Messages
259
I connect to an Oracle database by the code below. Is it possible to improve the code? What could a function which returns data based on a ID in a Excel sheet look like =getName(id)? Is it bad syntax to make a function with a sql string-parameter like =getValue("SELECT * FROM db WHERE Id = id")? I am very new at db connections through vba.


Option Explicit

Public objConnection As New ADODB.Connection

Public Function Connect() As ADODB.Connection
Const strNavn As String = "DB"
Const strBrugernavn As String = "user"
Const strKodeord As String = "pass"

With objConnection

.Open "DSN=" & strNavn & "; UID=" & strBrugernavn & "; PWD=" & strKodeord & ";"
.CursorLocation = adUseServer

End With

Set Connect = objConnection

End Function
Public Function SelectQuery(strSQL As String) As ADODB.Recordset
If objConnection.State <> 1 Then

Set objConnection = Connect

End If

Set SelectQuery = New ADODB.Recordset

With SelectQuery

.CursorLocation = adUseServer
.Open strSQL, objConnection, adOpenForwardOnly

End With

End Function
Public Function InsertQuery(strSQL As String) As Boolean
If objConnection.State <> 1 Then

Set objConnection = Connect

End If

objConnection.Execute strSQL

End Function
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.

Forum statistics

Threads
1,223,236
Messages
6,170,912
Members
452,366
Latest member
TePunaBloke

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