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”.
- Public Sub OpenDB2Recordset()
- ' needs reference to Microsoft ActiveX Data Object...
- Dim cn As ADODB.Connection
- Dim rs As ADODB.Recordset
- Dim SQL As String
- SQL = "select * from GOSALES.BRANCH"
- Set cn = New ADODB.Connection
- Set rs = New ADODB.Recordset
- cn.Open ("Provider=IBMOLEDB.IBMDBCL1;data source=sampledb;" & _
- "location=localhost;Protocol=TCPIP;" & _
- "Port=50000;Uid=USERNAME;Pwd=PASSWORD;")
- rs.Open SQL, cn
- ' do something with the recordset
- Debug.Print rs.Fields(1).Value
- Set rs = Nothing
- Set cn = Nothing
- End Sub