Sie sind auf Seite 1von 56

Appendix B: Solutions

Appendix B: Solutions

Exercise 3.1: SQLWindows Objects


Step 1: Step 2: Step 3: Step 4: Step 5: Choose File | Save or click on the diskette icon at the upper left portion of the tool bar. Choose File | New or click on the white paper icon at the upper left portion of the tool bar. From the left pane, open the Application1 folder. Open the Windows folder. Right-click on the Windows folder and choose New | FormWindow. A FormWindow will be created. Give it the name frmHello and press Enter. Step 6: In the left pane, right-click on the form frmHello and choose Properties... Give the form a title by moving your pointer to the Object Title and typing in Hello World Application. Step 7: With the form frmHello highlighted in the left pane, click on the Layout tab at the bottom of the workspace. You will now see your newly created form. To view the form full size, right-click on the forms background and choose Preview Window. Step 8: Step 9: Press Alt+4 or from the main menu choose Tools | Controls to make the Controls dialog box available. Choose a standard data field from the Controls dialog box, move your pointer to the form and click where you want to place your data field. With your pointer on the data field, right-click and click Properties Click Object Name and type in a name dfMessage. Click Editable and click No. Click on Justify and click Center and finally click Done. Step 11: Choose a push button from the Controls dialog box, move your pointer to Data Field the form and click where you wish to place your push button. With your pointer on the push button, right-click on Properties. Click Object Name and type in a name of pbSet, click Object Title and type &Set. (The & sets the accelerator for the push button, allowing the user to press Alt+S in place of clicking on the object) Step 13: Step 14: Repeat steps 4a and 4b, except this time the object name is pbExit and the object title is E&xit. We have completed the visual aspects of our application, so close down the Controls dialog box by clicking on the close button at the top far right hand corner. We now will turn our attention to writing the code. Step 15: Step 16: Start the Coding Assistant from the main menu by choosing Tools | Coding Assistant or pressing Alt+2. This solution will include the optional exercise that will alternately set or clear the data field dfMessage. For this to work, we must create a Boolean Variable. To do this, in the left pane highlight the form frmHello and in the right pane click on the tab Variables. Create a Boolean Variable named bValueSet.Push Button.

Step 10:

Step 12:

Page 2

Building Applications with SQLWindows

Appendix B: Solutions

Step 17:

Open the Child Windows folder in the left pane. (you will see all the child objects you have created so far).

Step 18:

In the left pane, click on pbSet and in the right pane, click on tab Actions.Using the Coding Assistant, duplicate the code pictured below. Now write the code for the pbExit push button. (the code is shown below) Press F7 to save and test your application. Save it as Chapter 3 Exercise 1.app.

Step 19: Step 20:

Gupta Technologies, LLC

Page 3

Appendix B: Solutions

Exercise 3.2: Using Dockable Application Toolbars


Step 1: Step 2: Step 3: Choose File | Save or click on the diskette icon at the upper left portion of the tool bar. Open the Chapter 03 Template 2.app file.

Step 4:

Step 5:

Save and test your application. Try and making it a floating toolbar See if you can move it to the bottom or the sides

Page 4

Building Applications with SQLWindows

Appendix B: Solutions

Optional Tasks
Step 6:

Gupta Technologies, LLC

Page 5

Appendix B: Solutions

Exercise 4.1: Event/Message Programming


Step 1: Step 2: Step 3: Click on File | Save, or select the diskette icon on the toolbar. Click on File | New, or select the blank page icon on the toolbar. In the left pane, right-click on the Windows tree item. From the pop-up menu, select NEW | Form Window. The window will surface in layout mode in the right pane. Double click on it and set the Object Name attribute to frmCustomerEntry. Set the Object Title attribute to Customer Entry. Step 4: If the Control Palette is not presently visible, type Alt + 4. Using the data field tool, drop and size a field on the form. Double-click on it to set its Object Name attribute to dfName. Using the background text tool, add suitable background text. Repeat for fields: dfAddress dfCity dfState dfZipCode dfCountry dfCreditLimit (In each case, double click on the item to set the requested attribute) Step 5: As we are done with the visual part of the application, we want to switch to outline view. In the right pane, click on the Outline tab. Review and duplicate the code snippets following.

Page 6

Building Applications with SQLWindows

Appendix B: Solutions

Step 6:

Choose File | Save as and type in the name Chapter 4 Exercise 1.app.

Gupta Technologies, LLC

Page 7

Appendix B: Solutions

Exercise 4.2: An OO Approach to Event/Message Programming


Step 1: Step 2: Choose File | Save, or click on the diskette icon at the upper left portion of the tool bar. Open the Chapter 04 Template 1.app file (see beginning of Solutions Appendix for help), or open your completed exercise from chapter three, Chapter 03 Exercise 1.app. Whichever file you are, choose File | Save as and type in the name Chapter 04 Exercise 2.app. Step 3: In the left pane, click on the application name and in the right pane click on the Constants tab. Create a global, user Numeric constant defined as PAM_Messages = SAM_User + 1. Step 4: If you did not complete the optional exercise in Chapter 5, take the following steps. First, in the left pane, navigate to your form frmMain or whatever you named it in Chapter Five. Second, in the right pane, click on the variables tab. Create a new variable of Boolean data type and name it bValueSet. Step 5: Modify the code for pbSet: Send a message to all the children of the form Send a variable in wParam that may be either TRUE or FALSE (the best way to do this is send bValueSet in the wParam position)

