Newsletters Subscriptions Media Kit About Us Contact Search Home

Stuff
OS/400 Edition
Volume 2, Number 13 -- June 19, 2003

Data Entry Robots


by Michael Sansoterra

[The code for this article is available for download.]

Sometimes undeserved credit can cause inflated expectations for future projects! I had just such an experience a few years back at a client's, after having a few simple successes that gave the impression I could do anything quickly. I was asked to load some data from a PC-based Microsoft Access database into an iSeries application. The amount of data (i.e., fields) was small, but the number of records was considerable and the expectation was that it could be done rapidly.

I set out to learn the structure of the destination iSeries tables and populate them from the PC using ODBC. Unfortunately, I had major difficulties in populating the tables due to a large number of flags and counters that I just couldn't figure out how to set in a short time. The hourglass to complete the project was draining fast, and I didn't want to have to tell the customer to hire data-entry personnel.

Another option would have been modifying the application's data-entry program to use my input files rather than the screen. It became increasingly apparent that I would have to heavily modify some RPG/400 code to use my input files rather than loading the tables directly.

I pondered entering the data myself. Blast! If I only had someone to quickly enter the data. Just then the light bulb went on. I did have someone--or something. That something that could enter my data is the Personal Communications (PCOMM) terminal emulator package, which comes with Client Access (known as iSeries Access since V5R2).

PCOMM is more than just a terminal emulator. It has a Component Object Model (COM) interface that allows Windows programmers to control its entire operation. Armed with this information, I was able to construct a computerized data-entry robot to read data from an Access database and enter it on the 5250 screen.

Actually, my original program did nothing more than send keystrokes to the application's data-entry screen. For the most part this was fine, except that every once in a while a data-entry error occurred, which would send my data-entry robot into orbit because it kept feeding data to the emulator even though the emulator wasn't accepting it! Later investigation revealed that an intelligent robot could be built to respond to specific events on the 5250 screen, such as monitoring for a keyboard-error condition or looking for a specific message on the screen.

Building the Robot

Say you're faced with the formidable task of entering history data for a set of items. You've come to the conclusion that populating the underlying tables is proving difficult and that modifying an application program is lengthy. Figure 1 shows a simple sample screen that needs to be filled in.

Figure 1

Figure 1: A data-entry screen is used to fill in data

It's not the most intricate screen in the world (and it only populates one table!), but this is for an example, so simplicity is important! How do you go about letting the computer enter data?

There are a few simple steps to preparing a robot to key the data.

Step 1: Identify all of the data-entry screens that will be required to receive the necessary information. For each screen, construct a grid containing information about the fields that need to be populated or analyzed by the data-entry robot. Running the Display File Field Description (DSPFFD) command against the display file will help you collect screen coordinates and edit code information. Table 1 shows a grid for the Maintain Item History screen.


Field Description Y Pos (Row) X Pos (Col) Length Numeric Scale Sign (L/R) Output Only?
Title 1 30 21 - - Y
Item Number 4 8 15 - - -
Item Description 4 31 30 - - Y
Unit of Measure 4 62 4 - - Y
Sales Year 6 14 4 0 - -
Qtr 1 Qty 10 9 7 0 R -
Qtr 2 Qty 11 9 7 0 R -
Qtr 3 Qty 12 9 7 0 R -
Qtr 4 Qty 13 9 7 0 R -
Qtr 1 Sales 10 21 9 2 R -
Qtr 2 Sales 11 21 9 2 R -
Qtr 3 Sales 12 21 9 2 R -
Qtr 4 Sales 13 21 9 2 R -
MsgLine 20 2 70 - - Y

Table 1: A sample grid with important field information to be analyzed or filled in


