Making Decisions
Once you have the data and can access the fields of the data, you can then use this data to make decisions.
To make decisions, you can use the IF statement in DbfScript.
Looking back at our previous example, let's say we have a record stored in #rec that contains product information. You could then output a message if the product is expensive:
if #rec/Price > 1000 then message "The product " + #rec/Title + " is quite expensive!" end if
Here we're checking if the price is greater than 1000, and if it is we output a message to the user telling them. In the message statement we're also getting the name of the product from the #rec variable, in a string expression.
You can perform any operations you wish inside the IF statement.
Sometimes you may also want to have an ELSE section that only gets executed if the condition for the IF statement is not true:
if #rec/Price > 1000 then message "The product " + #rec/Title + " is quite expensive!" else message "The price for " + #rec/Title + " is " + #rec/Price end if
Here we have an ELSE section that simply outputs the price of the product.
Sometimes you want to check if a variable is set at all. If a load statement doesn't find a record that matches, then the variable will be set to Nothing(), or will be empty. We can use the Exists function to check for this:
if Exists(#rec) then message "We found a product!" end if
Likewise we may want to display an error if there are no records found:
if NotExists(#rec) then error "No product was found!" end if
Here we are using NotExists, which returns true if the record is empty. We're also using the "error" command. This aborts the script and displays the given error to the user.
If we prefer we could simply WARN the user. This shows a message but lets them click an OK button to accept the warning and continue anyway:
if NotExists(#rec) then warning "No product was found - continue anyway?" end if