while loop stops before reaching end
I created a button in one table which is to export some values to another table creating new records there. I am using a while loop to create the records. But even a simplified version of the loop breaks before reaching the end.
I want to create records for each day between two dates (start, ending). The code looks like this:
let start := date(2022, 1, 1);
let ending := start + 100;
let d := start;
while d <= ending do
let newRec := (create DateSeries);
newRec.(Date := d);
d := d + 1
I would expect the button to create 100 records. But only around 10 to 28 records are created (varies).
I am running the desktop app (v3.6.6) on an M1 Mac (macOS 12.4). Does anyone have an idea what is wrong here?
Thanks a lot!
I too created the DateSeries Table
Copied the above script into a button on one of my other forms.
and pressed go!
It created the table no problem - id rec from 1 - 101 (i'm guessing the while does a loop at 0 or does all 100 and then commits suicide on run 101 ? or was start given the initial value of 1 then you added 100 to it - )
Your script looks good to me!
ps i'm on cloud version !
I have had another go at this and to be honest I cannot get it to fail.
I am still interested why the code run created 101 lines.
So if i have it right the first line already gave the variable start the value of 1- then then you added 100 to it. thus giving 101- am i right? (I don't really use while loops!)
What happens if you lower the rate to say 50 records - does it still only create 10 to 28 records
Also tested your imported DB. Like John's test it worked fine.
However - When I tested your script on your opening thread (i copied it) it created the records in the blink of an eye. Yet using your DB it touch a fair bit longer to do so.
So i'm inclined to agree with John in that maybe all those select statements are causing too many loops and not giving you enough memory headroom...
in your new table you created do you still have all those select statements?
This is definitely a weird one. I have the Mac app on my old (2015), but still functional MacBook Pro. I get the same results as schwiegel and Nick when I use the while loop, but it works correctly when I use a for loop. This is the code I used...
let startDate := date(2022, 1, 1); for i in range(0, 99) do let newRec := (create Test); newRec.(Date := startDate + i) end
If I execute the code from the Main table it takes a long time to complete like schwiegel mentions and if I execute it from the Test table it completes almost instantly. I agree that executing the code from the Main table shouldn't be affected by the 162 select statements since it is only writing records for the Test table. It's behaving like every formula is being refreshed during each iteration of the loop
I use the Mac app (v3.6.6) and the same thing happens for me. Main only creates 12 records while Test creates all 100. If I change the ending date to less than 12 then it creates 12. If I do 13 it only creates 12.
I would say to email Ninox support (email@example.com) to ask them why the while is acting funny.
schwiegel , as many have mention, too many select statements is not a good thing to do, regardless of this issue. What I have learned is to make one select statement into a variable and then use that in my scripts.
I see that you do a select Main, a lot. So in each formula field you could create a variable at the top for the select Main and then replace all other instances of the select with the variable.
Mel Charles , I think it creates 101 records, when working, is because the while command is based on the start date plus 100, so that should create 101 records (the start date is Jan 1 and 100 days from that would be 101 days) if I'm doing my math correctly.