Step 6:

Modify the code for dfMessage: Trap for the message PAM_Messages When the message is received, evaluate wParam If wParam is TRUE, set the data field value = Hello World If wParam is FALSE, clear the data field

Page 8

Building Applications with SQLWindows

Appendix B: Solutions

Step 7: Step 8:

Test and debug. Test your theory: In the left pane, click on the form and in the right pane, click on the Layout tab. Click on the forms background, right-click and choose Preview. Resize the form and the optional frame to be a bit longer, similar to the one in the Preview. Copy and paste several versions of the data field. In the code, append the numbers 2,3 and 4 to names these data fields and run the application. Each data field you duplicated should behave exactly the same as your first. If so, you have completed the exercise correctly. If not - ask your instructor for ideas to fix the problem.

Gupta Technologies, LLC

Page 9

Appendix B: Solutions

Exercise 5.1: A Simple Event-Based Application


Step 1: Step 2: Choose File | Save, or click on the diskette icon at the upper left portion of the tool bar. Choose File | New, or click on the white paper icon at the upper left portion of the tool bar. Choose File | Save as and type in the name Chapter 05 Exercise 1.app. Step 3: In the left pane, click on the application name. In the right pane, click on the Constants tab. Define all the PAM messages and any other constants you and your instructor have identified.

Step 4:

In the left pane, open the application folder. Right-click on the Windows folder and choose New | Form Window. Type the name of the form, SALCulator.

Step 5:

The Layout tab is already selected for you. Right-click anywhere on the forms background and choose Preview Window. Right-click on the form and choose Properties | Object Title and type in SALCulator.

Step 6:

Press Alt+4 to bring up the Controls Palette. Choose a data field from the Controls Palette and place it on the form. Press Alt+3 to bring up the Attribute Inspector with the focus on the data field and choose Properties Provide an Object Name of dfnVar1, change the Data Type to Numeric, Format to #,##0.00 and right Justify.

Step 7:

Now, copy this data field two times. Change the names of the two new objects to dfnVar2 and dfnResult. Right-click on dfnResult, choose Properties | Editable and set it to No.

Step 8:

Create background text, a frame and a group box for readability.

Page 10

Building Applications with SQLWindows

Appendix B: Solutions

Step 9:

Choose push button from the Controls Palette and drop it next to the Add background text. Type in the + key from the key board as the title and press Enter. Resize the push button to fit in the space you have provided. Right-click, choose Properties, provide a name of pbAdd and choose Done.

Step 10:

Right-click and choose Explore | Actions. (this is where we will write our first bit of code)

Step 11:

When completed, in the left pane, highlight the form SALCulator and choose the Layout tab. Now, repeat the steps above for pbSub, pbMul and pbDiv. Remember to replace the constant ADD with the appropriate constant for each push button. (to speed your development, you could simply copy the pbAdd push button and change the name and constant used in the code)

Step 12:

Now, create the push buttons and code for pbClear and pbExit. The respective code for each is shown below.

Step 13:

In the left pane, click on the Form Window SALCulator. In the right pane, click on the tab Actions and create the code to pass messages received from the push buttons to all the children of the current form.

Gupta Technologies, LLC

Page 11

Appendix B: Solutions

Step 14:

Code at the data field level covers two aspects. The first is the PAM_Clear, which causes each data field to clear its own value. This code will reside within each data field.

Step 15:

The second is the PAM_Calc message which, when received, dfnResult will extract the value of wParam and calculate the correct result. For PAM_Clear, navigate in the left pane to the data field dfnVar1 and in the right pane, choose Actions tab and create an On PAM_Clear block of code as pictured below. Highlight the code, right-click and choose copy. Navigate in the left pane to dfnVar2, in the right pane click on the Actions tab, right-click and Paste the code you just copied from dfnVar1. Repeat this for dfnResult.

Step 16:

We have all the data fields set so that they receive a message to clear. Each has a method that calls the SalClearField function. Now, we must write the code for dfnResult to perform the appropriate arithmetic (code illustrated below). Below is the case statement that deals with add, subtract and multiply.

Page 12

Building Applications with SQLWindows

Appendix B: Solutions

Step 17:

Above is the code required for the divide logic too. We will look at the part of the case statement that deals with division, and the code to ensure the user cannot divide by 0!.

Step 18: Step 19:

Choose File | Save , or click on the diskette icon at the upper left portion of the tool bar. Press F7 and test your work.

Gupta Technologies, LLC

Page 13

Appendix B: Solutions

Exercise 7.1: SAL Functions


Step 1: Step 2: Choose File | Save, or click on the diskette icon. Choose File | Open, or click on the file folder icon at the upper left portion of the tool bar. Choose the file you wish to work with, either your application Chapter 05 Exercise 1.app or the template Chapter 07 Template 1.app. Choose File | Save as and type in the name Chapter 07 Exercise 01.app. Step 3: Bring up your form and duplicate the divide push button. Rename it pbError and change the title to Test Error. Locate and resize it to look like the sample in the preview. Step 4: Step 5: In the Actions tab for pbError, modify the code to pass the number 10 in wParam where you now have the constant DIV. In the left pane, open the Function folder of your Form Window. Right-click and choose New | Function. Enter the name fnMyCalc and a suitable description. Step 6: Click on Parameter tab and create four parameters: nOperator: keeps track of the mathematical operation to be performed nArg1: for the first value being sent nArg2: for the second value being sent nResult: for the end result Each of these are numeric, but one must be defined as a Receive Number. You decide which one it should be! Step 7: To keep track of whether we were successful in executing the function, we will define a variable bSuccess of Boolean data type in the Variables tab. Since we already have the logic required to perform the interrogation of wParam, and carry out the desired mathematical operation defined under the data field dfnResult, it would be prudent to copy that code from the data field to the function fnMyCalc actions section. Modify the code in the function fnMyCalc to match the code below. Notice that you must change all references of data fields to the appropriate parameter. Also, notice that you may NOT use MyValue here, you must replace it with nResult.

