26
Fri, Apr
1 New Articles

The Incredible Index Listing Spreadsheet

Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

In January, I took a new gig with a new client and was exposed to the most obtuse field names that I had ever seen in my life. They had absolutely nothing to do with the underlying type of data, and there was no rhyme or reason to the naming convention. Now, this is normally not too much of a problem, but the system I was designing had to access over 70 files out of 1,000 in a single library, each file having about 100 fields and at least four logical files (indexes). In addition, most of the physical files had a minimum of 1 million records to contend with. All of this points to my having spent a lot of time trying to optimize the data access of my queries, and I did not relish using my handy decoder ring each time I wanted to search for an index that might be appropriate for the query I was optimizing.

Display File Field Description (DSPFFD) is a wonderful utility if you like browsing through green data, but what I needed in this situation was a friendly listing of indexes that decoded obfuscated field names into their column headers. Such a listing would have been a handy reference to have during the query optimization process. I developed a spreadsheet, which can be downloaded from the MC Web site at www.midrangecomputing.com/mc, to aid in query optimization and illuminate the dark world of the target data.

The Components

This spreadsheet uses ActiveX Data Objects (ADOs) to retrieve a list of indexes for a target physical file on the AS/400. It then uses another ADO method to retrieve column headers for fields in the target physical file so it can print a report of available indexes. The macro that produces the report uses a technique called disconnected recordsets to manipulate the collection of column headers as it iterates through each index entry retrieved from the AS/400. This technique is a valuable addition to your toolbox because it allows you to massage and manipulate recordsets that you obtain from databases. The resulting spreadsheets are also useful, so let me walk you through the macro so you can see how it’s done!


First the user of the spreadsheet enters the library, table name, data source name (DSN), user ID, and password in an Excel form. Clicking the List Indexes button executes the macro GetIndexInfo. GetIndexInfo starts by declaring an ADO connection object and connecting to the specified data source. (This spreadsheet is not AS/400-specific; it connects to any ODBC data source and produces an index report on any defined database table.)

ADO connection objects have a method available called OpenSchema, which can be used to retrieve information about the database that ADO is connected to. The OpenSchema method takes arguments that allow you both to specify the type of information that you want to retrieve about the database and to limit the scope of the query to only certain tables, views, or indexes. The first argument of OpenSchema is an enumerator (which is an integer constant) indicating the schema that you want to open. (There are enumerators for column information, indexes, tables, and other listings that you can ask for.) The second argument is actually an array of arguments that affect records returned by OpenSchema. Each different schema enumeration takes different arguments, which can be found in the Microsoft Platform SDK documentation for database access, ADO, and OLE DB.

Consider the adSchemaColumns enumerator for a moment. This enumerator returns a recordset containing a list of database columns. If it is called with all arguments empty, it returns all columns in all tables in the entire database. (This could take quite a lot of time, so don’t try this at home!) Here are two examples of arguments passed to OpenSchema requesting the adSchemaColumns collection:

Set x=Con1.OpenSchema(adSchemaColumns,array(Empty,”F”,”B”,Empty))
Set x=Con1.OpenSchema(adSchemaColumns,array(Empty,”F”,Empty,Empty))

The keyword Empty specifies that you do not care about that particular argument to the query. The first example creates x as a recordset containing all columns for the B table in library F on your AS/400; the second returns a recordset of all columns for all tables in library F on your AS/400. Once you have the recordsets in hand, you can easily navigate the recordset to find information about columns in your physical files.

The Macro

The macro detailed in this article is available as an Excel spreadsheet that you can download from the MC Web site at www.midrangecomputing. com/mc/. The only requirements for running the macro are some sort of ODBC connection available to your AS/400, such as the Client Access ODBC driver, and a good version of ADO installed and referenced. ADO can be obtained at www.microsoft.com/data, and, to reference ADO in the spreadsheet, go to Tools/References in the Visual Basic Editor and make sure that ADO is selected.

The macro, a snippet of which is shown in Figure 1 (you can download the code in its entirety at www.midrangecomputing.com/mc/), first generates a recordset containing all column definitions for the specified physical file. This is accomplished by using the OpenSchema method with the adSchemaColumns enumerator and by passing the target library and table name that the spreadsheet user specified. Next, the spreadsheet uses the MoveLast method of the created recordset to move to the last record, ensuring that all column information has been returned to the client recordset. The spreadsheet then sets the connection property of the recordset to Nothing, disconnecting the recordset from the AS/400. At this point, the recordset is still active and can still be searched, but all searches and manipulation are local to the client and do not involve positioning the cursor on the AS/400, resulting in significant performance improvement when navigating the column recordset.

