TechTip: Automatically Load Data into Excel PDF Print E-mail
Written by Kevin Forsythe   
Thursday, 01 July 2004

Learn how to use SQL, ADO, and ODBC together to automate downloads.

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 Con As New ADODB.Connection
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.


Last Updated ( Thursday, 01 July 2004 )
 
Discuss (14 posts)
K.Forsythe
TechTip: Automatically Load Data into Excel
Jul 21 2004 14:59:00
Does your statement look like the one below? <p>If so then double check the references under the Tools pull down menu of the VB editor, and include the ADO Objects. Make sure you are up to date on MDAC from Microsoft. Make sure that you have the OLEDB drivers installed through Client Access - <p>If you have all that and are still getting this error - Let me know. You can email me directly at kevin.forsythe@dmcconsulting.com I'll send you a copy of my working example that you can play with - or you can send me yours, and I'll take a look. <p>Good luck! <p>Kevin <p><!--mccodelink_begin--> <BR>
<!-- do not remove --> <BR>
<hr width=50 align=left><small><a href='http://www.mcpressonline.com/mc/showcode@@.6aec8184/13' target='_blank'>Code</a></small> <BR>
<!--mccodelink_end-->
#112570
K.Forsythe
TechTip: Automatically Load Data into Excel
Jul 21 2004 14:57:00
Does your statement look like the one below? <p>If so then double check the references under the Tools pull down menu of the VB editor, and include the ADO Objects. Make sure you are up to date on MDAC from Microsoft. Make sure that you have the OLEDB drivers installed through Client Access - <p>If you have all that and are still getting this error - Let me know. You can email me directly at kevin.forsythe@dmcconsulting.com I'll send you a copy of my working example that you can play with - or you can send me yours, and I'll take a look. <p>Good luck! <p>Kevin <p><!--mccodelink_begin--> <BR>
<!-- do not remove --> <BR>
<hr width=50 align=left><small><a href='http://www.mcpressonline.com/mc/showcode@@.6aec8184/12' target='_blank'>Code</a></small> <BR>
<!--mccodelink_end-->
#112569
asarac
TechTip: Automatically Load Data into Excel
Jul 21 2004 14:37:00
Automation Error <BR>
Specified Procedure cannot be found <p>The execution stops at the line which the following command resides <p>Con.Open "provider=IBMDA400;data source=XXX.XX.X.X;USER ID=*********;PASSWORD=********;" <p>Any suggestion for what to do? Thanks in advance. <p>Abdul
#112568
K.Forsythe
TechTip: Automatically Load Data into Excel
Jul 13 2004 13:23:00
The 65535 is the default for hex and in my experience, is typically seen in old "flat" program described files. Can you show me the raw format of the data in that column? (try DSPPFM)
#112567
K.Forsythe
TechTip: Automatically Load Data into Excel
Jul 13 2004 13:17:00
Use selective setup in Client Access to verify that the OLE DB Provider is installed. <p>If that doesn't work - get back with me off the forum and I'll help you out. <p>Take care, <p>Kevin <p>Kevin.forsythe@dmcconsulting.com
#112566
Guest.Visitor
TechTip: Automatically Load Data into Excel
Jul 12 2004 12:04:00
collette wrote:<BR>
> I am getting the message Provider not found. Any help would be<BR>
> greatly appreciated.<BR>
<P>
I'm just guessing here, but is the Client Access ODBC driver loaded on the<BR>
PC?<BR>
<P>
Bill<BR>
<P>
<P>
#112565
Guest.Visitor
TechTip: Automatically Load Data into Excel
Jul 09 2004 18:16:00
I am getting the message Provider not found. Any help would be greatly appreciated.
#112564
Guest.Visitor
TechTip: Automatically Load Data into Excel
Jul 02 2004 14:34:00
Have you configured your ODBC Data source in the Control Panel? In my case I configured my ODBC data source to point to our AS400 and gate the data source a name. It's that name that I used for my data source in the code. I didn't use the IP address because it is defined in the ODBC Data source.
#112563
fhabibi@officedepot.com
TechTip: Automatically Load Data into Excel
Jul 02 2004 12:56:00
Kevin, Thank you for share your code. but I get this message when I run the macro. <p>compiler error, invalid outside procedure on, <p>"provider=IBMDA400;data source=as400ipaddress;USER ID=myuserid;PASSWORD=mypwd;" <p>Any ideas? <BR>
Thank you in advance..
#112562
nlandry999
TechTip: Automatically Load Data into Excel
Jul 02 2004 11:48:00
Did a dspffd and found the text fields that came down correctly have a character id of 37. The fields not converting are id 65535. We have checked the ODBC source for the AS400 and it has the option to convert checked under the translation tab. is there somehwere else we need to enable the translation? <p>Also, how does character id 37 get assigned? <p>Included some of the info from dspffd: <p><!--mccodelink_begin--> <BR>
<!-- do not remove --> <BR>
<hr width=50 align=left><small><a href='http://www.mcpressonline.com/mc/showcode@@.6aec8184/3' target='_blank'>Code</a></small> <BR>
<!--mccodelink_end-->
#112561
K.Forsythe
TechTip: Automatically Load Data into Excel
Jul 02 2004 11:19:00
I havn't had that problem except in some situations where the CCSID 65535 needed to be converted. It sounds like you already have that covered. Does the file specify any particular character sets? Can you run the DSFFD command against the file? What CCSID is assigned?
#112560
Guest.Visitor
TechTip: Automatically Load Data into Excel
Jul 02 2004 10:39:00
I was able to get it to start downloading the data, but when the rowCount reaches 32,767, an overflow error occurs. I know that Excel sheets allow 65,534 rows. I was able to stop this error by changing rowCount to a Long variable type.
#112559
Guest.Visitor
TechTip: Automatically Load Data into Excel
Jul 02 2004 10:23:00
Numbers came dowm perfect, but all text except the headings are a combination of boxes, Chinese, and "@" signs. Kind of looks like the CCSID box has not been checked, but verified the ODBC and it should be right. <p>Any suggestions to making this work? The column headings came out perfect also....
#112558
MC Press Web Site Staff
TechTip: Automatically Load Data into Excel
Jul 21 2004 14:59:00
This is a discussion about <B>TechTip: Automatically Load Data into Excel</b>.<p align='center'><a href=http://www.mcpressonline.com/mc?1@232.1KNKfHX1eQT.17@.6aec1393>Click here for the article</a>.</p>
#112557


Discuss...
User Rating: / 0
PoorBest 
Related Articles
< Prev   Next >

   MC-STORE.COM