Step 8:

Step 9:

Page 14

Building Applications with SQLWindows

Appendix B: Solutions

Step 10:

Modify the code under dfnResult to call your function. (remember to provide error checking!)

Step 11:

Press F7 to test your work.

Gupta Technologies, LLC

Page 15

Appendix B: Solutions

Exercise 9.2: Inserting an Object into a Container Control


Step 1: Click on File | New, or select the blank page icon on the toolbar. Click on File | Save, or select the diskette icon on the toolbar, and name your application Chapter 09 Exercise 1.app. Step 2: In the left pane, right-click on the Windows tree item. From the pop-up menu, select New | Form Window. The window will surface in layout mode in the right pane. Step 3: Use the Attribute Inspector to set the Object Name attribute to frmAgeCulator. Set the Object Title attribute to AgeCulator. Step 4: If the Controls Palette is not presently visible, type Alt + 4. Using the background text tool, drop and size background text onto the form. Enter the text 'Please Select your Date of Birth'. Step 5: Using the ActiveX Control Tool, drop and size a Calendar 8.0 control onto your form window.

Step 6: Step 7:

Use the Attribute Inspector to set its Object Name attribute to axCalculator. Using the data field tool, drop and size a field on the form. Double-click on it to set its Object Name attribute to dfAgeDays. Using the background text tool add suitable background text.

Step 8:

As we are done with the visual part of the application, we want to switch to outline view. In the right pane, click on the Outline tab. Review and duplicate the following code snippets:

Step 9:

Page 16

Building Applications with SQLWindows

Appendix B: Solutions

Step 10:

Click on File | Save, or select the diskette icon on the toolbar. Name your application Chapter 09 Exercise 1.app.

Gupta Technologies, LLC

Page 17

Appendix B: Solutions

Exercise 9.5: Word Automation


Step 1: Run the sample application which demonstrates ActiveX automation with Microsoft Word 97 or Word 2000. If you are use the wrong template for the version of Word on your system, you will get an error message that the Microsoft Word X.0 Object Library.apl cannot be found. This application uses Microsoft Word 8.0 Object Library.apl (or Microsoft Word 9.0 Object Library.apl if you are using Word 2000). You should have generated this library during Exercise 9-4, but ensure that this library is still available on your machine (typically in <Team Developer Install directory>\AxLibs) before running this sample application. Step 2: Click on the Preview Letter button on the frmServer window. frmServer invokes Word as an out-of-process server and gets Word to load the document called ActiveX sample letter.doc, which should be located in your student files directory.

Step 3:

Add a name to the Name datafield in the frmContainerControl. Then click on the Set Value push button. This form embeds the same Word document as a file into the Container control. The file isn't required (after insertion), as the document becomes part of the application. The two windows work totally independently of each other. They are there just to demonstrate different coding techniques. Usually you would either embed a Word document in your application, or invoke it in a separate instance of Word in its own window.

Page 18

Building Applications with SQLWindows

Appendix B: Solutions

Gupta Technologies, LLC

Page 19

Appendix B: Solutions

Exercise 10.1: SQL Basics


Step 1: Step 2: Invoke Database Explorer by choosing from the main menu Tools | Database Explorer. In the left pane, expand Database Explorer, expand SQLBase and click on PRESIDNT. In the right pane, respond to the login tab with PRESIDNT as the Database name, PRESIDNT as UserId and Password. In the right pane, click on SQLTalk Script tab. You may now enter the SQL statements. Remember the semicolon and to right-click on the statement and choose Execute SQL.

Step 3:

SELECT pres_name FROM president WHERE state_born = 'Texas'; SELECT pres_name FROM pres_marriage WHERE nr_children > 4; SELECT p.pres_name , p.party FROM president p , winner w WHERE p.pres_name = w.name AND w.year_elected = 1972; INSERT INTO president ( pres_name , state_born , birth_date ...) VALUES (your_name , etc...............); SELECT p.pres_name , m.spouse_name FROM president p , pres_marriage m WHERE p.pres_name = m.pres_name(+) AND p.death_age is null; DELETE FROM president WHERE pres_name = 'your_name'; COMMIT;
Step 4: Close the Database Explorer down.

Page 20

Building Applications with SQLWindows

Appendix B: Solutions

Exercise 11.1: Working with SQL Databases


Step 1: Step 2: Choose File | New to create a new application. Create two global variables in the Global Declarations - Variables section of the outline. Select the Variables outline item and select Sql Handle from the Coding Assistant. A Sql Handle declaration will be inserted in the outline. Type the name of the variable, g_hSqlSelect. Follow the same process to create the global Boolean variable g_bConnected.

Step 3:

Create a new standard Form Window. Give your new form the title 'Presidents' and name the form frmMain.

Step 4:

Use the standard data field and background text objects from the Controls Palette. Create data fields (and their associated background text) to contain the presidents data: Name Death Age Birth Date Party Years Served State Born

Gupta Technologies, LLC

