The next step is to create the event procedures needed to complete the application logic.
Hopefully, as you complete this process, you will be quite impressed at how little time you will spend developing such an application.
The application will make use of one global variable to monitor the open/close state of the database.
Instead of placing a third command button on the window and writing code for it to open the database, you will use the global variable and some clever programming logic to place the database open function in the "Retrieve" command button's event procedure.
The logic you are going to write will use the single Retrieve command button. When it is initially clicked, it will sense that the database is not open and will open it. It will also perform a "SelectRecords" function call to select all of the database records.
The logic will then perform a "ReadNextRecord" until it reaches the end of the database table unless you change the value of the EMPLOYEE-ID field. If you change the value of the EMPLOYEE-ID field, it will attempt a specific "SelectRecords" function using the value you enter.
Instead of looking for an exact match when doing a specific "SelectRecords" function on a given EMPLOYEE-ID value, the application will use the greater than or equal (>=) operator to find the next match.
As complicated as this may sound, you are going to accomplish this using only a few lines of procedural code in PowerCOBOL.
You will first define the global data item needed to keep track of the current database open/close status. You are going to use a global variable so that you will be able to interrogate and set it from both of the separate event procedures you will define later.
Remember that data items defined in an event procedure are local data items to that procedure and may not be accessed directly from other event procedures.
You will thus define a global data item at the form level, which will allow any of the form's event procedures to access it directly and thus share it.
You will also define a second global data item (ReturnValue) that will be used by all ODBC calls to place a return value into, indicating success or an error code.
Make sure that you are currently viewing the application from the Project Manager window as shown in Figure 8.18.
Move the mouse to the MainForm form (the application's window) and right-click on it. From the pop-up menu that appears, move the mouse over Edit DATA DIVISION to bring up a secondary pop-up menu. Select WORKING-STORAGE.
This will bring up the PowerCOBOL Editor. Because you have not previously defined any WORKING-STORAGE items for the form, the Editor window will be empty. Enter the following lines of code in the Editor window and then save it and close the Editor:
Now it's time to create the main logic portion of the code that will be associated with the Retrieve command button.
Note that the object name assigned to the DB Access control by PowerCOBOL is CmDB1. This control has been connected to the MYODBC ODBC data source name that you defined earlier.
Move the mouse to the PUSH-RETRIEVE command button definition in the left windowpane of the Project Manager window (you may need to expand the project to see all application components if the PUSH-RETRIEVE object is not displayed).
Right-click on it once with the mouse to display a pop-up menu. Move the mouse over Edit the event procedure and select the Click event. This tells PowerCOBOL that you wish to create an event procedure for the Click event (a click event takes place when the user moves the mouse over the command button and left-clicks on it).
This will bring up the PowerCOBOL Editor to allow you to enter the COBOL code associated with this event procedure. Enter the following code in the Editor window:
When you are finished, save the edit session and close it.
Now it's time to write the final code snippet for the Close command button. When the close button is selected, you need to close the database and exit the application.
In the Project Manager window, move the mouse to the Close command button definition and right-click on it to display a pop-up menu. Move the mouse over Edit the event procedure and select the Click event from the secondary pop-up menu that appears.
The PowerCOBOL Editor appears, ready for you to input the event procedure. Type in the code as shown in the following example:
Notice the use of the control name "POW-SELF". In PowerCOBOL, POW-SELF contains the name of the current form. It is a convenient short cut to specifying the name of the current form. You could have optionally coded "MainForm" where POW-SELF appears. If you changed the name of "MainForm" later, however, you would be forced to change the name in any event procedure that references it. By using "POW-SELF", however, you need not worry if the name of the form changes.
Incidentally, if you would like to see a list of available database-related data fields (properties) and functions (methods), highlight the "CmDB1" string in the Editor window, and click on it once with the right mouse button.
From the pop-up menu that appears, you can select either Insert Method or Insert Property to see a list of either. If you select a property or method from one of these lists, PowerCOBOL will automatically build the appropriate procedural COBOL statement and place it into the Editor at the point of the cursor.
Save and close the edit session. You have completed the development portion of this project. That's all there is to it.
Select Save from the File menu to save the new additions to the project.
You are ready to build your application and run it.