SheerPower® 4GL
A Guide to the SheerPower Language


Previous Contents Index

9.6 Storing Selections from a Form into Variables

Values chosen from a dialogbox form drop-down menu can be stored into variables and used to be displayed later.

The way to do this is to regenerate the DIALOGBOX string with the default where it needs to be.

Example 9-53 Storing a select option into a variable for displaying later

  form_menu$ = '<form>' 
  form_menu$ = form_menu$ + 'Age: <select name=age>' 
  form_menu$ = form_menu$ + '<option value="_age_">_age_' 
  form_menu$ = form_menu$ + '<option value="21">21' 
  form_menu$ = form_menu$ + '<option value="22">22' 
  form_menu$ = form_menu$ + '<option value="23">23' 
  form_menu$ = form_menu$ + '</select>' 
  form_menu$ = form_menu$ + '</form>' 
 
  last_age$ = "23" 
  do
    default_age$ = last_age$ 
    default_form$ = replace$(form_menu$,'_age_='+default_age$) 
    input dialogbox default_form$: choice$ 
    if _exit then exit do
    for item = 1 to pieces(choice$, chr$(26)) 
      z0$ = piece$(choice$, item, chr$(26)) 
      varname$ = element$(z0$, 1, '=') 
      value$   = element$(z0$, 2, '=') 
      select case varname$ 
      case 'age' 
        last_age$ = value$ 
      case else
      end select
    next item 
  loop
  end

9.7 Performing Data Record Lookups and Updating the Record

Below is an example of creating a dialogbox form where data records from a data structure are accessed and able to be updated.

Example 9-54 Storing a select option into a variable for displaying later

// Simple customer query 
open structure cust: name 'sheerpower:\samples\customer', access outin
cust$ = '12513' 
set structure cust, field custnbr: key cust$  // do the search 
cname$   = cust(name) 
 
do
  
  dbox$ = '<form>' + 
          'Customer: <input type=text name=cnbr value=' + quote$(cust$) + '>' + 
          '<br>' + 
          'Name: <input type=text name=cname value=' + quote$(cname$) + '>' + 
          '<br>' + 
          '</form>' 
  input dialogbox dbox$: formdata$ 
  if _exit or _back then exit do
  if _help then repeat do
  
  z0$ = element$(formdata$, 1, chr$(26)) 
  cust$ = element$(z0$, 2, '=') 
  
  z0$ = element$(formdata$, 2, chr$(26)) 
  cname$ = element$(z0$, 2, '=') 
 
  set structure cust, field custnbr: key cust$  // do the search 
  if _extracted = 0 then
    message error: 'Cannot find '; cust$ 
    repeat do
  end if
  if cname$ = cust(name) then repeat do // nothing to do 
  cust(name) = cname$  // update the name 
loop
end

References used to gather information on HTML tags

http://www.htmlhelp.com

http://archive.ncsa.uiuc.edu/General/Internet/WWW/HTMLPrimerAll.html

http://www.webspawner.com/cc/html/alpha.html

http://www.willcam.com/cmat/html/crossref.html

http://www.htmlgoodies.com


Chapter 10
Loops, Conditionals, and Chaining

A LOOP is a section of code that can be repeated. There are two types of loops.

A FOR loop is used to repeat a block of code a specific number of times. FOR loops also inform the user how many times the loop was executed. A FOR loop might be used to input 10 similar data items or as a counter; for example, to count from 1 to 1000, or to make calculations from each of the 10 data items entered.

DO loops are used to execute a block of code until a specific condition is met. For instance, a DO loop might be useful to enter numbers until a 0 is entered. Additionally, DO loops are used to do calculations until two numbers match, or to continue a process until either the user chooses to stop or until a final result is reached.

Loops are constructs--they are created by using several statements which can only be used in conjunction with one another (FOR/NEXT, DO/LOOP). The statements which make up the constructs are described together.

10.1 FOR/NEXT Loop

FORMAT:


        FOR index_var = num_expr1 TO num_expr2 [STEP num_expr3] 
                ---                           
                ---  block of code 
                --- 
        NEXT index_var 