Page 21

Appendix B: Solutions

Step 5:

Use the standard push button from the Controls Palette to create two buttons on the form; one labeled &Next and one labeled &Previous. Disable each of them at start-up by capturing the SAM_Create message and using the function SalDisableWindow(hWndItem). We will enable them later in our code. Each button should have the associated code illustrated below. This is a illustration of what the left pane of your application might look like. You may have them in a different order, but the objects should.

Step 6:

Create a Window Variable called nInd in frmMain.

Step 7:

Add an On SAM_AppStartup action to the Application Actions section. In here you will populate SqlDatabase, SqlUser and SqlPassword. Make a call to the function SqlConnect(), passing as a parameter the global SQL Handle, g_hSqlSelect. This function returns TRUE or FALSE indicating success or failure. This information will help us set the Boolean Variable g_bConnected. If the connect was successful, the variable will be set to TRUE, otherwise it will be set to FALSE. If we fail to get a connection to the database, the application will call the function SalQuit(). The code should look like the example shown below.

Page 22

Building Applications with SQLWindows

Appendix B: Solutions

Step 8:

On closing the application, the connection to the database should dropped. This is done with an On SAM_AppExit message action in the Application Action section. If we successfully made a connection at the start of the program, the value of g_bConnected will be TRUE. If it is TRUE, we need to disconnect the Sql Handle, g_hSqlSelect. Do this by calling SqlDisconnect(), passing it the Sql Handle. The code to do this is illustrated above.

Step 9:

Add two menus to the Form. The first will be a Pop-up menu for the File | Exit option. In the left pane, click on Window Menu within frmMain. The illustration below shows the code.

Step 10:

Create a top-level menu item. This menu item executes a function immediately upon being clicked, rather than displaying a pop-up menu. The logic is explained below and the code is illustrated on the next page.

Step 11:

In the Menu Actions section create the code to: Prepare the SQL Statement, with SqlPrepare(). Select the desired columns from the president table and places them into the appropriate data fields. Use our global Sql Handle, g_hSqlSelect. Execute the SQL Statement with SqlExecute() Fetch the first row of data with SqlFetchNext() Enable the Next push button, pbNext.

Step 12:

If any of the Sql* functions fail, quit the menu action by returning FALSE immediately. Code is illustrated below:

Gupta Technologies, LLC

Page 23

Appendix B: Solutions

Step 13:

For push button pbNext code for an On SAM_Click event. When the button is clicked, we want to fetch the next row of data. To do this, make a call to SqlFetchNext(). In the call, reference g_hSqlSelect and nInd. The global variable, g_hSqlSelect, points to the data associated with the select that was performed when you picked the Select_All menu Item.

Step 14:

Test the result of the Fetch (stored in nInd). If the end of the data set was reached, nInd will contain FETCH_EOF. In this case, inform the user with a message box and disable the pbNext push button.

Step 15:

Remember to enable the Previous push button when this point is reached, using SalEnableWindow().

Step 16:

Capture the Click event on the pbPrevious push button by coding an On SAM_Click action. The code will be similar to the Next button except we wish to fetch the previous row of data. Make a call to SqlFetchPrevious(). In the call, reference g_hSqlSelect and nInd.

Step 17:

Test the result of the Fetch (stored in nInd). If the end of the data set was reached, nInd will contain FETCH_EOF. Again, inform the user with a message box and disable the push button. Remember to enable the Next push button using SalEnableWindow().

Step 18: Step 19:

Choose File | Save and save your work as Chapter 11 Exercise 1.app. Press F7 and test your work.

Page 24

Building Applications with SQLWindows

Appendix B: Solutions

Exercise 12.1: Login Dialog Box


Step 1: Choose File | New from the main menu to create a new application (that will become your library file). Choose the File |Save As menu option to save the application. In the Save As dialog, name the file Chapter 12 Exercise 1.apl. Ensure that the 'List Files of Type' combo box in the lower left corner is set to 'Libraries (*.apl)'.

Step 2:

Create a new modal dialog box. In the Parameters tab, create a Receive Sql Handle. Name the variable hSqlParam. A receive variable is a variable that is passed into the dialog and, if changed in the dialog, has its changed value reflected in the calling routine.

Step 3:

Add a data field to the dialog box to prompt for the Database to connect to. Name the data field dfDatabase Set the attribute Max Data Length to 8 Set the attribute Format to Uppercase Create a background text object and set it to &Database (In the Outliner, ensure the background text appears immediately before the dfDatabase data field)

Step 4:

Add a data field to the dialog to prompt for the UserId to log in with. Name the data field dfUserId Set the attribute Max Data Length to 8 Set the attribute Format to Uppercase Create a background text object and set it to &User Name (In the Outliner, ensure the background text appears immediately before the dfUserId data field)

Gupta Technologies, LLC

Page 25

Appendix B: Solutions

Step 5:

Add a data field to the dialog to prompt for the Password. Name the data field dfPassword Set the attribute Max Data Length to 8 Set the attribute Format to Invisible Create a background text object and set it to &Password (In the Outliner, ensure the background text appears immediately before the dfPassword data field)

Step 6:

Initialize dfDatabase to 'PRESIDNT' and dfUserId to 'PRESIDNT', by capturing the SAM_Create message in each of these data fields. In the outliner, the code in the contents section of the dialog should look like the example shown below.

Step 7:

Add a push button to allow you to close the dialog box and quit the application, without logging in. Title the push button &Cancel Name the push button pbCancel Set the attribute Keyboard Accelerator to Esc

