fhg
Volume 7, Number 21 -- June 6, 2007

Load a Spreadsheet from a DB2/400 Database

Published: June 6, 2007

Hey, Ted:

I know from my reading that it is possible to make an Excel spreadsheet read from an iSeries database. I've been an RPG programmer for years, and I don't know where to start. Can you point me in the right direction?

--"D"


I'll do my best. I have some Visual Basic code that I've used for years that I can share with you. I wish I could give credit to the original author, but I don't remember where I got it.

First, crank up a new Excel spreadsheet. Pull down the Tools menu, select Macro, then Visual Basic Editor. The VB editor will open in a new window.

Use the Insert menu to create a new module. The editor will probably give it the name Module1, which is fine. Copy the following code into the module window.

Option Explicit

Private Sub Workbook_Open()
Dim Con As New ADODB.Connection
Dim Cmd As New ADODB.Command
Dim Rs As ADODB.Recordset

Con.Open "provider=IBMDA400;data 
source=xxx;USER ID=xxx;PASSWORD=xxx;"

Set Cmd.ActiveConnection = Con
Cmd.CommandText = "SELECT * FROM QIWS.QCUSTCDT"

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
End Sub

Replace the xxx tokens with values to indicate data source name, user profile name, and password. Also change the SQL command that is assigned to Cmd.CommandText so that it retrieves the desired data. I'd save the spreadsheet at this point.

In the VB editor, pull down the Tools menu and select References. VB responds with a list of available references. You'll need to add one of the Microsoft ActiveX Data Objects libraries. I always pick the one with the highest release number. Click the check box to select, then click on the OK button.

To run the macro, press F5. Return to the spreadsheet window and you should see data.

I have used this code and variations of it for years and it has always served me well. If you'll search the Four Hundred Guru archives, you'll find more articles that touch on this subject. I've listed some of them below.

--Ted


RELATED STORIES

How to Call a Stored Procedure from Excel

Using Different ODBC DSN Types for i5 Access

DSN-Less Connections Using ActiveX Data Object



                     Post this story to del.icio.us
               Post this story to Digg
    Post this story to Slashdot


Sponsored By
HELP/SYSTEMS

SEQUEL can be used for
virtually ALL business intelligence functions
on the System i, including:

                                                    · Executive Dashboards
                                                    · Graphical Query & Reporting
                                                    · Drill-Down Data Analysis
                                                    · Multi-Platform Database Support
                                                    · E-Mail Report and File Distribution
                                                    · Secure Web Access

SEQUEL is the single solution for all
your business intelligence needs.

www.helpsystems.com


Senior Technical Editor: Ted Holt
Technical Editors: Howard Arner, Joe Hertvik, Shannon O'Donnell, Kevin Vandever
Contributing Technical Editors: Joel Cochran, Wayne O. Evans, Raymond Everhart,
Bruce Guetzkow, Brian Kelly, Marc Logemann, David Morris
Publisher and Advertising Director: Jenny Thomas
Advertising Sales Representative: Kim Reed
Contact the Editors: To contact anyone on the IT Jungle Team
Go to our contacts page and send us a message.

Sponsored Links

New Generation Software:  Leading provider of iSeries BI and financial management software
COMMON:  Join us at the Annual 2008 conference, March 30 - April 3, in Nashville, Tennessee
LASERTEC USA:  Fully integrate MICR check printing with your existing application


IT Jungle Store Top Book Picks

The System i Pocket RPG & RPG IV Guide: List Price, $69.95
The iSeries Pocket Database Guide: List Price, $59.00
The iSeries Pocket Developers' Guide: List Price, $59.00
The iSeries Pocket SQL Guide: List Price, $59.00
The iSeries Pocket Query Guide: List Price, $49.00
The iSeries Pocket WebFacing Primer: List Price, $39.00
Migrating to WebSphere Express for iSeries: List Price, $49.00
iSeries Express Web Implementer's Guide: List Price, $59.00
Getting Started with WebSphere Development Studio for iSeries: List Price, $79.95
Getting Started With WebSphere Development Studio Client for iSeries: List Price, $89.00
Getting Started with WebSphere Express for iSeries: List Price, $49.00
WebFacing Application Design and Development Guide: List Price, $55.00
Can the AS/400 Survive IBM?: List Price, $49.00
The All-Everything Machine: List Price, $29.95
Chip Wars: List Price, $29.95

 

The Four Hundred
IBM's Rumored System i Power6 Server Plans

IBM Tries Online Discounts to Move Server, Storage Gear

The Market for Servers in Europe Is Hot

Mad Dog 21/21: Missing Inaction

The Linux Beacon
Red Hat Puts Out Fedora 7 Community Release

Novell Posts Another Loss in the Second Quarter

Microsoft-Novell Deal Has Escape Clause

As I See It: Operating on Overload

Four Hundred Stuff
Maximum Availability Shakes Up Business Plan

SEA Delivers Web and Mobile Consoles for absMessage

Aldon Boosts Identity Tracking in ALM Tool

Quadrant Bolsters FastFax with Dynamic Line Allocation

Big Iron
Virtualization, Consolidation Drive Server Sales in Q1

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
June 2, 2007: Volume 9, Number 22

May 26, 2007: Volume 9, Number 21

May 19, 2007: Volume 9, Number 20

May 12, 2007: Volume 9, Number 19

May 5, 2007: Volume 9, Number 18

April 28, 2007: Volume 9, Number 17

The Windows Observer
All Your IT Dollars Are Belong to Microsoft

Microsoft-Novell Deal Has Escape Clause

Virtualization, Consolidation Drive Server Sales in Q1

As I See It: Operating on Overload

The Unix Guardian
The Persistence of Unix

HP Pursues Telcos with New Entry NonStop Server

The Market for Servers in Europe Is Hot

As I See It: Operating on Overload

Four Hundred Monitor
Four Hundred Monitor's
Full iSeries Events Calendar

THIS ISSUE SPONSORED BY:

WorksRight Software
Help/Systems
Guild Companies



TABLE OF CONTENTS
Special Files Can Do It All, Part 2

Load a Spreadsheet from a DB2/400 Database

Admin Alert: Weird i5 User Profile Sign-On Secrets

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
Keeping a trace of each CL

Problem with "cpyfrmimpf"

FTP a library to a server

Uploading data from Excel to the iSeries

How to calculate the last day of the month





 
Subscription Information:
You can unsubscribe, change your email address, or sign up for any of IT Jungle's free e-newsletters through our Web site at http://www.itjungle.com/sub/subscribe.html.

Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.
Guild Companies, Inc., 50 Park Terrace East, Suite 8F, New York, NY 10034

Privacy Statement