The grid consists of a field description (the actual field name usually isn't important), the field's screen coordinates and length, the number of decimals, whether the sign is on the left or the right (which is important to a 5250 screen), and whether the field is used for output or both input and output.

Step 2: Get to know your data-entry screen. You must know how the application screen works if you're going to program the computer to work with it. In particular you need to identify the order in which the fields need to be entered, if a function key needs to be pressed after a field is entered (such as Enter or Page Down), and whether an error can occur after a function key is pressed. Collect this information and put it in a grid, or pseudocode. Table 2 shows a sample grid illustrating the steps needed to enter data in the Maintain Item History screen.


Rule Condition Action
Enter Item Number - -
Press Enter If 'Invalid Item' message Log Error
Restart
Enter Sales Year - -
Enter Qtr 1 Qty - -
Enter Qtr 2 Qty - -
Enter Qtr 3 Qty - -
Enter Qtr 4 Qty - -
Enter Qtr 1 Sales - -
Enter Qtr 2 Sales - -
Enter Qtr 3 Sales - -
Enter Qtr 4 Sales - -
Press Enter twice If "History Already Exists" warning message Log Error
Press F12
Restart

Table 2: Sample grid containing important field information to be analyzed or filled in


Step 3: Start the coding in your favorite Windows environment. Your newly devised tables from steps 1 and 2 will be instrumental in helping your code move along quickly. On the PC side, Figure 2 shows a Microsoft Access table called tblItemHistory, which stores the data that needs to be entered in the iSeries.

Figure 2

Figure 2: A Microsoft Access table feeds the robot information

In your PC application, set references to the following PCOMM COM objects. (In Visual Basic this can be done by choosing Project, then References. In Access it can be done in the VBA [modules] editor by choosing Tools, then References.)

  • PCOMM autECLConnMgr Automation Object 1.0 Library
  • PCOMM autECLOIA Automation Object 1.0 Library
  • PCOMM autECLPS Automation Object 1.0 Library
  • PCOMM autECLWinMetrics Automation Object 1.0 Library

Table 3 has a brief description of each of these PCOMM objects.


PCOMM Object Description Function
AutConnMgr Connection Manager Contains collection that references the open PCOMM sessions.
AutPS Presentation Space Returns information about data displayed on PCOMM screen; also has ability to place data at specific coordinates on PCOMM screen.
AutWinMetrics Window Metrics Allows retrieval and setting of emulator window characteristics, including title bar text and window state (minimized, maximized, etc.).
AutOIA Operator Info Area Returns information about host connection state of PCOMM emulator window (input inhibited, message wait, system available, etc.).

Table 3: Four important PCOMM COM objects are necessary to control the emulator


The sample Access database, developed in Microsoft Access 2000, along with the iSeries source that I used for this article, is available for download. To use this example, you have to compile the physical file, display file, and program objects on the iSeries. You also have to populate the iSeries item master file with sample data for item numbers 125,127,128, and 150. Finally, you need to have an emulator session open running the sample item master maintenance program, by issuing the command CALL ITEMHISTR.

The first thing the code does is to retrieve the number of open 5250 sessions from the connection manager:

intSessCount = pcommConnMgr.autECLConnList.Count

Assuming that one or more sessions are found, the code loops through each session and retrieves a handle (or reference) for each PCOMM session. For each session handle, the "presentation space," "windows metrics," and "operator information area" objects are attached to the given session:

pcommPS.SetConnectionByHandle lngSessionHandle
pcommWin.SetConnectionByHandle lngSessionHandle
pcommOIA.SetConnectionByHandle lngSessionHandle

Once attached, you can use these COM objects to retrieve and modify the session's attributes. As noted in Table 3, setting a reference to the presentation space object for the current session will allow us to peek at what's on the screen for the current session. In particular, the GetText method is used to read 21 characters starting from coordinate (1,30) to test if the session has our data-entry screen open (as identified by title):

If pcommPS.GetText(1, 30, 21) = "Maintain Item History" Then

In this code, the program is assuming that at least one session will be waiting at the desired screen. (Our robot can be programmed to start all the way back at the sign-on screen, if needed.) If the current session is not running the desired program, the robot looks at the next session. Instead of looking for a program title, it's common to search for a program name as well, or both--as long as the robot program can uniquely identify a screen.

When the proper screen is found, the data-entry robot invokes the pcomm_EnterData subroutine and passes the current session's presentation space and operator information area. This is the routine where all the real data-entry work is done. The local data-entry table is opened, and a loop is begun to process all rows.

The first data-entry step is to enter the item number at coordinates (4,8) using the presentation space's SetText method, then press Enter, using the SendKeys method. The code waits for the emulator to become available by invoking the WaitForInputReady method of the OIA object. According to the rules laid down in Table 2, you check the message line window of the screen to make sure that this is a valid item number.

If the item number is invalid, you log an error and continue with the next item; otherwise, fill in the remaining fields on the screen using the SetText method again. Of particular importance is making sure that the sign of a numeric field appears on the proper side (left or right), depending on the field's edit code. Once that is completed, Enter is pressed twice and then our intelligent robot checks for the "History Already Exists" warning message on the message line and takes appropriate action.

That was simple! Here are a few more pointers.

  • The GetText method will only read a single screen row. Display fields that wrap over more than one line will need special attention. GetTextRect (Get Text Rectangle) may be a better method to use for this case.
  • The SetText method of the AutPS (presentation) object doesn't change the cursor position. If you're expecting the cursor to be at a specific place, so you can send a special key such as field exit or field -, use the SetCursor method first.
  • An alternative to SetText is the SendKeys method, to send all of the data. When using SendKeys you will need to instruct the computer down to every last keystroke (including tabs, new lines, and field key strokes). You should also check for keyboard errors when using this technique.
  • After each function key is sent, it is a good idea to monitor the operator information area object to make sure that the application is ready to receive more information. In particular, you could check to make sure the system is still available and that the emulator is not waiting for "error reset" to be pressed. It would also be wise to double-check the screen after a function key is pressed to make sure that the program didn't blow up with an error. Having the robot send information when the emulator isn't ready to receive it is bad news!
  • Dig deeper. Most of these PCOMM objects have many more properties and methods than I have demonstrated.
  • If you can't figure out how to make the emulator do something, let the macro recorder show you how. Just position your cursor on the screen to the desired location, choose Start Recording Macro from the Actions menu on your 5250 session, assign your macro a file name, then type away. When you've finished typing, choose Stop Recoding Macro from the Actions menu. Locate and open the macro you just recorded with notepad. You will see a VBScript program that tells the emulator to do exactly what you just typed in. Further, the generated script will also contain some goodies on how to do certain things like waiting for a particular attribute to appear on the screen. When I want to learn how to do something, I let the macro recorder tutor me!
  • The sample code illustrated here contains hardcoded coordinates for each screen field, which is okay for one time tasks. A dynamic application could use a table lookup to retrieve each field's coordinates. That would make synchronization between the display screen and the data-entry robot easier if changes were made to the display file.
  • When using SendKeys, the 5250 function keys are enclosed in square brackets and are generally in the following form:
Enter   [enter]
Field Exit   [fldext]
F1-F24   [pf1-pf24]
  • I wasn't able to find a comprehensive list of function key codes in the PCOMM help. However, I did find the list of function keys for the Host On Demand product, which seems to be compatible with PCOMM's SendKeys. This list can be found on IBM's Web site.

Save Your Fingers and Brain for Other Things

While a data-entry robot definitely has its benefits, it does have some drawbacks. The data-entry screen coordinates may not be moved without having to reengineer the robot. A bullet-proof application takes a while to code, since each entry field would have to be followed by a large number of checks to verify, among other things, that the system is available, that an error message didn't occur, and that error reset is not waiting to be pressed.

Nonetheless, by doing a reasonable coding job, most data-entry applications coded with basic error checking will do fine. Most of the time, they can be tweaked for special cases as they're encountered. If you're looking to avoid the headaches and dangers of populating data in an application's tables, and you want to be sure your data-entry person won't botch the keying process, then this technique is for you.


Michael Sansoterra is a programmer/analyst for SilverLake Resources, an IT services firm based in Grand Rapids, Michigan. E-mail: msansoterra@silver-lake.com


Sponsored By
DAMON TECHNOLOGIES

The Next Evolution of RPG is Here!

RSP (RPG Server Pages) is the best way to develop Web applications with RPG.

  • Developers use their existing RPG skills.
  • More robust than CGI with greater flexibility.
  • RSP is not just visual development. It is an application server built specifically for the iSeries.
  • Full debug capabilities.
  • Session Handling with a built in garbage collector.
  • The most cost effective solution for the iSeries.

With RSP, Web content is developed with the Ease, Speed, and Reliability of RPG.

In today's fast paced business world, there is not enough time or resources to convert RPG developers into Java developers. The logical step to bring your business critical applications to the Web is with RSP. RSP gives the developer the tools necessary to create fast and reliable Web applications.

Download your FREE copy of RSP today!

www.damontech.com


THIS ISSUE
SPONSORED BY:

T.L. Ashford
Damon Technologies
WorksRight Software
Profound Logic Software


BACK ISSUES

TABLE OF
CONTENTS
Making HTML as Functional as a Green Screen

Data Entry Robots

Dr. Programmer, Private Eye

Back To Basics: Edit Words


Editors
Shannon O'Donnell
Kevin Vandever

Managing Editor
Shannon Pastore

Contributing Editors:
Howard Arner
Raymond Everhart
Joe Hertvik
Ted Holt
Marc Logemann
David Morris

Publisher and
Advertising Director:

Jenny Thomas

Advertising Sales Representative
Kim Reed

Contact the Editors
Do you have a gripe, inside dope or an opinion?
Email the editors:
editors@itjungle.com


Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.