Step 8:

Add a SAM_Click message action to pbCancel which closes the dialog without setting hSqlParam, and returns FALSE to the calling routine - SalEndDialog(hWndForm,FALSE).

Step 9:

Add two Window Variables to the dialog box to track the process of logging in to the database. Add a Boolean called bConnect and a Sql Handle called hSqlLocal. These will be used as working variables in step 14 where we process the SAM_Click action on a Connect push button.

Step 10:

Add a push button to the dialog to allow connection to the database with the current selections. Title the push button &Connect Name the push button pbConnect Set the attribute, Keyboard Accelerator, to Enter.

Step 11:

Add a SAM_Click message action to pbConnect, which attempts to connect a Sql Handle to the database and, if successful, returns TRUE to the calling routine, indicating success.

Page 26

Building Applications with SQLWindows

Appendix B: Solutions

Step 12: Step 13:

Save the library file. If you have not already done so, open your solution from Chapter 11 Chapter 11 Exercise 1.app (or the template Chapter 12 Template 1.app). Save it as Chapter 12 Exercise 1.app.

Step 14:

At the application level, in the Libraries tab, add a new Include Library entry. Type the file name of the include file, Chapter 12 Exercise 1.apl. (notice that the login dialog box dlgLogin is now included in your code. It appears in a different color or font, and can not be edited directly)

Step 15:

Modify the SAM_AppStartup code to use your dialog to log in rather than connecting automatically. Pass the global Sql Handle g_hSqlSelect to the dialog in the SalModalDialog() function call. This will allow the dialog to connect our global Sql Handle to the database. Remove the code that sets the system variable SqlDatabase. Replace the call to SqlConnect with a call to create the login dialog box (SalModalDialog()). The dialog was created to return TRUE, if Connect is clicked (and the database connection is successful) and to return FALSE, if Cancel is selected. This return value becomes the return value for the SalModalDialog() function. Set g_bConnected equal to this return value, and test it as you did before. Quit the application if the return is FALSE. If the return is TRUE, create the main form window, frmMain, with a call to SalCreateWindow(). Make the owner hWndNULL since this will be the first form in our application.

Gupta Technologies, LLC

Page 27

Appendix B: Solutions

Step 16: Step 17:

Modify frmMain to not be automatically created by setting the attribute Automatically Create to No. Press F7 to save the application and test it.

Page 28

Building Applications with SQLWindows

Appendix B: Solutions

Exercise 12.2: Handling Known Connection SQL Errors


Step 1: Open the library from the previous exercise Chapter 12 Exercise 1.apl (or the template) and save it as Chapter 12 Exercise 2.apl. Open the application from the previous exercise Chapter 12 Exercise 1.app (or the template) and save it as Chapter 12 Exercise 2.app. Change the Include File: reference in the Libraries section of the outline to reference the new file Chapter 12 Exercise 2.apl. Save the application. Step 3: Open the library file Chapter 12 Exercise 2.apl again. This can be done by highlighting the line Dialog Box: dlgLogin and hitting <F5>. Step 4: Define global constants for the errors: SQL_Err_NoDatabase SQL_Err_InvalidPassword SQL_Err_InvalidUserName Step 2:

Step 5:

Define the following Dialog Box Window Variables. These will be used as temporary variables when processing errors locally. Sql Handle: Number: Number: hSqlError nSqlError nSqlErrorPosition

Step 6:

Modify the Click action for pbConnect to include Local Error Handler code (When SqlError). Ensure this code appears before the call to SqlConnect and at the same level of indentation.

Gupta Technologies, LLC

Page 29

Appendix B: Solutions

Step 7:

The code in the Error Handler should extract the error parameters, test them against the constants defined in step 3, and display appropriate messages. If the error is one of these three, return FALSE. If it is not, do nothing (so that the error falls through to be handled by global error processing).

Step 8:

Save the changes and return to your application Chapter 12 Exercise 2.app. Re-run your application to test your new Error Handler.

Step 9:

Go back to the library Chapter 12 Exercise 2.apl and remove the maximum character length constraint in the properties of the Database data field. Click the secondary mouse button on the data field and delete the Max Data Length attribute of 8.

Step 10:

Save the library and return to the application Chapter 12 Exercise 2.app. Ensure that global error processing occurs if a database name of 9 characters or greater is used.

Step 11:

Add your own global error processing to Chapter 12 Exercise 2.app. Capture the application message SAM_SqlError, and display a message to the user, 'An unknown database error occurred. Quit the application?'. Offer the user the opportunity to choose Yes or No. If Yes is selected, quit the application Otherwise let the program fall through to default error processing.

Step 12:

Save and test.

Page 30

Building Applications with SQLWindows

Appendix B: Solutions

Exercise 13.1: Getting Started with Table Windows


Step 1: From the SQLWindows menu, select File | New to create a new application. Again from the menu, select File | Save to save the new program. Use the name Chapter 13 Exercise 1.app. Step 2: Create a new standard Form Window. Give your new form the title 'Presidents' and name the form frmPresident. Step 3: Add a child table window to the form, to hold president information. Set the following attribute: Object Name: tblPresident Step 4: Add an exit push button to the form. Set the following attributes: Object Name: Object Title: Step 5: pbExit E&xit

