Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OleDB Data provider can not be found VBA/Excel

I am almost not familiar with VBA (have had some courses back at school and that's it). Now I need to connect to Oracle database (which is running on remote server) from Excel file. I've goggled around and found some examples. So, there is the following code I have written so far:

    Sub Try()
         Dim cn As New ADODB.Connection
         Dim rs As ADODB.Recordset
         Dim cmd As ADODB.Command
         Dim chunk() As Byte
         Dim fd As Integer
         Dim flen As Long
         Dim Main As ADODB.Parameter
         Dim object As ADODB.Parameter

     Stil = vbYesNo + vbCritical + vbDefaultButton1
        Titel = "db connection test"
    '   Meldung anzeigen.
        Antwort = MsgBox("trying to connect to db", Stil, Titel, Hilfe, Ktxt)

         ' Connect to the database using ODBC [msdaora][ORAOLEDB.Oracle]Provider=ORAOLEDB.Oracle;
         With cn
             .ConnectionString = "Provider=ORAOLEDB.Oracle;Password=pass;User ID=usr;Data Source=host:port:sid"
             .Open
             .CursorLocation = adUseClient
         End With

         ret = cn.Execute("create table newtesttable (main integer, object oid)")

         ' Here is an example if you want to issue a direct
    ' command to the database
         '
         'Set cmd = New ADODB.Command
         'With cmd
         '    .CommandText = "delete from MYTABLE"
         '    .ActiveConnection = cn
         '    .Execute
         'End With
         'Set cmd = Nothing

         '
         ' Here is an example of how insert directly into the
     ' database without using
         ' a recordset and the AddNew method
         '
         Set cmd = New ADODB.Command
         cmd.ActiveConnection = cn
         ' cmd.CommandText = "insert into newtesttable(main,object) values(?,?)"
         cmd.CommandText = "select * from test"
         cmd.CommandType = adCmdText

         ' The main parameter
        ' Set main = cmd.CreateParameter("main", adInteger, adParamInput)
         'main.Value = 100 '' a random integer value ''
         'cmd.Parameters.Append main

         ' Open the file for reading
         'fd = FreeFile
         'Open "myBlobFile.txt" For Binary Access Read As fd
         'flen = LOF(fd)
         'If flen = 0 Then
          '   Close
           '  MsgBox "Error while opening the file"
            ' End
         'End If

         ' The object parameter
         '
         ' The fourth parameter indicates the memory to allocate
     ' to store the object
       '  Set object = cmd.CreateParameter("object", _
       '                                       adLongVarBinary, _
       '                                       adParamInput, _
                                              flen + 100)
       '  ReDim chunk(1 To flen)
       '  Get fd, , chunk()

         ' Insert the object into the parameter object
       '  object.AppendChunk chunk()
       '  cmd.Parameters.Append object

         ' Now execute the command
         Set rs = cmd.Execute

     '   Mldg = "test"
        Stil = vbYesNo + vbCritical + vbDefaultButton1
        Titel = "asdasdasd"
    '   Meldung anzeigen.
        Antwort = MsgBox(rs, Stil, Titel, Hilfe, Ktxt)
         ' ... and close all
         cn.Close
         Close

 End Sub

I believe there are many problems in this code, but at the moment it fails when trying to execute .Open, saying, that "Provider cannot be found. It may not be properly installed". After that I've found that I need to download and install ORAOLEDB.dll. I did this by installing ORAOledb11.dll (I've tried both 32 bit and 64 bit, thou my machine is 64 bit). I've installed it by executing regsvr32 OraOLEDB11.dll.

Unfortunately the problem is till there. So, what could be the steps to troubleshoot this problem? Can I somehow ensure, that Oraoledb is properly installed on my machine?

Any tips would be greatly appreciated.

like image 299
Alex K. Avatar asked Oct 05 '22 01:10

Alex K.


1 Answers

32-bit OS

I managed to get this working on Windows XP virtual machine by downloading oracle OLEDB provider from Oracle official website Oracle10g Provider for OLE DB Version 10.1.0.4.0. Current working link OLEDB for older OS (32 - bit)

But be aware it will replace JDK and JRE to lower version (It can be prevented by playing with the configuration xml - products.xml - I did not have enough of mental health potion, so I did full install instead). Afterwards you need to delete reference in environment variables as it may effect other programs. After installation, I registered OraOLEDBxx.dll with regsvc32

I was connecting to oracle db 11G with excel 2003. :)

Connection string

I had to enable extensions (ActiveX Data Object and record libraries). My function returning connection was:

Public Function connectToDb(provider As String, host As String, sid As String, user As String, pwd As String, db As String) As ADODB.Connection
Dim conn As ADODB.Connection
Dim dbConnectStr As String

    Set conn = New ADODB.Connection
    If provider = "Oracle" Then
        dbConnectStr = "Provider=OraOLEDB.Oracle;Data Source=" & host & ":1521/" & sid & ";User Id=" & user & ";Password=" & pwd & ";"
    Else

    End If
    conn.ConnectionString = dbConnectStr
    conn.Open
    Set connectToDb = conn
End Function

64-bit OS but 32-bit Office

When our VMs migrated to 64-bit Windows 7 with Excel 2010. Make sure you will download ODAC - Oracle Data Access Components - for right -bit version of your excel installation because I had 32-bit excel installed and thought it was 64-bit (as windows is 64-bit) so I was giving birth trying to get this working with 64-bit ODAC version. Afterwards I did download 32-bit version and it works just as before. For installation just follow install instructions included in downloaded archive folder.

Current working links for ODAC on Oracle website

like image 94
GentSVK Avatar answered Oct 10 '22 01:10

GentSVK