Normal Topic FormThrowFocus or ThrowFocus or GOTO? (Read 1149 times)
obfusc88
Full Member
***
Offline


No personal text

Posts: 194
Joined: Dec 17th, 2005
FormThrowFocus or ThrowFocus or GOTO?
Aug 4th, 2011 at 5:00pm
Print Post Print Post  
I have a series tests to do some validation.  If any test fails I want to go back to the first field on the form.
Using FromThrowFocus or ThrowFocus does not break out of the program.  They are designed to finish the current program before going to the first field.  Using GOTO also seems to continue the current program.  How do I change from this:
Code
Select All
// Test Length for Color Codes
vColorLength = @Len(vNewString)
If vColorLength <> vLength Then {
	vErrorCode = "Bad Color length"
	srFailed(Color,vErrorCode)
	GoTo Product
	}

// Test Color Codes
vColor = @InStr(vColor1, vValidColors)
If vColor = 0 Then {
		vErrorCode = "Bad Color code"
		srFailed(Color,vErrorCode)
		FormThrowFocus("Product Choices", "Product")
		} 


to something that does what I want?  This only shows two test, but there are a series of them, more than 10.  I want to do the tests in sequence, but as soon as one fails, I want to break out this programming and force the user back to the first data entry field, "Product" to start data entry over again.  After the last test, there is additional programming that follows.  The code is in "On Element Change"

What happens now, is on a failure, the subroutine just shows the error message associated for the error, and ends.  But the programming continues through the additional tests.  I thought control would return from the ssubroutine and proceed to the FormThrowFocus or GOTO command, but it seems to return control to the end of the IF statement.
  
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: FormThrowFocus or ThrowFocus or GOTO?
Reply #1 - Aug 4th, 2011 at 5:29pm
Print Post Print Post  
Hello,

FormThrowFocus, ThrowFocus and GoTo do not cause the current programming event to stop running. It will run till completion and then the cursor will be placed in the element you specified. To achieve what you want, you are going to want to structure your code like below so that each If statement checks to see if vErrorCode has been set. If vErrorCode has been set, then that If statement evaluates to False and Sesame moves on to the next If statement.

At the top of your event after variable declarations
Code
Select All
vErrorCode = "" 



then

Code
Select All
// Test Length for Color Codes
vColorLength = @Len(vNewString)
If vColorLength <> vLength and vErrorCode = "" Then
{
	vErrorCode = "Bad Color length"
	srFailed(Color,vErrorCode)
}

// Test Color Codes
vColor = @InStr(vColor1, vValidColors)
If vColor = 0 and vErrorCode = "" Then
{
	vErrorCode = "Bad Color code"
	srFailed(Color,vErrorCode)
}

//****Rest of your checks here****

If vErrorCode <> "" Then
{
	ThrowFocus(Product)
}
Else
{
	//If you want to do something if there is no errors that code would go here.
}
 



-Ray
« Last Edit: Aug 5th, 2011 at 1:04pm by Ray the Reaper »  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
obfusc88
Full Member
***
Offline


No personal text

Posts: 194
Joined: Dec 17th, 2005
Re: FormThrowFocus or ThrowFocus or GOTO?
Reply #2 - Aug 4th, 2011 at 6:53pm
Print Post Print Post  
Nope, that won't do it. 

That runs all tests, and moves Focus to Product after all tests are done, if any one of them failed.

I want to move Focus to Product (or another field) as soon as any one of the tests failed. Some failures may need to go to a different field, not always to Product.  I don't want to continue testing until each preceding check has been passed.  I need to break out of the testing steps immediately upon a failure, and Throw focus to the proper field depending on which test failed.
  
Back to top
 
IP Logged
 
Ray the Reaper
Global Moderator
Members
Lantica Support
*****
Offline


The One & The Only

Posts: 2482
Joined: Aug 20th, 2003
Re: FormThrowFocus or ThrowFocus or GOTO?
Reply #3 - Aug 4th, 2011 at 7:06pm
Print Post Print Post  
Hello,

I thought the focus was always going back to product based on your original post Quote:
I want to do the tests in sequence, but as soon as one fails, I want to break out this programming and force the user back to the first data entry field, "Product" to start data entry over again.


Regardless the code would be the same just with the Throwfocus moved. After one test fails, all the other If Statements will evaluate to False.

After variable declarations
Code
Select All
vErrorCode = ""  



then

Code
Select All
// Test Length for Color Codes
vColorLength = @Len(vNewString)
If vColorLength <> vLength and vErrorCode = "" Then
{
	vErrorCode = "Bad Color length"
	srFailed(Color,vErrorCode)
	ThrowFocus(Product)
}

// Test Color Codes
vColor = @InStr(vColor1, vValidColors)
If vColor = 0 and vErrorCode = "" Then
{
	vErrorCode = "Bad Color code"
	srFailed(Color,vErrorCode)
	ThrowFocus(Product)
}
 



Another way to write this would be

Code
Select All
vColorLength = @Len(vNewString)
vColor = @InStr(vColor1, vValidColors)

