Suppose you want to allow the operator to enter the date for the total, or you want to provide for a weekly or monthly total by allowing the operator to enter a beginning and ending date. You can change the ChargesByDateRange.rmd file to allow this.
1. | At the R> Prompt, enter RBEDIT ChargesByDateRange.rmd |
2. | Edit the first part of the command file to match the syntax below. |
The text in the blue font are new commands to add to the file
The text in the green font are requires changes to the existing command
The text in the maroon font we will keep unchanged
You'll need to move the cursor to the first line and press [Enter] several times to open sufficient space at the top of the command file for the new lines. Use the space bar, not [Tab] to indent lines. You can even copy and paste the below command syntax into your file.
SET VARIABLE vDay1 TEXT = NULL
SET VARIABLE vDay2 TEXT = NULL
DIALOG 'Enter Beginning Date: ' vDay1 vKey1 1 +
CAPTION 'Question' ICON QUESTION
IF vDay1 IS NULL THEN
SET VARIABLE vDay1 DATE = .#DATE
ENDIF
DIALOG 'Enter Ending Date: ' vDay2 vKey2 1 +
CAPTION 'Question' ICON QUESTION
IF vDay2 IS NULL THEN
SET VARIABLE vDay2 DATE = .#DATE
ENDIF
SET VARIABLE vDay1 DATE
SET VARIABLE vDay2 DATE
SELECT SUM(TotalCharge) INTO vSum FROM Flights +
WHERE FlightDate >= .vDay1 AND FlightDate <= .vDay2
SET VAR vMessage TEXT = +
('The total from' & (CTXT(.vDay1)) & 'to' & +
(CTXT(.vDay2)) & 'is:' & (CTXT(.vSum)))
PAUSE 2 USING .vMessage CAPTION 'Sum' ICON INFO
RETURN
Some of the commands shown above are indented by a few spaces. This is not necessary but makes the command file easier to read. Blank lines may be used for the same purpose. Notice that the indented commands allow you to easily see where programming structures such as the commands inside an IF...ENDIF structure begin and end. Also, R:BASE allows you to use upper or lower case or a mixture as shown in the listing. Upper case is used in the manuals to show keywords such as commands or command clauses, and lower case is used to show variable information such as column or variable names.
The first two SET VARIABLE commands set the data types of two variables to the TEXT data type so they are accepted by the DIALOG command, which only uses TEXT variables.
The DIALOG commands on lines 4 and 10 accept the beginning and ending dates and place them in variables vDay1 and vDay2. The command is too long to enter on a single line, and so it is continued by entering a space and then a plus sign as the last character of the first line. As is the case when you enter a command at the R> Prompt, when R:BASE sees a plus sign at the end of a line, whether it is a command or data, it expects the next line to continue the previous one.
As a date value is entered, the IF structure checks to see if a value was actually entered. If no date is entered, the variable has a null value, so the command file sets the variable to a DATE data type and to the current date (.#DATE). It does this by checking the date variables for IS NULL. IS NULL is the keyword used to check for null entries.
After the DIALOG commands, the two SET VARIABLE commands set the data types of the two variables to the DATE data type in the event that the operator entered valid date values (i.e 08/12/2022). Since the resulting value of the DIALOG variables are always of the TEXT data type, the resulting variables must be converted to the correct data type if they are to be compared against a data type other than TEXT. The variables vDay1 and vDay2 must be forced to DATE data types so the variable values can be correctly compared against the FlightDate column values.
The SELECT command now includes a WHERE clause that limits the computation to the rows with a flight date greater than or equal to the beginning date and less than or equal to the ending date.
The SET VAR command which built the vMessage variable has now been enhanced to include the vDay1 and vDay2 variables with the value of vSum, which is, of course, the computed total for the transactions.
The PAUSE 2 command displays the value of the dates and the computed total for the transactions.
When the command file is executed, the vDay1 and vDay2 date values change according to what the operator enters. The sum changes depending on the dates entered.
3. | Now save the file. |
4. | Choose the "R> Prompt" button to move the focus to the "R> Prompt" window. |
5. | At the R> Prompt, enter RUN ChargesByDateRange.rmd |
The command file should execute prompting you for dates.
6. | Enter the beginning date "04/15/2022", and click the "OK" button. |
7. | Enter the ending date "07/01/2022", and click the "OK" button. |
The message should display that the total from 04/15/2018 to 07/01/2022 is $12,450.00.
This process may seem a little complicated. All that is happening is that you are using the concatenation operator & to combine the values into a single variable and then display that single variable with a PAUSE command.
The data types of vDay1, vDay2, and vSum variables change to TEXT so they can be concatenated with other text. The SET VARIABLE command combines all the text values. The & operator is used to insert a space between each text value.
Notice that when you want R:BASE to use the value of a variable, you put a dot in front of the variable name. For example, in the SET VARIABLE command that performs the concatenation, the variables vDay1 and vDay2 have dots because you want to use the value of these variables, not just the variable names. The basic rule is that if the variable name is to the right of a comparison operator such as an equal sign, then the variable needs to be dotted. If the variable is to the left of a comparison operator, it should not be dotted.
Your next enhancement is to add comments to the file. A command file should always be documented in case another user needs to read what it does.
8. | Enter the following command at Line 1, and move the SET VAR commands down |
-- Calculates flight totals for a given flight date range
Notice that R:BASE recognizes the hyphens "--" at the beginning of the line and adds a pink italicized highlighting. This means that the text will not be read by R:BASE when you run the file. You could also place your name and date in the file.
Your final enhancement is make your command file clean up after itself. You do not want to collect unneeded variables, because each variable uses space in your computer's memory that might better be used by R:BASE for its own processing needs.
8. | Enter the following command after the PAUSE command and before the RETURN command: |
CLEAR VAR vDay1, vDay2, vSum, vMessage, vKey1, vKey2
This command deletes the variables you have used in this command file. You do not want to delete all the defined variables by using the CLEAR ALL VARIABLES form of this command, because there may be some variables needed in another procedure file, that defines some variables it needs for its own operation.
The entire command file now looks like this:
-- Calculates flight totals for a given flight date range
-- Written by James Howlett 02/19/2022
SET VARIABLE vDay1 TEXT = NULL
SET VARIABLE vDay2 TEXT = NULL
DIALOG 'Enter Beginning Date: ' vDay1 vKey1 1 +
CAPTION 'Question' ICON QUESTION
IF vDay1 IS NULL THEN
SET VARIABLE vDay1 DATE = .#DATE
ENDIF
DIALOG 'Enter Ending Date: ' vDay2 vKey2 1 +
CAPTION 'Question' ICON QUESTION
IF vDay2 IS NULL THEN
SET VARIABLE vDay2 DATE = .#DATE
ENDIF
SET VARIABLE vDay1 DATE
SET VARIABLE vDay2 DATE
SELECT SUM(TotalCharge) INTO vSum FROM Flights +
WHERE FlightDate >= .vDay1 AND FlightDate <= .vDay2
SET VAR vMessage TEXT = +
('The total from' & (CTXT(.vDay1)) & 'to' & +
(CTXT(.vDay2)) & 'is:' & (CTXT(.vSum)))
PAUSE 2 USING .vMessage CAPTION 'Sum' ICON INFO
CLEAR VAR vDay1, vDay2, vSum, vMessage, vKey1, vKey2
RETURN
If your own command file does not look like this, use RBEDIT to edit it. When you finish, leave RBEDIT so that you can test the command file.
9. | Save your file and leave the R:BASE Editor. |
10. | To execute the program, at the R> Prompt, enter RUN ChargesByDateRange.rmd |
The dates contained in the flights table range from 03/03/2022 to 09/07/2022, so enter any combination of these dates for beginning and end dates as long as the first date is earlier than the second date.
In the next lesson, you will learn how to create and modify an application by using the Application Designer.
If you want, you can exit from R:BASE at this time.
11. | At the R> Prompt, enter EXIT |