Normal Topic Using a variable in a FOR - NEXT loop (Read 1031 times)
NHUser
Full Member
***
Offline



Posts: 320
Location: Amherst, NH
Joined: Aug 2nd, 2010
Using a variable in a FOR - NEXT loop
Apr 18th, 2014 at 8:04pm
Print Post Print Post  
I'm trying to create a loop to check two items and depending on result, change the color of an other element. 

I've set up a variable vCheck.  I want the loop to adjust the names of the elements to check.  I've used the following format:
FOR vLoop=1 to 10

vCheck="LotNumber"+@Str(vLoop)+"Approved"

NEXT

When I run this vLoop nothing happens.  However, If I replace the vCheck statement above with this:

vCheck ="LotNumber1Approved"

The programming works!

My programming would be much more efficient with a FOR/NEXT loop.  Am I missing something?

Thanks!

Paul




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



Posts: 2530
Joined: Nov 22nd, 2002
Re: Using a variable in a FOR - NEXT loop
Reply #1 - Apr 18th, 2014 at 8:09pm
Print Post Print Post  
The only code I see in your loop simply sets vCheck, but does not use it. When the loop exits, vCheck is set to "LotNumber10Approved" - the last value of vLoop.

Is there more code in the loop that you are not showing?

Try something like this:
Code
Select All
for vLoop = 1 to 10
    vCheck="LotNumber" + @Str(vLoop) + "Approved"
    writeln(vCheck)
next
writeln("All Done: " + vCheck)
 

  

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



Posts: 320
Location: Amherst, NH
Joined: Aug 2nd, 2010
Re: Using a variable in a FOR - NEXT loop
Reply #2 - Apr 21st, 2014 at 12:45pm
Print Post Print Post  
Yes, there is more programming.  I'm simply trying to change the background color of 10 elements depending on the value in the LotNumberXApproved" elements (where "X" is 1 through 10 for the ten elements).

The point is when I use "LotNumber10Approved" in my program for vCheck, the programming works but if I use "LotNumber"+Str(vLoop)+"Approved" the programming doesn't work although the variable looks correct when I verify it using a writeln statement.

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



Posts: 2530
Joined: Nov 22nd, 2002
Re: Using a variable in a FOR - NEXT loop
Reply #3 - Apr 21st, 2014 at 12:56pm
Print Post Print Post  
How are you deriving an element reference from the string?

If you hand code each of the ten element referencing strings, do they all work? Have you used writeln to compare the working hand-coded element references with those built through string concatenation?

When you say it doesn't work, does that mean that it throws an error or that the color doesn't change, or neither, or both? If it does throw an error - which one? ...On which command?
  

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



Posts: 320
Location: Amherst, NH
Joined: Aug 2nd, 2010
Re: Using a variable in a FOR - NEXT loop
Reply #4 - Apr 21st, 2014 at 2:21pm
Print Post Print Post  
What I mean is that the command simply doesn't work.  The colors do not change.  There is no error message.
  
Back to top
 
IP Logged
 
The Cow
YaBB Administrator
*****
Offline



Posts: 2530
Joined: Nov 22nd, 2002
Re: Using a variable in a FOR - NEXT loop
Reply #5 - Apr 21st, 2014 at 3:09pm
Print Post Print Post  
When it fails, what is @error?

Also, how are you getting an element reference from the string?

Are these elements in a main form or sub-form, and if in a subform - is it a tableview subform?
  

Mark Lasersohn&&Programmer&&Lantica Software, LLC
Back to top
IP Logged
 
Carl Underwood
Senior Member
Members
*****
Offline



Posts: 1351
Location: New Hampshire
Joined: Mar 11th, 2003
Re: Using a variable in a FOR - NEXT loop
Reply #6 - Apr 21st, 2014 at 6:27pm
Print Post Print Post  
Here's a stripped down portion of my own code that does what I think you are trying to do. The overall process of mine is more elaborate than just what's here, but I think this is the heart of what you are after.

Code
Select All
vList = "LE1;LE2:LE3"

For i = 1 to @CountStringArray(vList)
	vLE = @AccessStringArray(vList, i)

	If @IsBlank(@(vLE)) and @Mode() <> 2	// Blank and not in Search Mode
	{
		// Highlight background in yellow
		Attribute(@(vLE), 5, "255 255 0")	// 5 = ATTR_ID_BACK_RGB_COLOR
		vCount += 1
	}
	Else					// In Search Mode or not blank
	{
		// Restore background color to white
		Attribute(@(vLE), 5, "255 255 255")	// 5 = ATTR_ID_BACK_RGB_COLOR
	}
Next 


  


Carl Underwood
CDU Computer Consulting LLC
Epsom, New Hampshire
Back to top
IP Logged