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.
Right-click on the "customers" table and select the Open Default Form.
Click the Design Mode button to switch to Design Mode.
Click the Button Object tool on the Toolbox.
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.
Enter "Deactivate" as the Label.
Make sure the Define script for this button now? check box is checked.
Make sure the Use Action Scripting radio button is selected.
Click Next >. The embedded Action Script Editor appears. Picture
Click the Add New Action button.
Select the "Fields" category and the "Set Field Values" Action, then click OK.
The Set Field Values Genie appears.
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.
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."
Select "Active0" and click OK.
Click the '+' button again to add another field.
Select the "date_deactivated" field and click OK. Your screen should now look like this:
Set "date_deactivated" to today's date.
Set the How will you specify the field's value? combo box to "Specify an expression that evaluates to the value".
Enter "date()" as the expression. The DATE() function returns the current date.
Select the "Active0" object.
Choose the Specify the value now option.
Select "False" as the value.
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.
Click Finish. The Button Genie closes and your form now has a button.
Save the form, naming it "Deactivate".
Click the Form View button to run the form.
Click the new Deactivate button to test it out.
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.
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.
Click the Add New Action button.
Select the "Dialog Boxes" category and the "Display a Message Box" Action. Click OK.
Select the Yes, No, Cancel option.
Select Second Button as the button with Initial Focus.
Click Next >.
Fill in the Genie options as shown below:
Click Next >.
Enter "deactivate" as the dialog box name.
Click Next > and Finish to close the Genie. Your Action Script should look like this:
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
Select the "Set Field Values Action" to give it focus. We need to make this action conditional.
Click the Show/hide Conditions button.
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.
Select "deactivate_YES_Button" in the Flag combo box. The Code Editor should now look like this:
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.
Select the OK button on the first page of the Display a Message Box Genie.
Complete the second page of the Genie as shown below:
Enter "notdeactivated" as the name of the dialog box.
Check the Action is Conditional check box.
Set Execute action if to "Flag variable is True".
Set Flag to deactivate_NO_button. Your screen should look like this:
Save your form and test the button.
Click No on the message box, and you will see the "Customer was not deactivated" message.
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.
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.".