Avatar of LarsBone
LarsBone

asked on 

Excle 2000/XP/03: How to realise SQL REQUEST to Oracle database via ODBC as cell function or VBA?

Hi everybody!
From inside Excel 2000/XP/2003 (cell function or VBA) I want to make an sql request via ODBC to an Oracle 9i database. The request itself would be very simple and would fill approx. 1 column and a couple of rows. TNSPING to this DB works fine.

The question now is: how can I realize this request in an easy way? I had a look at the sql.request function inside Excel but couldn't get it working because I am not sure how to define the connection_string properly. In this case the database is defined by an Oracle service name ("dbserv_tmp") that is the same than the ODBC service name ("dbserv_tmp"). My result was always the same: #value in the cell with the sql.request function.
Oracle DatabaseMicrosoft Excel

Avatar of undefined
Last Comment
nike_golf
ASKER CERTIFIED SOLUTION
Avatar of nike_golf
nike_golf
Flag of Afghanistan image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of LarsBone
LarsBone

ASKER

Unfortunately the compiler already stops at:   "Set cnMySql = New ADODB.Connection" with the message "User-defined type not defined". I love these messages... ;-)
Are you sure this code should work on Excel 2000?
Avatar of nike_golf
nike_golf
Flag of Afghanistan image

What references do you have added in? (TOOLS/REFERENCES in the VBE)

NG,
Avatar of LarsBone
LarsBone

ASKER

- Visual Basic For Applications
- Microsoft Excel 9.0 Object Library
- OLE Automation
- Microsoft Office 9.0 Object Library
Avatar of LarsBone
LarsBone

ASKER

Now I selected the right references and changed the code to this one and it works fine!
Thanks a lot!!

Sub Makro2()
Dim SQL As String

On Error GoTo errhandler:
 'set up a remote data connection using the MySQL ODBC driver.
 'change the connect string with your username,
 'password, server name and the database you wish to connect to.

Set cnMySql = New ADODB.Connection
   
cnMySql.ConnectionString = "UID=vetsone;PWD=vetsone;driver={Microsoft ODBC for Oracle};" _
        & "CONNECTSTRING=DBSERV_TMP;"

cnMySql.Open
Set adoRS = New ADODB.Recordset
adoRS.CursorLocation = adUseServer

SQL = "SELECT TESTTYP FROM TESTTYPDEFINITION"

Debug.Print SQL

' Query Sales from data
adoRS.Open SQL, cnMySql

' Push to Excel
Application.DisplayAlerts = False

Sheets("Tabelle1").Range("A5").CopyFromRecordset adoRS

Application.DisplayAlerts = True

' close record set
adoRS.Close
Exit Sub

errhandler:
    MsgBox "There was a problem conecting to ORACLE: " & vbCr & Err.HelpContext & " - " & Err.Description

End Sub
Avatar of nike_golf
nike_golf
Flag of Afghanistan image

Great glad it worked for you I should have mentioned you needed ADO as reference....

NG,
Microsoft Excel
Microsoft Excel

Microsoft Excel topics include formulas, formatting, VBA macros and user-defined functions, and everything else related to the spreadsheet user interface, including error messages.

144K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo