fhg
Volume 7, Number 25 -- July 11, 2007

Load a Spreadsheet from a DB2/400 Database: Part 2

Published: July 11, 2007

by Michael Sansoterra


Note: The code for this article is available for download here


Ted Holt's tip on loading database data into Excel was great. I'd like to offer a few potential enhancements to the VBA code that was offered in the tip.

The first thing that I would like to note is that if you want to copy all rows and columns from an ADO recordset (an object that holds the results of a query) into the spreadsheet, then there is a CopyFromRecordset method of the Range object to do this:

ws.Range("A2").CopyFromRecordset Rs

In the original code sample, row one was reserved for headings. In this example, the contents (rows and columns) of the recordset object will be dumped into the spreadsheet starting from cell A2. This method is much faster than iterating through the recordset programmatically. Of course there may be some circumstances when a programmatic step through the recordset is warranted, in which case dumping the whole recordset onto a worksheet would not be appropriate. Also, don't forget that some of the "exotic" database data types such as large objects and DataLinks may not be compatible with this command. Further, if you're using CHAR or VARCHAR data tagged with CCSID 65535, CopyFromRecordset will fail because it doesn't know what to do with binary data. This character data should be CAST to a different CCSID in the query, or the option to translate CCSID 65535 automatically should be activated for the IBM ODBC driver or OLE DB provider.

A second potential improvement is to save the user grief by formatting the spreadsheet columns automatically based on the underlying database column's data type. Fortunately, an ADO recordset contains a field object for every column in the recordset. Among other properties, the field object has a type property that indicates its database data type (CHAR, INTEGER, DECIMAL, etc.) If we know that a recordset field is a text field (SQL Types CHAR or VARCHAR, for example) we can automatically format the column as text instead of letting Excel guess the data type. Have you ever downloaded a CHAR(5) zip code column into Excel only to have a leading zero truncated because Excel kindly (but wrongly) interpreted the data as numeric? Formatting the column appropriately will fix this problem. For another example, if we know the data type is currency, then we can automatically format the column accordingly using a currency symbol and a thousands separator.

The original program contained a loop to iterate through each column in the recordset and place the column name as a heading in the spreadsheet. Adding a simple VBA "If" statement to this loop to assign a spreadsheet format to the current column is all it takes:

    If lngType = adDate _
    Or lngType = adDBDate Then
        strFormat = "yyyy-mm-dd"
    ElseIf lngType = adDBTime Then
        strFormat = "hh:mm:ss"

The if structure needs to account for each possible data type. You may want to review and enhance this section of code to meet your particular needs as the generic formats I picked may not be quite right for your application. Once the format is selected, simply assign it to the current column in the worksheet's column collection (where variable col refers to a column letter in the spreadsheet):

.Columns(col).NumberFormat = strFormat

For my third comment, a few other simple things can be done to enhance the code. These are:

  • Activating the hour glass so the user knows that processing is occurring:
  • Application.Cursor = xlWait. Revert it to normal when processing is done: Application.Cursor = xlDefault
  • "Auto-fitting" the rows and columns to a "best fit" for the row heights and column widths once the data has been loaded:
    Cells.Select
    Cells.EntireColumn.AutoFit
    Cells.EntireRow.AutoFit
  • Finally, we can make the headings stand out a little by activating the bold property for Row 1 of the spreadsheet:ws.Rows(1).Font.Bold = True

Click here for the revised sample code. Please note that my subroutine name is different than the Workbook_Open subroutine name used in the original code. It was tested with Excel 2000 and Excel 2007. The column formatting portion of code may need a little tinkering to work with 2002 and 2003 as each version of Excel seems to be a little quirky with its string formats. Enjoy!


RELATED STORY

Load a Spreadsheet from a DB2/400 Database


Michael Sansoterra is a programmer/analyst for i3 Business Solutions, an IT services firm based in Grand Rapids, Michigan. Click here to contact Michael Sansoterra by e-mail.



                     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

Bsafe:  Enterprise security for System i and Linux systems
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


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
Small Form Factor Disks Go Mainstream, the System i Has Gone Fishin'

Project Costs Tell the VoIP Story

IBM to Break Petaflops Barrier with Blue Gene/P

As I See It: The All-American Exhausting Vacation

The Linux Beacon
Red Hat Starts Fiscal 2008 with Modest Profit, Big Revenue Growth

Top 500 Supers: Moore's Law Is Alive and Well

IBM to Break Petaflops Barrier with Blue Gene/P

As I See It: The All-American Exhausting Vacation

Four Hundred Stuff
InfoManager Provides Another Option for System i OLAP

Robot/LPAR Streamlines Tape Backups of Partitioned i5/OS Servers

ROBObak Delivers Online Backups for Remote Offices

Lotus Quickr Now Available from IBM

Big Iron
For Some Users, Multiprise and VSE May Have a Bright Past Ahead

Top Mainframe Stories From Around the Web

Chats, Webinars, Seminars, Shows, and Other Happenings

System i PTF Guide
July 7, 2007: Volume 9, Number 27

June 30, 2007: Volume 9, Number 26

June 23, 2007: Volume 9, Number 25

June 16, 2007: Volume 9, Number 24

June 9, 2007: Volume 9, Number 23

June 2, 2007: Volume 9, Number 22

The Windows Observer
Microsoft Back on the Top 500 List of Biggest HPC Systems

Is Windows Vista Really More Secure Than Linux or OS X?

Mandriva, Ubuntu Not Interested in Microsoft Deals

Microsoft Concedes to Google, Will Scale Back Search with Vista SP1

The Unix Guardian
Sun Gets Serious (Finally) About Supercomputing

Top 500 Supers: Moore's Law Is Alive and Well

HP Promotes Transitive Tool to Port Solaris Apps to Integrity Servers

As I See It: Dare to Be Rich

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

THIS ISSUE SPONSORED BY:

WorksRight Software
Help/Systems
COMMON



TABLE OF CONTENTS
Comment Your Binder Language

Load a Spreadsheet from a DB2/400 Database: Part 2

Admin Alert: More Info on SAVSYSINF, Green-Screen Printing, Performance PTFs, and Batteries

Four Hundred Guru

BACK ISSUES

From the IT Jungle Forums
Urgent Help Needed--Limit the result Set in SQL Stored Proc

FNDSTRPDM Output Member Name to *OUTFILE

Stuck in the Dark Ages

Referencing the key of current record

Brother HL 6050 printing '$' instead of '£' symbol





 
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