To your Exit push button, code an On SAM_Click message action that makes a Call to SalQuit(). Once the design window is back in the Layout tab, right click on the "Exit" push button and choose Explore | Actions from the context menu. This will select the pbExit folder in the left pane and the Actions tab in the right pane. You can then code the message handler using the Coding Assistant and/or the outliner.

Step 6:

Add a column for the president name to your tblPresident child table window. Set the following attributes: Object Name: Object Title: Data Type: colPresName President String

Step 7:

Add a column for the state where the president was born to your tblPresident child table window. Set the following attributes: Object Name: Object Title: Data Type: colStateBorn State Born String

Step 8:

Add a column for the president's birth date to your tblPresident child table window. Set the following attributes: Object Name: Object Title: Data Type: colBirthDate Birth Date Date/Time

Step 9:

Add a column for the president's party to your tblPresident child table window. Set the following attributes: Object Name: Object Title: Data Type: colParty Party String

Step 10:

Add a column for the president's spouse to your tblPresident child table window. Set the following attributes: Object Name: Object Title: Data Type: colSpouse Spouse String

Gupta Technologies, LLC

Page 31

Appendix B: Solutions

Step 11:

Add a column for the spouse's age to your tblPresident child table window. Set the following attributes: Object Name: Object Title: Data Type: colSpouseAge Spouse's Age Number

Step 12:

Add a column for the number of children to your tblPresident child table window. Set the following attributes: Object Name: Object Title: Data Type: colNumChildren # Children Number

Step 13:

Add the Chapter 12 Exercise 2.apl library file to the application. To do this, select the application folder in the left pane and select the Libraries tab in the right pane. Right click in the right pane and choose Add Next Level | File Include from the context menu. An Open dialog box will appear where you can select the library file. Once you click on Open button, it will be included in the application.

Step 14:

Select the frmPresident folder in the left pane and the Variables tab in the right pane. Add a Sql Handle variable called hSqlPres and add a Boolean variable called bConnected.

Step 15:

Select the Actions tab in the right pane and add both On SAM_Create and On SAM_Destroy message handlers. The following code fragment shows how to connect the Sql Handle using the Login dialog box on the creation of the form window and how to disconnect the Sql Handle on the closing of the form window.

Step 16:

In the Functions section of the Child Table Window, add a Populate() function which, when called, will use SalTblPopulate() to fill the Table Window with columns from the president and spouse tables, joined by pres_name.

Page 32

Building Applications with SQLWindows

Appendix B: Solutions

Step 17:

Call your Populate function from an On SAM_CreateComplete action on the Child Table Window. Add the message handler code as shown:

Step 18:

Save your work and test by pressing F7.

Gupta Technologies, LLC

Page 33

Appendix B: Solutions

Exercise 13.2: Selecting Data with a Combo Box


Step 1: Open up either your Chapter 13 Exercise 1.app or open the template Chapter 13 Template 2.app. Save it as Chapter 13 Exercise 2.app. Step 2: Add a Combo Box for the state column. Set the following properties: Object Name: Editable: Sorted: Step 3: CmbStates No Yes

Set the list initialization for cmbStates to three or four states.

Step 4: Step 5:

Add a background text item containing the word States in front of your combo box. Previously, our table populated itself on SAM_CreateComplete. We want to change our approach so that the table will be populated when we press a push button. To begin, we need to define our own PAM_Populate user message, using SAM_User, in the Constants tab of the application folder as shown:

Step 6: Step 7:

In tblPresident, change the On SAM_CreateComplete action to On PAM_Populate. In the Child Table Window's Populate function, include a reference to the combo box in the where clause of the SalTblPopulate() function. Our goal is to only fill the table with presidents born in the state selected in cmbStates.

Step 8:

Add a Query push button. Set the following attributes: Object Name: Object Title: pbQuery &Query

Step 9:

Include a SAM_Click action in pbQuery to post a PAM_Populate message to the form window.

Page 34

Building Applications with SQLWindows

Appendix B: Solutions

Step 10:

Finally, add an On PAM_Populate message handler to the form window, which sends a PAM_Populate message to the form's children.

Step 11:

Compile and test your application.

Gupta Technologies, LLC

Page 35

Appendix B: Solutions

Exercise 13.3: Populating a Combo Box


Step 1: Open up either your Chapter 13 Exercise 2.app or the template Chapter 13 Template 3.app. Save it as Chapter 13 Exercise 3.app . Step 2: Remove the list initialization items from the Combo Box. From the Outline tab of cmbStates, select all the text items and delete them. Step 3: Finally, add an On SAM_DropDown Message Action to populate the Combo Box if the list count is presently 0. We will use SalListPopulate() to do so, selecting our states from the state table with the form window's handle.

Step 4:

Save your work and test by pressing F7.

Page 36

Building Applications with SQLWindows

Appendix B: Solutions

Exercise 14.1: Manipulating Data with Table Windows


Step 1: Step 2: Open the template Chapter 14 Template 1.app. Our states table needs a populate function. It can rely on the Sql Handle hSqlStates, which is connected and disconnected by the states dialog box. We want our populate function to select the state and rowid of each state from the states table, and bind them into the state and rowid columns in our child table window.

Step 3:

Your states table needs a function to add a new row. Ideally, your function should insert the row, place the focus on that row in the state column and scroll the row into view.

Step 4:

Add a delete function to toggle the ROW_MarkDeleted flag on the current row. In the example below, the current state of the ROW_MarkDeleted flag is tested and then reversed:

Step 5:

Add an ApplyChanges() function to the states table to perform DML from the table window against the states database table. Commit the transactions, and then re-populate the table window. You may want to turn the hourglass cursor on and then off before and after the following sample code:

Gupta Technologies, LLC

Page 37

Appendix B: Solutions

Step 6:

Complete the message actions for the table window so that each calls the appropriate function, as follows:

Step 7:

Save your work and test by pressing F7.

Page 38

Building Applications with SQLWindows

Appendix B: Solutions

Exercise 14.2: Appling Updates with Table Windows


Step 1: Step 2: Open the template Chapter 14 Template 2.app. Inside dlgMaintainPresidents, in the functions section of tblPresidents, look for the empty ApplyChanges() function. Add a local number variable called nCurrentRow. Step 3: You may want to code the logical 'shell' of the function first. We want our function to process deletes (rows marked deleted, but not also new), then updates (rows marked edited, but not also deleted or new), and then inserts (rows marked new, but not also deleted). As before, your function should then commit and re-populate the table.

Step 4:

What remains is to code each of the three If blocks. In each case, you should prepare a suitable SQL statement and then, using SalTblFindNextRow(), skip through each of the rows with the appropriate flags, executing the statement. Here are examples for each of the three code blocks:

Gupta Technologies, LLC

Page 39

Appendix B: Solutions

Step 5:

Save your work and test by pressing F7.

Page 40

Building Applications with SQLWindows

Appendix B: Solutions

Exercise 15.1: Advanced Table Windows


Step 1: Step 2: Open the template Chapter 15 Template 1.app. Inside dlgMaintainPresidents, in the table tblPresidents change the cell type attribute of your column colStateBorn to Drop Down. Much like our combo box in Chapter 12, we want colStateBorn to populate itself on SAM_DropDown, if its list query count is 0. We can use the dialog's Sql Handle hSqlPres to perform the requisite select statement. Here is one example:

Step 3:

Step 4:

In the SAM_CreateComplete action for the tblPresidents, set the TBL_Flag_SelectableCols table flag on.

Step 5:

In the functions section of the table, write a function to sort the table. Your function should accept a single window handle argument and sort the table by that column.

Step 6:

Add a SAM_ColumnSelectClick action to tblMaintainPresidents that calls your sort function, passing it the result after converting wParam to a window handle.

Gupta Technologies, LLC

Page 41

Appendix B: Solutions

Step 7:

In the lower left corner of dlgMaintainPresidents, add a radio button. Set its properties as follows: Object Name: Object Title: rb1700 1700

Step 8: Step 9: Step 10:

Add two more radio buttons beside rb1700, named rb1800 and rb1900, and titled 1800 and 1900 respectively. Surround your radio buttons with a group box entitled Show Presidents Born After In the functions section of tblPresidents, write a function to hide the rows containing presidents born before the specified century. Your function should accept a single numeric argument (the year), and then scan through each president row, hiding the older ones and showing the ones born after the specified year. (Hint: Use SalTblQueryScroll() to get the number of rows in the table first)

Step 11: Step 12:

Finally, call your hide function from a SAM_Click action in each radio button. Save and test your code by pressing F7.

Optional Solutions Step 13: Return to the ColumnSort() function you created in step 4. Add the following code after the SalTblSortRows() function:

Page 42

Building Applications with SQLWindows

Appendix B: Solutions

Exercise 15.2: Using Aggregate Functions


Step 1: Step 2: Open the template Chapter 15 Template 2.app. In the SAM_CreateComplete action for the child table, define the table as a split table window with 1 row in the lower pane. Make it a non-adjustable split.

Call SalTblDefineSplitWindow(hWndForm, 1, FALSE)


Insert the following function call between the SalTblSetTableFlags() and SalPostMsg() function calls: Step 3: Insert the following code into the Populate function between the SalTblPopulate() and SalWaitCursor(FALSE) function calls:

Optional Solutions Step 5: In the Constants tab of the application folder, add the following user constant definition:

Step 6:

In the Actions tab of the table window, add the following message handler: The nSummaryRow variable should be defined as a variable of the child table window, not the Populate() function, so that we can use its value in the SAM_SetFocus message handler. And in the Actions tab of the table window, add the following message handler for the PAM_SetFocusToRow message

Gupta Technologies, LLC

Page 43

Appendix B: Solutions

Page 44

Building Applications with SQLWindows

Appendix B: Solutions

Exercise 16.1: SQLWindows and Reports


Step 1: Click on File | New, or select the blank page icon on the toolbar. Click on File | Save, or select the diskette icon on the toolbar, and name your application Chapter 16 Exercise 1.app. Step 2: Step 3: You may bypass these steps, as the report template has already been built for you. In the left pane, right-click on the Windows tree item. From the pop-up menu, select NEW | Form Window. The window will surface in layout mode in the right pane. Double click on it and set the Object Name attribute to frmSimpleReport. Set the Object Title attribute to Simple Classic Report. If the Control Palette is not presently visible, type Alt + 4. Using the push button tool, drop and size a push button on the form. Double click on it and set the Object Name attribute to pbRunReport. Set the Object Title attribute to Run Report. Step 4: As we are done with the visual part of the application, we want to switch to outline view. In the right pane, click on the Outline tab. Review and duplicate the code snippet below:

Step 5:

Review and duplicate the code snippet below:

Step 6:

Review and duplicate the code snippet below:

Step 7:

Review and duplicate the code snippet below:

Gupta Technologies, LLC

Page 45

Appendix B: Solutions

Step 8:

Review and duplicate the code snippet below:

