Put a Button on a Form that Sets Form Field Values

The goal in this lesson is to put a button on a form that sets the "Active" field to False, and sets the date on which the customer was deactivated.

  1. Right-click on the "customers" table and select the Open Default Form.

  2. Click the Design Mode button to switch to Design Mode.

  3. Click the Button Object tool on the Toolbox.

  4. Click and drag to draw a button on the form next to the Active field. When you release the mouse button, the Define Button dialog box appears.

  5. Enter "Deactivate" as the Label.

  6. Make sure the Define script for this button now? check box is checked.

  7. Make sure the Use Action Scripting radio button is selected.

  8. Click Next >. The embedded Action Script Editor appears. Picture

  9. Click the Add New Action button.

  10. Select the "Fields" category and the "Set Field Values" Action, then click OK.

    • The Set Field Values Genie appears.

  11. Click the green '+' icon button to select the field you want to update. Alpha Anywhere shows the fields on the current form.

    Alpha Anywhere is actually showing the objects on the form that are bound to fields. By default, Alpha Anywhere gives the object the same name as the field itself, but you are free to change any Object name by right clicking on the object and selecting the Properties command.
  12. Scroll down the list until you reach "Active". Note that since "Active" is a reserved word in Alpha Anywhere, the control name has been automatically renamed to "Active0."

  13. Select "Active0" and click OK.

  14. Click the '+' button again to add another field.

  15. Select the "date_deactivated" field and click OK. Your screen should now look like this:

  16. Set "date_deactivated" to today's date.

  17. Set the How will you specify the field's value? combo box to "Specify an expression that evaluates to the value".

  18. Enter "date()" as the expression. The DATE() function returns the current date.

  19. Select the "Active0" object.

  20. Choose the Specify the value now option.

  21. Select "False" as the value.

  22. Click the Finish button.

    When you create an Action Script in the embedded Action Script Editor, Alpha Anywhere does not prompt for a comment in the last step, as it does when you create the Action Script in the Code Editor. (The Action Script Editor is said to be "embedded" because it is embedded in the Button Genie.) The Action Script looks like this.
  23. Click Finish. The Button Genie closes and your form now has a button.

  24. Save the form, naming it "Deactivate".

  25. Click the Form View button to run the form.

  26. Click the new Deactivate button to test it out.

  27.  Enhancing the Script

    It would be nice if, after the button was pressed, a dialog box came up asking the user to confirm that they wanted to deactivate the customer. Click the Design button to put the form back into Design Mode.

  28. Right-click the Deactivate button and select Events > OnPush to edit the button's OnPush event. The Code Editor opens. Note the name on the tab. ":Deactivate" is the form name, "Button1" is the object name, and "OnPush" is the event name.

  29. Click the Add New Action button.

  30. Select the "Dialog Boxes" category and the "Display a Message Box" Action. Click OK.

  31. Select the Yes, No, Cancel option.

  32. Select Second Button as the button with Initial Focus.

  33. Click Next >.

  34. Fill in the Genie options as shown below:

  35. Click Next >.

  36. Enter "deactivate" as the dialog box name.

  37. Click Next > and Finish to close the Genie. Your Action Script should look like this:

  38. You want to display the message box BEFORE you set the values in the fields. Move this action to the top of the list. Click the Move Action Up button

  39. Select the "Set Field Values Action" to give it focus. We need to make this action conditional.

  40. Click the Show/hide Conditions button.

  41. Check the Action is Conditional check box. Accept the default "Flag variable is True" choice. "Flag" variables are special variables set by the message box in the previous step. For example, if the user clicks the "No" button in this dialog, then the deactivate_No_button variable will be set to .T.. In this example, you only want to update the field values if the user clicks the "Yes" button on the message box. So the flag variable we care about is the deactivate_Yes_button.

  42. Select "deactivate_YES_Button" in the Flag combo box. The Code Editor should now look like this:

  43. If the user selects the "No" or "Cancel" buttons, create a message box saying that the customer was not deactivated. To do this, follow the steps to add a new message box action.

  44. Select the OK button on the first page of the Display a Message Box Genie.

  45. Complete the second page of the Genie as shown below:

  46. Enter "notdeactivated" as the name of the dialog box.

  47. Check the Action is Conditional check box.

  48. Set Execute action if to "Flag variable is True".

  49. Set Flag to deactivate_NO_button. Your screen should look like this:

  50. Save your form and test the button.

  51. Click No on the message box, and you will see the "Customer was not deactivated" message.

  52. If you select the Cancel button, the field value will not be updated (as you would expect), but the "Customer was not deactivated" message will NOT be shown.

  53. To fix this, edit the button Action and change the Execute Action if setting to "Condition expression is True". Then enter the expression, "Var->deactivate_NO_Button = .t.".