DB2 connection from excel macro

前端 未结 2 455
醉酒成梦
醉酒成梦 2021-01-07 14:50

I want to connect to DB2 from excel macro...This is my code, but it not working, Its giving error as \'Run-time Error\'...Can anyone help me...

Option Explic         


        
相关标签:
2条回答
  • 2021-01-07 15:11

    Start with changing

    DBCON = CreateObject("OLEDB.Connection")
    

    to

    Set DBCON = CreateObject("ADODB.Connection")
    

    If you still get an error, double-check your connection string.

    0 讨论(0)
  • 2021-01-07 15:21

    The JDBC functionality I am pretty sure is not supported through vba and I think you need to use ODBC connectors to connect to DB2 if you are trying to integrate it into excel.

    Private Sub query()
      DBCONSRT = "Provider=MSDASQL.1;Persist Security Info=False;User ID=user;Data Source=NZ1;DSN=NZ1;UID=user;SDSN=;HST=ibslnpb1.sysplex.homedepot.com;PRT=4101;In‌​itial Catalog=PRTHD;"
      Using connection = New OleDbConnection(DBCONSRT )
          connection.Open()
          Dim cmd = connection.CreateCommand()
          cmd.CommandText = QRYSTR //This is where your sql statement should go, or the variable that is equal to the query.
          Using dr = cmd.ExecuteReader()
              //Process your query results here 
          End Using
      End Using
    End Sub 
    
    0 讨论(0)
提交回复
热议问题