Step 9:

Click on File | Save, or select the diskette icon on the toolbar. Name your application Chapter 16 Exercise 1.app.

Page 46

Building Applications with SQLWindows

Appendix B: Solutions

Exercise 16.2: Creating an RB Automation App


Step 2: Click on File | New, or select the blank page icon on the toolbar. Click on File | Save, or select the diskette icon on the toolbar, and name your application Chapter 16 Exercise 2.app. Step 3: Launch Report Builder Create a query using the following tables and columns: Parties: President: Step 4: Step 5: Step 6: Party Pres_name, Birth_Date, Yrs_Serv, Death_Age

Set a break group on PARTY Define the report layout. Click on File | Save, or select the diskette icon on the toolbar. Name your application Chapter 16 Exercise 1.app.

Step 7: Step 9:

In your application, define a function ( OpenReport) as a global internal function. Save and test your application.

Gupta Technologies, LLC

Page 47

Appendix B: Solutions

Exercise 18.1: Create a Simple COM Server


Step 1: Create a new application with SQLWindows. Save it as Chapter 18 Exercise 1.app. Step 2: Step 3: Invoke the COM Class Wizard via Components | Wizards Select a CoClass component. Click on Next >. Step 4: Give it a name of AddNumbers and derive it from a New interface. Click on Next >.

Step 5: Step 6:

Add a method with a name of Add. Add arguments Number1 and Number2 both of data type Number(VT_R4).

Step 7:

Set the Return Data Type to Number(VT_R4) .

Page 48

Building Applications with SQLWindows

Appendix B: Solutions

Step 8: Step 9:

Click on Next > and then click on Finish to generate the SAL Code. In the Interface for AddNumbers ( IAddNumbers) click on the function Add and in the outline add the code:

Return Number1 + Number2


Step 10: Examine the parameters Number1 and Number2 and also the Return value. The variant type used for these numbers should be VT_R4. If not change the definition. Step 11: Compile this code and then save.

Gupta Technologies, LLC

Page 49

Appendix B: Solutions

Exercise 18.2: Building an In-Process COM Server


Step 1: Step 2: Open the previous application in SQLWindows Use the Project | Build Settings dialog to set the Target Type as In-process server (DLL). Give the COM Server a New GUID.

Step 3: Step 4:

Use Project | Build Server to build Chapter 18 Exercise 1.dll. Use Project | Register Server to register your server in the registry.

Page 50

Building Applications with SQLWindows

Appendix B: Solutions

Exercise 18.3: Testing Your COM Server


Step 1: Open a new application in SQLWindows. Save it as Chapter 18 Exercise 3.app. Step 2: Create a new Form Window with three numeric data fields (dfNumber1, dfNumber2, and dfResult), the last of which is noneditable. Add a Pushbutton with a title of Add. Use the ActiveX Explorer to generate the APL code for Chapter 18 Exercise 1 1.0 Type Library. Verify that this includes the library into the application. Step 5: In the Windows Variables section of the form, use the Coding Assistant to define an instance AddNum of the Chapter_18_Exercise_1_AddNumbers Proxy Class. In the Message Actions section of the Form Window add the following code:

Step 3: Step 4:

Step 6:

Step 7:

In the Message Actions section for the Add Pushbutton, code the On SAM_Click action. This should call the Add function of the instance of the COM Proxy Class that you defined:

Step 8:

Compile and test your program. Test your program with values other than integers to verify that the correct data types are being used as parameters. The default data type is I2, which will cause 1.01 + 1.49 to return a result of 2 rather than 2.5.

Gupta Technologies, LLC

Page 51

Appendix B: Solutions

Exercise 20. 1: Export Data in a Child Table


Step 2: Open up the template Chapter 20 Template 1.app. Save it as Chapter 20 Exercise 1.app. Step 3:

Step 4:

Step 5:

Add a pushbutton and label it XML Create

Step 6:

Compile and test your program.

Page 52

Building Applications with SQLWindows

Appendix B: Solutions

Exercise 20.2: Serialize a UDV


Step 2: Open up the template Chapter 20 Template 2.app. Save it as Chapter 20 Exercise 2.app. Step 3: In the Classes section, Add a Functional Class (FC) data type and give it a name. Then Add a String data type in the Instance Variables Section of the new FC and give it a name.

Step 4:

In the Variables section of the dialog box dlgMaintainStates, Add a variable of the newly created (FC) data type. Make it an array by added a left [, a value (* - for dynamic) and a right ].

Step 5:

In the Actions section of the Child Table tblMaintainStates, Add the an assignment statement to populate you array.

Step 6:

In the Actions section of the dialog box dlgMaintainStates, Add the SalSerializeUDV function call.

Gupta Technologies, LLC

Page 53

Appendix B: Solutions

Step 7:

Compile and test your program.

Page 54

Building Applications with SQLWindows

Appendix B: Solutions

Exercise 20.3: Create an XML Document Using SQL


Step 2: Open up the template Chapter 20 Template 3.app. Save it as Chapter 20 Exercise 3.app. Step 3:

Step 4:

Step 5:

Compile and test your program.

Gupta Technologies, LLC

Page 55

Appendix B: Solutions

Exercise 20.4: Communicate with a Web Service


Step 2: Open up the template Chapter 20 Template 3.app. Save it as Chapter 20 Exercise 3.app. Step 3:

Step 4:

Compile and test your program.

Page 56

Building Applications with SQLWindows

Das könnte Ihnen auch gefallen