简体   繁体   中英

Excel VBA Connect to IBM DB2

Currently, I'm trying to query DB2 instance from VBA but am having hard time finding proper documentation or previous stack questions that provide complete answers.

When I run the below, I receive an error:

"SQL1032N No start database manager commmand was issued. SQLSTATE=57019"

Current connection code:

Sub connect()

    Dim conn As Object 'Variable for ADODB.Connection object
    Dim rs As Object 'Variable for ADODB.Recordset object

    Set conn = CreateObject("ADODB.Connection")
    Set rs = CreateObject("ADODB.Recordset")

    conn.ConnectionString = "Provider=IBMDADB2.1;Server=servername;Database=dbname;Port=port;Data Source=DB2;ProviderType=OLEDB;UID=uid;PWD=pw"
    conn.Open

    rs.Open "Select * .....", conn
    rs.Close
    conn.Close

End Sub

Thanks yall

Just for reference for anybody who stumbles upon this question in the future, I was able to successfully connect with the following:

Dim conn As Object 'Variable for ADODB.Connection object
Dim rs As Object 'Variable for ADODB.Recordset object

Set conn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

conn.ConnectionString = "Provider=IBMDADB2.1;UID=" & username & ";PWD=" & password & ";Data Source=ABCDB1;ProviderType=OLEDB"
conn.Open

Note: username and password are strings assigned appropriate credentials

This works on my machine with Windows 7, client DB2 installed, and Excel 2010 over a private/secure LAN network

Thanks for that. The code seems to work. If you have an ODBC connection setup (which already handles all the connection intricacies) then the ConnectionString is very simple, simply put the ODBC connection name:

Dim conn As Object 'Variable for ADODB.Connection object
Dim rs As Object 'Variable for ADODB.Recordset object

Set conn = CreateObject("ADODB.Connection")
Set rs = CreateObject("ADODB.Recordset")

conn.ConnectionString = "<YourODBCConnectionName>"
conn.Open

rs.Open "Select * from <schema>.<tableName> fetch first 10 rows only", conn
rs.Close
conn.Close

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM