mobile databases, mobile forms, and mobile synchronization … where you need to work
Providing Pocket Access, Mobile Database, Windows CE Database, and Windows CE Development Solutions

Tip of the Month (April 2007)

"IF...THEN...ELSE", "FOR...NEXT", "WHILE...DO", and "REPEAT...UNTIL" in a Macro

When writing computer programs, "real" computer progammers control the sequence of operations using "IF...THEN...ELSE", "FOR...NEXT", "WHILE...DO", and "REPEAT...UNTIL". You can build these sort of consructs in a Visual CE macro.


First some background information...

Visual CE has a SKIP command. The SKIP command tells Visual CE to skip commands in a sequence. For example, if the commands are:

       Command 1: << operation 1 >>
       Command 2: SKIP 2
       Command 3: << operation 3 >>
       Command 4: << operation 4 >>
       Command 5: << operation 5 >>
       Command 6: << operation 6 >>
then operations 1, 5, and 6 will be performed.

The number of buttons to skip can be an expresssion. For example

       SKIP (3 + 5)
would skip 8 operations.

This expression can be in the form

       if << condition >> then << expression_1 >> else << expression_2 >>

In this example:

       Command 1: << operation 1 >>
       Command 2: SKIP (if @var(34) = 18 then 0 else 2)
       Command 3: << operation 3 >>
       Command 4: << operation 4 >>
       Command 5: << operation 5 >>
       Command 6: << operation 6 >>
operations 1, 3, 4, 5, and 6 will be done if @var(34) is 18. Otherwise only operations 1, 5, and 6 will be done.

Negative numbers can be used to skip backwards in the sequence. In this example:

       Command 1: << operation 1 >>
       Command 2: << operation 2 >>
       Command 3: << operation 3 >>
       Command 4: SKIP -2
operations 1, 2, 3, 2, 3, 2, 3, ... will be done (as an infinite loop).



So, here's how to create the control constructs:


if << condition >> then ... else ...

       Command 1: SKIP (if << condition >> then 0 else 3)
       Command 2: << operation 2 >>
       Command 3: << operation 3 >>
       Command 4: SKIP 2
       Command 5: << operation 5 >>
       Command 6: << operation 6 >>
       Command 7: << operation 7 >>


for @var(45) = 1 to 10 ... next

       Command 1: ASSIGN 1 to @var(45)
       Command 2: SKIP (if @var(45) > 10 then 4 else 0)
       Command 3: << operation 3 >>
       Command 4: << operation 4 >>
       Command 5: assign @var(45) + 1 to @var(45)
       Command 6: SKIP -4
       Command 7: << operation 6 >>


while (<< condition >>) do

       Command 1: SKIP (if << condition >> then 0 else 3)
       Command 2: << operation 2 >>
       Command 3: << operation 3 >>
       Command 4: SKIP -3
       Command 5: << operation 5 >>


repeat until << condition >>

       Command 1: << operation 1 >>
       Command 2: << operation 2 >>
       Command 3: SKIP (if << condition >> then 0 else -2)
       Command 4: << operation 4 >>