Connect to IBM DB2

Tested in Excel 365 (16.8730.2046) 64-bit. All links open in new tab.

Prerequisites: IBM DB2 Data Server Driver, minimum requirement is the IBM Data Server Driver Package (DS Driver).
Here I used the GSDB sample database offered by IBM, installed as “sampledb”.

 
 
  1. Public Sub OpenDB2Recordset()
  2.     ' needs reference to Microsoft ActiveX Data Object...
  3.    Dim cn As ADODB.Connection
  4.     Dim rs As ADODB.Recordset
  5.     Dim SQL As String
  6.    
  7.     SQL = "select * from GOSALES.BRANCH"
  8.     Set cn = New ADODB.Connection
  9.     Set rs = New ADODB.Recordset
  10.    
  11.     cn.Open ("Provider=IBMOLEDB.IBMDBCL1;data source=sampledb;" & _
  12.         "location=localhost;Protocol=TCPIP;" & _
  13.         "Port=50000;Uid=USERNAME;Pwd=PASSWORD;")
  14.    
  15.     rs.Open SQL, cn
  16.     ' do something with the recordset
  17.    Debug.Print rs.Fields(1).Value
  18.     Set rs = Nothing
  19.     Set cn = Nothing
  20. End Sub
  21.  

Add a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.