Accessing Records in a View

Once a view has been sorted and filtered the way you want it, you can access each record in the view sequentially to perform various operations. The #DBFIRST command sets the current record to the first record in the view. The #DBNEXT command sets the current record to the next record in the view, and the #DBPREV command sets the current record to the previous record in the view. When you access past the first or last record, then %rec returns a null value. So, to loop through all the records in the current view and display the Name of each record, you would do:

#DBFIRST;#WHILE (!%null(%rec)) {#SHOW &Name;#DBNEXT}

This command sets the first record as the current, then loops until the %rec is null displaying the Name field and moving to the next record. Don't forget the #DBNEXT or you will get an infinite loop (which you can abort using the ESC key).

You can also loop through a view using the #LOOPVIEW command. This is sometimes easier than using the #WHILE command above. By default, #LOOPVIEW cycles through the records in the current view. To specify a different View|Database, use that as the first argument and put the commands to execute in the second argument. So, you can also display the names of all the items in the current view with

#LOOPVIEW {#SHOW &Name}

which is much simpler than the example using #DBFIRST and #DBNEXT although less powerful. If you wanted to display the names of the records in the Potions view, you would use

#LOOPVIEW Potions {#SHOW &Name}

You can also directly access records in a view using the %viewrec function. The first argument is the record number to retrieve. The first record in the view is record zero. The optional second argument is the View|Database to query. The optional third argument is set to non-zero to return formatted fields instead of raw data. The %numrec function will return the number of records in the current view. Its optional argument is the View|Database to query.



Contents Setting View Filters Database Column calculations