Kubie
Kubie

Reputation: 1571

Excel VBA Connect to IBM Db2

Currently, I'm trying to query a 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 command 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

Upvotes: 1

Views: 9940

Answers (2)

Andre
Andre

Reputation: 11

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

Upvotes: 1

Kubie
Kubie

Reputation: 1571

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

Upvotes: 2

Related Questions