If vColorLength <> vLength Then
{
	vErrorCode = "Bad Color length"
	srFailed(Color,vErrorCode)
	ThrowFocus(Product)
}
Else If vColor = 0 Then
{
	vErrorCode = "Bad Color code"
	srFailed(Color,vErrorCode)
	ThrowFocus(Product)
}
Else If
{
	//Next check
}
 



-Ray
« Last Edit: Aug 5th, 2011 at 1:05pm by Ray the Reaper »  

Raymond Yoxall Consulting
ray.yoxall@gmail.com
ryoxall@lantica.com
Sesame Applications, Design and Support
Back to top
IP Logged
 
obfusc88
Full Member
***
Offline


No personal text

Posts: 194
Joined: Dec 17th, 2005
Re: FormThrowFocus or ThrowFocus or GOTO?
Reply #4 - Aug 4th, 2011 at 7:41pm
Print Post Print Post  
That is just a variation of what I have already tried.  But the problem is that although ThrowFocus command is there, it does not actually throw the focus until the rest of the testing code is done.

Maybe the answer is to make each test a subroutine with Throw focus as the last line in the SR? 
Does ThrowFocus consider the original code that called the SR as the one to be completed, or does is use just the SR that called it?  If it uses the SR, then that should ThrowFocus right away since there is no more code to be processed, and I would not return to the line of code that called the SR?  Would like to thoroughly understand the logic used by ThrowFocus before I start doing a lot of cut/past to make SRs
  
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: FormThrowFocus or ThrowFocus or GOTO?
Reply #5 - Aug 4th, 2011 at 11:10pm
Print Post Print Post  
Building a pinball machine?

Anyways, if you follow the logic in Ray's second example, it won't run more tests (or any other code) after the first test-failure. Basically, wrap the tests in the success condition for the previous test.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
obfusc88
Full Member
***
Offline


No personal text

Posts: 194
Joined: Dec 17th, 2005
Re: FormThrowFocus or ThrowFocus or GOTO?
Reply #6 - Aug 5th, 2011 at 3:08am
Print Post Print Post  
Okay, I think I got it figured out.  Not totally fixed, but closer to understanding what is happening.  I had to insert a lot of WriteLn commands to trace the programming steps.
I modified my code to the second format that Ray provided making each test its own ELSE section.  But, even when a test failed, it still ran code at the bottom of the tests that I did not want running.

One of the things that happens in SubRoutine is to increment vFailed.  It aborts the process after three failures.
So, I changed the SubRoutine to a Function, FuncFailed as Int with the same input variables, and returned vFailed as a value.
Code
Select All
Function FuncFailed(Color as String,vErrorCode as String) as Int
     Function steps in these lines ....
    ........
     ........
     Return vFailed
End Function 



I declared the variable vFailed at the top of the code.  It is already declared and initialized to 0 in the Global Code section where its value is updated in the FuncFailed.
Then I changed the lines calling the subroutine to a function call:
Code
Select All
var vFailed as Int
.....
.....
.....
vErrorCode="Some text values"
vFailed=FuncFailed(Color,vErrorCode). 



And finally at the end of the testing ELSE loops, I inserted the following line:
Code
Select All
          }
     }
}
If vFailed = 0 Then {
      The rest of the code goes in here between the braces....
       ..........
       ..........
       } 


That forced the flow after the tests to skip the rest of the code.  Looks good so far....
Now the big surprise... When a test failed, the flow went to the end as programmed above, but instead of Focus going to Product field, it went right back to this field, looking for ImmediateChange programming.  It waits for key input, or else you need to manually move the focus somewhere else. So this is now the definition of my problem.  Why is it flowing like this?

Product is the first field, is a combo box, is not ReadOnly.
This field is the second field , so don't understand why ThrowFocus("Product") would come here.  I might understand that if Product was ReadOnly (like in Q&A movements).  

Oh yes, one more thing.  I need to add a note about the sample code that was provided.
The ThrowFocus lines need the quotes removed around the element name.
Change ThrowFocus("Product") to ThrowFocus(Product).
But the quotes are needed if using FormThrowFocus!

Thanks for the help so far, still trying to trace why ThrowFocus comes back to this same element vs. moving to Product.

  
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: FormThrowFocus or ThrowFocus or GOTO?
Reply #7 - Aug 5th, 2011 at 1:04pm
Print Post Print Post  
Again: Pinball Machine!

This logic:
Code
Select All
r = test1()
if(r = FAIL)
{
    // do fail stuff
}
else
{
    r = test2()
    if(r = FAIL)
    {
	  // do fail stuff
    }
    else
    {
	  r = test3()
	  if(r = FAIL)
	  {
		// do fail stuff
	  }
	  else
	  {
		// final success code
	  }
    }
}
 


If you use the logic above, there will be no need to a final conditional at the end of your routine.

As to why the focus does not land in the intended field: try commenting out all of the other programming (backup it all up first) to see if some other factor is affecting focus. If that isn't it, try putting a simple text element as the target of the focus. When in doubt as to the cause of a problem: simplify, eliminate possibilities.
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged