OleDB Data provider can not be found VBA/Excel

后端 未结 2 1908
情歌与酒
情歌与酒 2021-01-13 06:33

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 Ex

2条回答
  •  感情败类
    2021-01-13 06:56

    You can insure that your connection string is accurate by creating a text file with the ending .udl then close and open the file. You'll be prompted with a user interface to connect to the server. Enter your info and test the connection. Then if your connection is working close the file. Open that file in text format and copy the connection string into your code. Also be sure that your reference library is selected for ADO. This line looks wrong:

    Data Source=host:port:sid
    

    Below is a sample I use to pull in the sql from text and extract results to text.

     Public Function ObjectConnect(AID As String, APswd As String)
    
     ObjectConnect = "Provider=ORAOLEDB;Password=" & APswd & ";Persist Security Info=True;User ID=" & AID & ";Data Source=(nameofserverConn)"
    
     End Function
    
    Sub RunSQL()
    
    Dim strConn As String
    Dim Query As String
    Dim txt As Object
    Dim ns As ADODB.Connection
    Dim rs As ADODB.Recordset
    Dim txtfile As Object
    Dim f As ADODB.Field
    Dim myFileSystemObject As Object
    Dim txtN As String
    Dim DL As String
    
    
    FName1 = ""
    Query = ""
    txtStrngX = ""
    
    Set ns = New ADODB.Connection
    Set rs = New ADODB.Recordset
    ns.ConnectionTimeout = 99000
    ns.CommandTimeout = 99000
    
    
    
    ns.Open ObjectConnect('UserID', 'Password') 'this is a public function w. userform for people to enter ID and Password.
    With rs
        .ActiveConnection = ns
          'however you're writing the sql it would go here.
        .Open Query 
    End With
    
    
    If rs.State <> 0 Then
        DL = Sheet1.Cells(2, 2)
        RecordsetToText rs:=rs, FullPath:=txtN, ValueDelimiter:=DL  'this is a sub function that writes to a text file.  Didn't include the code but left this.  Here you would want to do something with the recordset.
    End If
    
    On Error Resume Next
    rs.Close
    Set rs = Nothing
    
    
    End Sub
    

提交回复
热议问题