How to Get VB to Talk to a Mainframe, AS40, Etc.
By Michael Doyle.
Mainframe systems tend to be very hard to modify. Often it is faster and easier to write a PC application that uses a terminal emulator to pull data from the mainframe rather than writing new code on the mainframe. To the mainframe, the data capturing application looks just like a user typing on a terminal. The capture program "looks" at the screen and pulls data off of it (hence the term "screen scraping"). Once you have the data on the PC, you can write a Visual Basic program to analyze it.
In this tutorial, Michael Doyle explains the basics. Your terminal emulation software may have specific library routines for making this easier.
|
This is an old but useful method for quickly getting data from a legacy database. In essence, you turn your terminal emulator into a COM object, so that VB can manipulate it. I have personally used this method on an IBM mainframe (PC posing as a Telnet 3270 terminal), a Unix system (PC posing as a VT100), and an AS400 (PC posing as a Telnet 5250 Terminal).
(You may have to check your terminal emulator's documentation for specifics on how to modify this code to fit your needs.)
This example will take a data file containing customer numbers, read those numbers into a mainframe screen, get the customer's name, and print the results to a text file.
STEPS:
- Search your terminal emulator's subdirectories for some type of HLAPI.dll.
- In VB, set a reference to that DLL. (In some cases, the emulator's documentation will tell you to set a reference to an executable instead.) You should then be able to see the emulator object's properties, methods, and events in your Object Browser. (Hit [F2] in the VB IDE.) This will enable you to get far more fancy than we will here.
- Write the code:
- Create the emulator object. ("HostExplorer", in this instance, is emulator-specific. You will have to modify that line according to your own documentation.)
Dim Mainframe As Object
Set Mainframe = CreateObject("HostExplorer").CurrentHost
Mainframe.Activate ' Makes mainframe the active window,
' so you can watch your code run...
Mainframe.Maximize |
- Dimension variables and open data files...
Dim strCustomerNumber as String
Dim strCustomerName as String
Open "MainframeCustNo.txt" For Input As #1
Open "Output.csv" For Output As #2
Do Until EOF(1)
Input #1, strCustomerNumber |
- Talk to mainframe:
' Code here to send strCustomer to the screen,
' varies according to emulator
' On "Tiger Terminal" , it would be:
result = Mainframe.ScriptExec(strCustomerNumber)
' On "HostExplorer", it would be:
Mainframe.Keys(strCustomerNumber)
' Your Mileage May Vary...
' Code here to hit the key. YMMV again here.
Mainframe.Keys("{Enter}")
' You may need a sleep function here,
' depending upon your mainframe |
- Scrape data:
' Now we use the object's "text" property,
' and a string function:
strCustomerName = Mid(Mainframe.text, position-on-screen, FieldLength)
' If there are 80 characters per line and
' your field begins in the third position of
' the second line, position-on-screen is 83.
' FieldLength is the number of
' characters you need to capture.
' Output file gets result
Print #2, strCustomerNumber & "," & strCustomerName
Mainframe.Keys("Break") ' Clear screen for another pass...
' Sleep function here, too. I don't specify,
' because some of your HLAPI's have them built in.
' See your documentation... or use a standard VB SLEEP function. |
- House keeping chores: Close loop and data files. Destroy object.
Loop
Close #1
Close #2
Set Mainframe = Nothing |
When this has run, you have a file containing the customer numbers you had in the first file, and the customers' names you pulled from the screen. (I'm sure you can think of a less trivial use for this tool.) It is a .csv file, so you can open it directly in Excel, if that's useful. If you have customer numbers (or other data) with leading zeros, you will want your output file to be a .txt file (rather than the .csv we used here), so you can tell Excel to format that field as text when you import the file.
If you just want to open the data file with another app to throw the data into a SQL database or some such place, then the text file's format does not matter.
If you're comfortable with the Excel object in VB, you can create a spreadsheet directly. (I didn't want to muddy the waters here.)
The beauty of this method is that it will work on different platforms. I suppose as the .NET framework catches on, we will no longer need this code, but in the meantime, use it in good health.
|