Creating Buttons that Run Code

Description

How to create a button with an action script and attach a script to a button. This page also contains an explanation of the code involved in doing this.

Button1 , Button2 and Button3 demonstrate how to move an Alpha Anywhere function onto a button on a form by attaching an Action Script or a developer created script:

 Procedure: Creating a Button with an Action Script

The following procedure assumes that the developer has opened the form in the Form Editor. Follow these instructions to create a button similar to Button3 :

  1. Select the Button tool on the toolbar and sketch the new button.

  2. Enter the label of the button in the Label type-in box.

  3. In the resulting Define Button dialog box accept the default values for Define script for this button now? and Use Action Scripting.

  4. Click the Next > button.

  5. In the resulting Select Action dialog box select "Query" from the Category list and "Query by Form" from the Action list.

  6. Click the OK button.

  7. Click the Finish button in the Script Genie.

  8. Click the Finish button in the Define Button dialog box.

  9. The Action Script is automatically attached to the button's OnPush event.

 Procedure: Creating and Attaching a Script to a Button

The following procedure assumes that the developer has opened the form in the Form Editor. Follow these instructions to create a button similar to Button1 :

  1. Select the Button tool on the toolbar and sketch the new button.

  2. Enter the label of the button in the Label type-in box.

  3. In the resulting Define Button dialog box accept the default value for Define script for this button now?

  4. Click the Launch Script Editor button.

  5. Enter the following Xbasic code.

    current_filter = current_filter_expn()
    current_order = current_order_expn()
    query.filter = current_filter
    query.order = current_order
    if (vcLayoutType = "reports") then
        report.preview(vcReports, query.filter, query.order)
    else if (vcLayoutType = "letters") then
        letter.preview(vcLetters, query.filter, query.order)
    else if (vcLayoutType = "labels") then
        label.preview(vclabels, query.filter, query.order)
    end if
  6. Click the Save button and the Close (X) button.

  7. The script is automatically attached to the button's OnPush event.

You can further edit the script after opening it up from the Code tab of the Control Panel.

 An Explanation of the Code

The Preview button on CustQueryReport form calls this Xbasic code. The purpose of this routine is to preview the appropriate type of layout, using the primary table's current filter and order expressions. The first step is to return the filter expression of the primary table. The variable current_filter is implicitly dimmed as C (character).

current_filter = current_filter_expn()

Next the routine gets the order expression of the primary table. The variable current_order is implicitly dimmed as C (character).

current_order = current_order_expn()

This statement sets the value of the query.filter variable, the second argument of the preview() method.

query.filter = current_filter

This statement sets the value of the query.order variable, the third argument of the preview() method.

query.order = current_order

The value of vcLayoutType variable, as set by the DROPDOWN1 list box, defines the first argument of the preview() method.

if (vcLayoutType = "reports") then
    report.preview(vcReports, query.filter, query.order)
else if (vcLayoutType = "letters") then
    letter.preview(vcLetters, query.filter, query.order)
else if (vcLayoutType = "labels") then
    label.preview(vclabels, query.filter, query.order)
end if

A more compact version of the same Xbasic routine would be:

if (vcLayoutType = "reports") then
    report.preview(vcReports, current_filter_expn(), current_order_expn())
else if (vcLayoutType = "letters") then
    letter.preview(vcLetters, current_filter_expn(), current_order_expn())
else if (vcLayoutType = "labels") then
    label.preview(vclabels, current_filter_expn(), current_order_expn())
end if

See Also