Excel makes a great tool for crunching numbers in all sorts of ways. But, all too often, getting the data into Excel is a challenge or at least a burden. Wouldn't it be nice if Excel could automatically and easily get data from your iSeries database? It can! And even better, it's not that hard to do, once you get the basics in place.
To get this process to work, you'll need to complete four steps. The first two steps are simply a matter of installing the right (free) software. You'll need to get the ActiveX (ADO) components from Microsoft, define an ODBC data source, include references to the ADO components in the workbook, and then write the code. Each step is discussed below.
Step One
Microsoft publishes ADO within its MDAC product. Simply download the current version of MDAC and its associated security patches from www.microsoft.com/downloads.
Step Two
Use the ODBC Administration tool shipped with iSeries Access to define the ODBC data source for your iSeries.
Step Three
Open a new Excel workbook. From the Tools pull-down menu, select Macro and then Visual Basic Editor. This launches the VB editor, which can be used to write VB code to customize the behavior of the workbook. But, before any code is written, the ADO objects must be made available to the editor. From the Tools pull-down menu of the VB editor, select References. Select the following components:
- Microsoft ActiveX Data Objects (Multi-dimensional) 2.7 Library
- Microsoft ActiveX Data Objects 2.7 Library
- Microsoft ActiveX Data Objects 2.7 Recordset Library
- Microsoft ADO Ext. 2.7 for DDL and Security
Step Four
Write the code to perform the download. In the Project window, right-click on ThisWorkbook and select View Code. In the Window that opens, change the object combo box to Workbook. The beginning and end of a procedure are written for you; this is the procedure that runs whenever the workbook is opened. Now, simply insert the code such as that listed below to perform the download.
Dim Cmd As New ADODB.Command
Dim Rs As ADODB.Recordset
Con.Open "provider=IBMDA400;data source=xxxx.xxxx.xxxx.xxxx;USER ID=user-id;PASSWORD=password;"
Set Cmd.ActiveConnection = Con
Cmd.CommandText = "SELECT * FROM your-lib.your-file"
Dim rowCount As Integer
Dim colCount As Integer
Dim text As String
Dim Number As Long
Dim val As Variant
Set Rs = Nothing
Set Rs = Cmd.Execute()
Worksheets("sheet1").Activate
Range("A1").Activate
Selection.CurrentRegion.Select
Selection.ClearContents
Range("A1").Select
rowCount = 1
For colCount = 0 To Rs.Fields.Count - 1
Worksheets("Sheet1").Cells(rowCount, colCount + 1).Value = Rs.Fields(colCount).Name
Next colCount
While Not Rs.EOF
rowCount = rowCount + 1
For colCount = 0 To Rs.Fields.Count - 1
If Rs.Fields(colCount).ActualSize = -1 Then
text = ""
Else
val = Rs.Fields(colCount).Value
If VarType(val) = vbNull Then
text = ""
Else
text = val
End If
End If
Worksheets("Sheet1").Cells(rowCount, colCount + 1).Value = text
Next colCount
Rs.MoveNext
Wend
Set Rs = Nothing
Con.Close
This code may be included almost exactly as shown here. The only things that need to change are the iSeries IP address, user ID, password, library, and file name. The code will download the indicated file, retrieve column headings, and load the data into sheet1 of the current workbook. All of this is done each time the workbook is opened. So to test it, obviously you must first save these changes and close it. Then, reopen it.
In the Select statement above, the library name and file name are separated by a period (.). Depending on how the ODBC data source is defined, a slash (/) may be required instead.
If you are comfortable with SQL, you can write dramatically more complex SQL statements. This example simply illustrates the basic principles involved in the process.
Kevin Forsythe has over 18 years of experience working with the iSeries platform and its predecessors. He has been a member of the DMC team for the past nine years. Kevin's primary responsibility is providing iSeries education, but he also provides customers with project management, system design, analysis, and technical construction. In addition to his technical skills (RPG IV, CL, OS/400, SQL, FTP, Query, VB, Net.Data), Kevin possesses the ability to communicate new and complex concepts to his students. He has been the primary instructor for DMC's iSeries-based AS/Credentials training courses since 1997 and has authored courses such as Advanced ILE, SQL, Embedded SQL, Operations Navigator, and Intro to WebSphere Studio. An award-winning speaker, he has spoken at every COMMON Conference since the spring of 2000.
LATEST COMMENTS
MC Press Online