The macro then opens a recordset of indexes available on the target physical file. This is accomplished by using the OpenSchema method with the adSchemaIndexes enumerator and by again passing an array of arguments that indicate the physical file and


library you are interested in. The macro goes into a while loop (so it can process each record in the recordset) and begins printing index information into the spreadsheet. Inside the while loop, things get interesting. The first if/then condition checks whether or not the index name has changed. If it has, the macro moves down the spreadsheet one line and prints both the name of the index (offset 5 in the recordset) and whether or not the index is unique (offset 7). It then sets the IXNAME variable equal to the current index name it is processing so the index name is not printed unless it changes. (Fields returned by the OpenSchema method are also documented in the Microsoft Platform SDK data access documentation.)

Next, the macro examines offset 20 in the recordset. This offset indicates whether the column indexed is returned in ascending or descending order. The macro uses this value to print the appropriate human-readable response on the spreadsheet. The macro then prints the name of the indexed column (offset 17) and uses the column name to set a filter on the column recordset. The filter is just like an SQL WHERE clause. The filter restricts the disconnected recordset to only records that match the filter condition.

The column recordset has a field in it called COLUMN_NAME. The macro is saying to show only records in the column recordset where COLUMN_NAME equals this column. The other records in the recordset do not disappear; they are just masked by ADO so it appears to the program that only one record is in the recordset. A byproduct of setting a filter against a disconnected recordset is that the record pointer is moved to the first record in the set that matched the criteria. The next line, “if not CS.EOF,” ensures that a matching column definition is found for the indexed column. When a matching column definition is found, the macro prints offset 27 of the column recordset, which represents the column description of the database field. The macro then moves to the next index record via the MoveNext method and loops.

When all index records have been processed, the macro drops out of the loop and defines the PrintArea of the spreadsheet. PrintArea is a property of the PageSetup object, which is associated with the current worksheet. Defining the PrintArea ensures that, if the spreadsheet user clicks File/Print, the spreadsheet prints only information returned by the last call to the macro and no empty or erroneous cells. I find this to be a nice convenience to add to any spreadsheet, so I wanted to make sure that I pointed the method out to you.

Where Can You Go from Here?

Disconnected recordsets give you the ability to retrieve a recordset from your AS/400 and then peruse it at your leisure during the processing of a macro or script. An area where you might use this technique is typically small recordsets (usually those with fewer than 2,000 records), where it makes sense to retrieve data once and then use the filter or find methods to navigate the data during processing. For example, I built a graphical front-end to analyze sales data for a company. The list of salesmen had about 800 records but was needed by several different screens in the application. I retrieved the recordset at the start of the program and never had to hit the AS/400 for that information again. This technique saves bandwidth and makes the application appear much more lithe to users, as the data is retrieved only once but used over and over by different processes and screens.

In the index report generated by this spreadsheet, you also have a neat little utility that may help you during optimization of Query/400 and SQL queries. Explore other schemas that can be returned by the OpenSchema method, and you may find other information that you want to retrieve. There’s a wealth of info out there.


‘... Code that dims objects and
‘... connects to AS/400 and prints all of the
‘... column headers is here but removed for space

‘... Open CS which is a recordset
‘... of columns in the target table

ArgArray = Array(Empty, Library, Table, Empty)
Set cs = con1.OpenSchema(adSchemaColumns, ArgArray)

‘move to end of recordset and disconnect
cs.MoveLast
cs.ActiveConnection = Nothing
cs.MoveFirst

‘... Open the list of indexes

ArgArray = Array(Empty, Library, Empty, Empty, Table)
Set rs = con1.OpenSchema(adSchemaIndexes, ArgArray)

‘r is cussent offset of spreadsheet
r=5
‘while we read indexes
While Not rs.EOF

‘...code to print index info
‘...removed for space
‘next line gets the field name FldName = rs.Fields(17).Value

‘set the filter on the cs recordset
‘to only show the current column cs.Filter = “COLUMN_NAME = ‘“ & FldName & “‘“

‘if we found the column description
‘print the column description of the field If Not cs.EOF Then

ActiveCell.Offset(R, 4).Value = cs.Fields(27).Value

End If

‘move to the next line of the spreadsheet

R = R + 1

rs.MoveNext
Wend
‘set the print area to be only what we just retrieved
Worksheets(“TableIndexes”).PageSetup.PrintArea = “A1:E” & R + 4
Application.ScreenUpdating = True

‘move the pointer to the top of the spreadsheet
Range(“A1”).Activate

Figure 1: This macro snippet retrieves the list of indexes for an AS/400 table and uses a disconnected recordset to add the column descriptions to the spreadsheet.


BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: