======================================================================
USE SSUB ON VARIABLES THAT HOLD MULTIPLE CHECKBOX PICKS
======================================================================
PRODUCT : R:BASE VERSION : 3.1B or Higher
CATEGORY : PROGRAMMING SUBCATEGORY : CHOOSE, SSUB
======================================================================
When the CHOOSE command presents you with a checkbox menu (CHKBOX), you
can check off more than one item on the menu list. R:BASE puts all the
items you choose into a single choose variable, separating items with
commas. Then you can use the SSUB function in a WHILE loop to pull apart
the resulting choose variable to use or count the individual choices.
Here are two examples. Both use the concomp sample database, and in both,
VCHKBOX is the choose variable that holds all the checked items. The code
changes the delimiter to an @ symbol for both the CHOOSE command and the
SSUB command. This is so the listed items will be separated by the @
symbol, thus allowing the code to extract data that might have embedded
commas.
Counting the Number of Items
============================
This example counts the number of items chosen from a CHKBOX menu.
*(CNTPICKS.CMD -- Count the CHKBOX items that were chosen. )
*( Saves current delimit setting. )
SET VAR vdelsave TEXT = (CVAL('delimit'))
LABEL rechoose
CLS
CLEAR VAR vchkbox, vcount, vitem, vyn, vend
SET VAR vchkbox TEXT, vyn TEXT, vend TEXT
*( Change the delimiter to account for data values that )
*( might have commas embedded within. )
SET DELIMIT=NULL
SET DELIMIT='@'
CHOOSE vchkbox FROM #VALUES FOR company FROM customer FOOTING CHKBOX
SET DELIMIT=NULL
SET DELIMIT=.vdelsave
*( Check for HELP or the absence of a selection )
IF vchkbox = 'HELP' OR vchkbox = '[ESC]' THEN
DIALOG 'No selection was made. Try again?' vyn vend YES
IF vyn = 'YES' THEN
PAUSE FOR 1 USING 'Remember to Press F2 when your selection is made'
GOTO rechoose
ELSE
GOTO the_end
ENDIF
ENDIF
SET VAR vcount INTEGER = 1
SET DELIMIT=NULL
SET DELIMIT='@'
*( Since we have changed the delimiter symbol, any command )
*( that requires a delimiter must reflect the current )
*( setting. That is why the @ symbol appears in the SSUB )
*( function on the next line. )
SET VAR vitem = (SSUB(.vchkbox@.vcount))
SET DELIMIT=NULL
SET DELIMIT=.vdelsave
WHILE vitem IS NOT NULL THEN
*( Insert code here to utilize the extracted item. )
SET VAR vcount = (.vcount + 1)
SET DELIMIT=@
SET VAR vitem = (SSUB(.vchkbox@.vcount))
SET DELIMIT=.vdelsave
ENDWHILE
SET VAR vcount = (.vcount - 1)
CLS
WRITE 'You chose',.vcount,'items.'
LABEL the_end
CLEAR VAR vchkbox, vcount, vitem, vyn, vend, vdelsave. vchkbox
RETURN
The IF block at the top ensures that you choose at least one item. Then
the WHILE loop keeps adding one to VCOUNT until VCOUNT is larger than
the total number of items in the CHOOSE variable (VCHKBOX in this
example). At that point, R:BASE sets VITEM to a null value and breaks
out of the WHILE loop. You therefore know that the CHOOSE variable
(VCHKBOX) contains (.VCOUNT-1) items when the WHILE loop finishes.
Building an IN List
===================
You can build an IN list from the CHOOSE variable by using the SSUB
function in a WHILE loop to pull out each item and enclose it in single
quotation marks. Quotation marks are required if an item might contain
more than one word. This code is very similar to the previous code; the
bold characters show the small additions.
*( MAKEIN.CMD - Make an "IN" list out of the checked items.)
*( Saves current delimiter setting. )
SET VAR vdelsave TEXT = (CVAL('delimit')
LABEL rechoose
CLS
CLEAR VAR vchkbox, vcount, vitem, vyn, vend, vinlist
SET VAR vchkbox TEXT, vyn TEXT, vend TEXT
*( Change the delimiter to account for data values that )
*( might have commas embedded within. )
SET DELIMIT=NULL
SET DELIMIT='@'
CHOOSE vchkbox FROM #VALUES FOR company FROM customer FOOTING CHKBOX
SET DELIMIT=NULL
SET DELIMIT=.vdelsave
*( Check for HELP or the absence of a selection )
IF vchkbox = 'HELP' OR vchkbox = '[ESC]' THEN
DIALOG 'No selection was made. Try again?' vyn vend YES
IF vyn = 'YES' THEN
PAUSE FOR 1 USING 'Remember to Press F2 when your selection is made'
GOTO rechoose
ELSE
GOTO the_end
ENDIF
ENDIF
SET VAR vcount INTEGER = 1
SET DELIMIT=NULL
SET DELIMIT='@'
SET VAR vitem TEXT = (SSUB(.vchkbox@.vcount))
SET DELIMIT=NULL
SET DELIMIT=.vdelsave
*( Put quotes around it )
SET VAR vinlist = (CHAR(39)+.vitem+CHAR(39))
WHILE vitem IS NOT NULL THEN
SET VAR vcount = (.vcount + 1)
SET DELIMIT=NULL
SET DELIMIT='@'
SET VAR vitem = (SSUB(.vchkbox@.vcount))
SET DELIMIT=NULL
SET DELIMIT=.vdelsave
IF vitem IS NOT NULL THEN
*( Add the new item to the list )
SET VAR vinlist = (.vinlist+','+CHAR(39)+.vitem+CHAR(39))
ENDIF
ENDWHILE
*( Enclose the "IN" list in parentheses. )
SET VAR vinlist = (CHAR(40)+.vinlist+CHAR(41))
CLS
*( Use "IN" list in WHERE clause )
PRINT customer WHERE company IN &vinlist
LABEL the_end
CLEAR VAR vchkbox, vcount, vitem, vyn, vend, vinlist
RETURN
Now you can add &VINLIST to any WHERE clause, as shown in the following
example.
SELECT * FROM customer WHERE custid IS NOT NULL AND company IN &vinlist