Page 1 of 1

Using a variable to refer to a field

Posted: Fri Feb 26, 2021 3:36 pm
by trags3
I have a series of fields named pHlp1,pHlp2 ...
Trying to find the first empty field (if any) and store that in another field... iFA (First Available)

Code: Select all

repeat with N=1 to 15
     put "pHlp" & N into emptyFLD
     if fld emptyFLD of cd"Page 2" of stack"MJADMIN" is empty then
         put N into field "iFA"
         exit repeat
     end if
 end repeat
I've tried quite a few different things on the third line to no avail.
I know this is basic but can't seem to find the answer.

Tom

Re: Using a variable to refer to a field

Posted: Fri Feb 26, 2021 4:10 pm
by Klaus
Hi Tom,

syntax seems correct!
What does not work?


Best

Klaus

Re: Using a variable to refer to a field

Posted: Fri Feb 26, 2021 4:16 pm
by bogs
I dunno, Tom, while I wouldn't have gone this way, it works here with this setup -

Image

...so at least one question comes to mind.
When you named the fields, for instance, is there a space between "pHlp" and the number? That would sure throw off the code.

Re: Using a variable to refer to a field

Posted: Fri Feb 26, 2021 4:59 pm
by trags3
Sorry Bogs :oops:
Where I have the problem is in the code that uses the result. I didn't have any coffee when I wrote the post.What I posted works swimmingly!

I'll work that for a few minutes and get back.
Tom

Re: Using a variable to refer to a field

Posted: Fri Feb 26, 2021 5:27 pm
by trags3
Hi Klaus and Bogs!
Here is where I have the problem.

Code: Select all

 repeat with N=1 to 15
      put "pHlp" & N into emptyFLD
      if fld emptyFLD of cd"Page 2" of stack"MJADMIN" is empty then
         put "pHlp" & N into field "ifa"
         exit repeat
      end if
   end repeat
   
   if the hilite of btn "ic1" then
      put fld"ifa" into tfirst
      put the label of btn"ic1" into fld"&tfirst" of cd"page 2" of stack "MJADMIN"
   end if
Thanks

Re: Using a variable to refer to a field

Posted: Fri Feb 26, 2021 5:43 pm
by Klaus
The only problem I see is:

Code: Select all

...
## put the label of btn "ic1" into fld "& tfirst" of cd"page 2" of stack "MJADMIN"
## results in -> field "&tFirst"
## Since this is a STRING!
## You should have seen the error -> no such object
put the label of btn "ic1" into fld tfirst of cd "page 2" of stack "MJADMIN"
...

Re: Using a variable to refer to a field

Posted: Fri Feb 26, 2021 5:53 pm
by trags3
Klaus,
Thank you so much!
Tom

Re: Using a variable to refer to a field

Posted: Fri Feb 26, 2021 5:56 pm
by Klaus
Please use some more SPACES to make you code better readable.

And you can directly use concatenated object names if you use parens:

Code: Select all

...
repeat with N = 1 to 15
      if fld ("pHlp" & N) of cd "Page 2" of stack "MJADMIN" is empty then
...

Re: Using a variable to refer to a field

Posted: Fri Feb 26, 2021 10:06 pm
by liveme
Trying to find the first empty field (if any) and store that in another field.
That problem, seems a bit funny depending how you read it:... :) ...and store empty field content (?)...into something else ? :lol:
(just kidding trags3 ) :wink:

Re: Using a variable to refer to a field

Posted: Sat Feb 27, 2021 3:13 am
by trags3
Hi Liveme
Here is an explanation of what I'm doing in this part of the App.
I have a set of fields that hold a list of selections by the user. I this case the number is 15.
The user may delete a field for any of a number of reasons.
I want to keep the list (is the word congruous?) with no empty lines in the middle of the list for cosmetic reasons. Also I need to find an empty field if another item is added to the list.
That is the whole purpose of finding the first empty field in a list and putting the number or other reference to that field in another field. ie fld"iFA" (First Available.)
I appreciate all the help I've been given off and on for the past 6 years :D

Tom