Looping A Table in MapBasic
Looping A Table in MapBasic
(http://www.pitneybowes.com/us/support.html)
Support
One of the often used things in MapBasic is to loop thru a table to read values from the table and
then react to or modify these values.
MapBasic has a number of looping methods:
However one seems better than the others when it comes to looping a table: Do Until ... Loop. At
least that's the one I have been using for years. You can also use Do
While ... Loop and While ... Wend.
But I will recommend that you stay away from For ... Next when looping thru a table. I'll get back to
why.
Now you need a few other statements as well when looping a table:
https://support.pitneybowes.com/VFP05_KnowledgeWithSidebarHowTo?id=kA180000000CtKCCA0&popup=false&lang=en_US 1/4
2/3/2021 Looping a table in MapBasic
nRowID = MY_TABLE.ROWID
oFeature = MY_TABLE.OBJ
In the example above I loop the table called MY_TABLE. For each record I read the RowID, the
hidden dynamic column referring to a specific post in a table or query, and the spatial object from
the column called OBJ.
Oh, and I set my application to use the same coordinate system as the table before I start looping.
Now this loop doesn't really do anything clever yet. I would need to add my logic just before
the Fetch Next From statement.
I also promised to explain why you shouldn't use the For ... Next loop when working with a table.
The problem isn't as much the loop, but more that people tend to combine this loop with a
different Fetch statement; Fetch Rec nRec From.
nRowID = MY_TABLE.ROWID
oFeature = MY_TABLE.OBJ
Next
This Fetch statement will put the table cursor at a specific row in the table, which in most case is
fine. But it has a drawback; it will fail if the specific row has been deleted. And if you haven't built-in
error handling in your application, the application will crash.
Fetch Next, on the other side, will skip the deleted records during the loop.
https://support.pitneybowes.com/VFP05_KnowledgeWithSidebarHowTo?id=kA180000000CtKCCA0&popup=false&lang=en_US 2/4
2/3/2021 Looping a table in MapBasic
_______________________________________________
Did this article answer your question?
Yes No Somewhat
Contact Us
Chat
Live Chat is currently unavailable
Chat is available Mon - Fri, 8am - 8pm ET
Global Contact Us
(http://www.pitneybowes.com/us/contact-dcs.html)
https://support.pitneybowes.com/VFP05_KnowledgeWithSidebarHowTo?id=kA180000000CtKCCA0&popup=false&lang=en_US 3/4
2/3/2021 Looping a table in MapBasic
(http://www.pitneybowes.com/us.html)
Shop
Customer Service
Follow Us
Legal (http://www.pitneybowes.com/us/legal.html)
Privacy Statement (http://www.pitneybowes.com/us/legal/privacy-statement.html)
Sitemap (http://www.pitneybowes.com/us/sitemap.html)
https://support.pitneybowes.com/VFP05_KnowledgeWithSidebarHowTo?id=kA180000000CtKCCA0&popup=false&lang=en_US 4/4