EXAMPLE:

Example 10-1 FOR/NEXT loop

  dim name$(4) 
  for j = 1 to 4 
    input 'Enter name': name$(j) 
    print j; ' '; name$(j) 
  next j 
  print 'Finished' 
  print 'Final value:'; j 
  end
 
 
Enter name? Jack 
 1  Jack 
Enter name? Tom 
 2  Tom 
Enter name? Sue 
 3  Sue 
Enter name? Toby 
 4  Toby 
Finished 
Final value: 5 

PURPOSE:

The FOR loop executes a block of code a specific number of times. This construct can be used to repeat a section of code a certain number of times.

DESCRIPTION:

In the above example, the INPUT and PRINT statements make up the body of the loop. This block of code is executed each time the loop is repeated. (For clarity, the body of the loop is indented two spaces.) The FOR statement marks the beginning of the loop and determines how many times the loop is repeated. The NEXT statement marks the end of the loop.


         index variable 
                 | 
                 V 
             for J = 1 to 4   <-- limit expression 
                     ^ 
                     | 
             initial expression 

General Information

The index variable keeps track of how many times the loop has been executed. The initial expression is the number SheerPower begins counting at. The limit expression is the number SheerPower stops counting at. In the example, SheerPower counts from 1 to 4, so the loop executes four times.

When SheerPower runs the example program, it executes the loop four times. The first time the FOR statement is executed, the variable J is set to 1. SheerPower executes the body of the loop. Since J = 1, SheerPower inputs NAME$(1), Jack, and prints NAME$(J).


        J = 1       Enter name? Jack 
                     1  Jack 

When SheerPower reaches the NEXT J, it adds one to J and jumps back to the beginning of the loop. J is now 2. SheerPower checks to see if J is greater than 4. Since J isn't greater than 4, SheerPower repeats the loop. When SheerPower executes the loop for the last time, it jumps back to the beginning of the loop and checks to see if J is greater than 4. Since J is greater than 4, SheerPower jumps to the statement following the NEXT J (PRINT 'Finished') and continues normal program execution.


J = 1       Enter name? Jack 
             1  Jack 
J = 2       Enter name? Tom 
             2  Tom 
J = 3       Enter name? Sue 
             3  Sue 
J = 4       Enter name? Toby 
4  Toby 
Finished 
Final value: 5 

10.1.1 The STEP Option

By default, when a FOR loop is executed, SheerPower increments the index variable by one. The increment can be changed with the STEP option. The format of the FOR statement with the STEP option is:


        FOR index_var = num_expr1 TO num_expr2 STEP num_expr3 
            --- 
            ---  block of code 
            --- 
        NEXT index_var 

num_expr3 is a numeric expression specifying the increment. Each time the FOR statement is executed, SheerPower adds the increment to the index variable. SheerPower stops executing the loop when the index variable is greater than the limit.

Example 10-2 STEP option in FOR/NEXT loop

  dim name$(4) 
  for j = 1 to 4 step 3 
    input 'Enter name': name$(j) 
    print j; ' '; name$(j) 
  next j 
  print 'Finished' 
  print 'Final value:'; j 
  end
 
 
Enter name? Fred 
 1  Fred 
Enter name? John 
 4  John 
Finished 
Final value: 7 

10.1.2 Nesting Loops

FOR loops can be nested. A nested loop is a loop which begins and ends inside of another loop. Loops cannot overlap. The inner loop must begin and end completely within the outer loop.

Example 10-3 Nesting FOR/NEXT loops

  dim name$(4)           
  for j = 1 to 4      //<--- start of outer loop 
    input name$(j) 
    for k = 1 to j 
      print name$(k); '  ';            //<--- inner loop 
    next k 
    print        
  next j                               //<--- end of outer loop 
  print 'Finished' 
  end
   
 
? FRED                  <--- type in FRED 
FRED 
? JOHN                  <--- type in JOHN 
FRED  JOHN 
? MARY                  <--- type in MARY 
FRED  JOHN  MARY 
? KATE                  <--- type in KATE 
FRED  JOHN  MARY  KATE 
Finished 


Previous Next Contents Index