Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

IF problem

Status
Not open for further replies.

woodyinoz

IS-IT--Management
Jan 8, 2002
215
GB
Hi all,

Below is a list of IF statements that I am trying to run on a form. Each has an ELSE condition and should display required details in the 'Route' field.

However, when running the code I come up with a problem. When the criteria of the IF statements is met the detail displayed in the field is correct, however when the criteria is not met the same ELSE value is used for all IF statements. That is the last one in the list,
Route = "MESSERtf"

Can anyone see where I'm going wrong?!

method depart(var eventInfo MoveEvent)

if USER2 = "TF" AND NHG >=1 then

Route = "FARL_MESStf"

ELSE

if USER2 = "BF" AND NHG >=1 then

Route = "FARL_MESSbf"

ELSE

if USER2 = &quot;WEB&quot; AND THICK <=25 then

Route = &quot;FARLEYweb&quot;

ELSE

if USER2 = &quot;SPBF&quot; AND THICK <=15 then

Route = &quot;FARLEYspbf&quot;

ELSE

if USER2 = &quot;SPTF&quot; AND THICK <=15 then

Route = &quot;FARLEYsptf&quot;

ELSE

if USER2 = &quot;SPWEB&quot; AND THICK <=15 then

Route = &quot;FARLEYspweb&quot;

ELSE

if USER2 = &quot;STF&quot; AND THICK <=25 AND NHG >=1 then

Route = &quot;FARLEYstf&quot;

ELSE

if USER2 = &quot;STF&quot; AND THICK >=26 AND NHG >=1 then

Route = &quot;MESS_HARTstf&quot;

ELSE

Route = &quot;MESSERstf&quot;
Route = &quot;MESSERstf&quot;

Route = &quot;MESS_HARTspweb&quot;
Route = &quot;MESS_HARTsptf&quot;
Route = &quot;MESS_HARTspbf&quot;

Route = &quot;FARL_MESSweb&quot;

Route = &quot;MESSERbf&quot;
Route = &quot;MESSERtf&quot;

endif
endif
endif
endif
endif
endif
endif
endif


Thanks,

Woody.
 
Woody,

With IF statements, remember that all conditions need to be true, otherwise control flows to the next else statement.

Based on the code itself, it looks like User2, NHG, and Thick are fields and you're trying to set a value of a fourth field based on the values in the first three. Is that correct?

If so, let's see what happens when you add

Code:
   doDefault

before the first IF statement in the code. If that accomplishes what you're after, then the problem lies in the way the event model is intepreting your code. If this is what's going on, then the type of effect you're trying to accomplish is sometimes called a &quot;side-effect;&quot; that is, a result of some action on the part of the user.

However, because your code is currently executing before the default behavior of the depart event, the values in the fields may not yet match the ones you're looking for.

Part of the default behavior of the depart even submits the values typed into the screen controls to the actual underlying fields. Since you're (by default) trying to pull the value property of these fields, you're asking for the underlying field value. Since it hasn't been submitted yet (because the default behavior hasn't yet taken place), then the code is not giving you the results you're looking for.

Now, that may not be the problem. You have some pretty complicated logic and I'm not sure I'm completely following what you're after. When I'm in similar circumstances, I ususally try to simplify things a little and see if that makes things clearer.

With that in mind, see if this captures the basic idea of what you're trying to accomplish:

Code:
switch 

   case ( User2 = &quot;TF&quot; ) OR ( User2 = &quot;BF&quot; ) :
      if NHG >= 1 then
         Route = &quot;FARL_MESS&quot; + lower( User2 )
      else
         Route = &quot;MESSER&quot; + lower( User2 )
      endIf

   case User2 = &quot;WEB&quot; : 
   
      if Thick <= 25 then
         Route = &quot;FARLEYweb&quot;
      else
         Route = &quot;FARL_MESSweb&quot;


   case ( USER2 = &quot;SPBF&quot; ) OR
        ( USER2 = &quot;SPTF&quot; ) OR 
        ( USER2 = &quot;SPWEB&quot; ) : 
        
      if Thick <= 15 then
         Route = &quot;FARLEY&quot; + lower( User2 )
      else
         Route = &quot;MESS_HART&quot; + lower( User2 )
      endIf


   case  USER2 = &quot;STF&quot; :

      if NHG >= 1 then
      
         if Thick <= 25 then
            Route = &quot;FARLEYstf&quot;
         else
            Route = &quot;MESS_HARTstf&quot;
         endIf
         
      else
         Route = &quot;MESSERstf&quot;
      endIf

   otherwise : Route = &quot;????&quot;

endSwitch

You'll note that I pulled a couple of new tricks out of the syntax bad. First, SWITCH lets you provide a linear set of conditions to test. It also helps you separate bnusiness rules from each other. Also, you'll notice that I consolidated a couple of your individual tests into more general ones. For example the TF and BF tests are essentially the same; the results only vary by the specific field value. So, I used string concatenation and a formatting function to return the original value you'd coded.

I suspect User2, Thick, Route, and NHG are all fields. Is this correct? If so, you might consider adding .Value to eahc reference. For example:

Code:
   case ( User2.Value = &quot;TF&quot; ) OR 
              ( User2.Value = &quot;BF&quot; ) :
      if NHG.Value >= 1 then
         Route.Value = &quot;FARL_MESS&quot; + lower( User2 )
      else
         Route.Value = &quot;MESSER&quot; + lower( User2 )
      endIf[code]

Personally, I find this easier to read.  While it is more typing, I've found it helpful to be as specific as possible, especially when working with ObjectPAL as there are times when Paradox is forced to assume things that I'd rather it didn't.

Hope this helps...

-- Lance
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top