Adding an ignore statement or 'IF statement' - if a table blank

41 views
Skip to first unread message

new user1234

unread,
Jun 6, 2021, 7:04:47 AM6/6/21
to MapInfo-L
Hello everyone,

I'm new to MapBasic, but very much enjoying my journey in learning how to automate some of the processes.

I'm a bit stuck on modifying some code and wondered if I could ask for some help?

I have gone back in the group thread and copied a program that selects all polygons on a table and then changes them all to whatever style, color and pen that is programmed in.

This works well (once i figured out how to find the codes for the different patterns and colors etc)

What I would like to do is 'daisy chain' this code together so that i can update multiple tables with different style attributes.

Here is the code which I have duplicated and deleted lines so that it all runs from one to the next:

Include "MapBasic.def"

Dim oUpdateObj as Object
Dim b1 as Brush
Dim p1 as Pen
Dim x as Integer


Select * from MYTABLEONE where Str$(obj) = "Region" into NewStyle

Fetch first from NewStyle

x = 1

Do
        oUpdateObj = NewStyle.obj

        b1 = makebrush (5,255,-1) '<--- Change values for your style
        p1 = makepen (30,1,0) '<--- Change values for your style
        Alter Object oUpdateObj Info OBJ_INFO_BRUSH, b1
        Alter Object oUpdateObj Info OBJ_INFO_PEN, p1

        Update NewStyle set Obj = oUpdateObj where RowID = x

        x = x + 1

        Fetch next from NewStyle

Loop while x <= TableInfo(NewStyle,TAB_INFO_NROWS)


'-------------------------------------------------------------------------------
Select * from  MYTABLETWO  where Str$(obj) = "Region" into NewStyle

Fetch first from NewStyle

x = 1

Do
        oUpdateObj = NewStyle.obj

        b1 = makebrush (5,300,-1) '<--- Change values for your style
        p1 = makepen (30,2,0) '<--- Change values for your style
        Alter Object oUpdateObj Info OBJ_INFO_BRUSH, b1
        Alter Object oUpdateObj Info OBJ_INFO_PEN, p1

        Update NewStyle set Obj = oUpdateObj where RowID = x

        x = x + 1

        Fetch next from NewStyle

Loop while x <= TableInfo(NewStyle,TAB_INFO_NROWS)


I can keep adding instances of this and it will loop through my list of tables. However, the problem I have encountered is that if one of the tables in my file structure happens to be empty for this particular workspace project then the program stops, prints an error statement and doesn't continue to change the styles of the tables further down the list.

I understand some basic microsoft excel functions so I know that I could write an IF statement which could say "if this cell happens to be blank then do this" 

Is it possible to 'wrap' this code in some kind of IF statement that says "If the table is empty then ignore and carry on. If the table contains data (in this case regions or polygons) then run the looping code that is currently here - then move on?

Apologies this is so long, but would very much appreciate any help or advice.

Many thanks,

Gav






Uffe Kousgaard

unread,
Jun 6, 2021, 9:19:45 AM6/6/21
to mapi...@googlegroups.com
Hi,

It is quite simple:

If TableInfo(NewStyle,TAB_INFO_NROWS)>0 then
.....
end if

Regards
Uffe Kousgaard
--
--
You received this message because you are subscribed to the
Google Groups "MapInfo-L" group.To post a message to this group, send
email to mapi...@googlegroups.com
To unsubscribe from this group, go to:
http://groups.google.com/group/mapinfo-l/subscribe?hl=en
For more options, information and links to MapInfo resources (searching
archives, feature requests, to visit our Wiki, visit the Welcome page at
http://groups.google.com/group/mapinfo-l?hl=en

---
You received this message because you are subscribed to the Google Groups "MapInfo-L" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mapinfo-l+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/mapinfo-l/42d6168a-4be8-4a83-a982-4426c46c8c40n%40googlegroups.com.

accumanddis trading

unread,
Jun 6, 2021, 5:35:40 PM6/6/21
to mapi...@googlegroups.com
Hi Uffe, thanks so much for the quick reply.

I will try this out. I'm guessing I insert my repeating bit of the code from the "select * from" downwards into the part where you have put the dots.

Will report back.

Thanks once again for your help :)

Gav

Peter Horsbøll Møller

unread,
Jun 7, 2021, 2:07:44 AM6/7/21
to mapi...@googlegroups.com

Hi Gav

 

You may also consider restructuring you script and use a function to change the style of your objects.

This will perform better and also make it easier to add more tables to your script.

 

Include "MapBasic.def"

 

Declare Sub Main

Declare Sub ChangeRegionStyleForTable(ByVal sTab As String, ByVal penNew As Pen, ByVal brsNew As Brush)

Declare Function AlterRegionStyle(ByVal oRegion As Object, ByVal penNew As Pen, ByVal brsNew As Brush) As Object

 

'-------------------------------------------------------------------------------

Sub Main

 

Dim b1 As Brush

Dim p1 As Pen

 

      b1 = makebrush (5,255,-1) '<--- Change values for your style

      p1 = makepen (30,1,0) '<--- Change values for your style

      Call ChangeRegionStyleForTable("MYTABLEONE", p1, b1)

 

      b1 = makebrush (5,300,-1) '<--- Change values for your style

      p1 = makepen (30,2,0) '<--- Change values for your style

      Call ChangeRegionStyleForTable("MYTABLETWO", p1, b1)

 

End Sub

 

'-------------------------------------------------------------------------------

Sub ChangeRegionStyleForTable(ByVal sTab As String, ByVal penNew As Pen, ByVal brsNew As Brush)

 

      Select * from MYTABLEONE

            where Str$(obj) = "Region" into NewStyle NoSelect Hide

 

      If TableInfo(NewStyle, TAB_INFO_NROWS) > 0 then

            Update NewStyle

                  Set OBJ = AlterRegionStyle(OBJ, penNew, brsNew)

      End If

 

      Close Table NewStyle

 

End Sub

 

'-------------------------------------------------------------------------------

Function AlterRegionStyle(ByVal oRegion As Object, ByVal penNew As Pen, ByVal brsNew As Brush) As Object

 

      AlterRegionStyle = oRegion

 

      '**Only modify area types

      If ObjectInfo(oRegion, OBJ_INFO_TYPE) In (OBJ_TYPE_REGION, OBJ_TYPE_RECT, OBJ_TYPE_ROUNDRECT) Then

            Alter Object oRegion Info OBJ_INFO_BRUSH, brsNew

            Alter Object oRegion Info OBJ_INFO_PEN, penNew

 

            AlterRegionStyle = oRegion

      End If

 

End Function

 

To add another table into the mix, you only have to add another three lines in the Main sub specifying the pen and brush, and then calling ChangeRegionStyleForTable.

I haven’t tried running this tool so there could be some minor mistakes

 

Peter Horsbøll Møller

www.precisely.com

 

Peter Horsbøll Møller
Principal Sales Engineer - Distinguished Engineer

 

From: mapi...@googlegroups.com <mapi...@googlegroups.com> On Behalf Of accumanddis trading
Sent: 6. juni 2021 23:35
To: mapi...@googlegroups.com
Subject: Re: [MI-L] Adding an ignore statement or 'IF statement' - if a table blank

 

This message originated Externally. Use proper judgement and caution with attachments, links, or responses.

 

accumanddis trading

unread,
Jun 7, 2021, 2:17:23 AM6/7/21
to mapi...@googlegroups.com
That's amazing, cheers Peter. I will try building this today. I hadn't gotten as far as trying to understand functions yet, but I'm guessing that the efficiency it creates by having less code, and better structure, could mean it cycles through the tables quicker.

thanks for your help,

Gav

new user1234

unread,
Jun 13, 2021, 11:03:38 AM6/13/21
to MapInfo-L
Hello Peter, Hello Uffe,

I have been trying both solutions this weekend, I'm sorry I haven't been able to get either working as yet.

Uffe - I think I'm probably not putting your 'IF' statement in the right place in the code. I built 3 test tables
TEST_TABLE_1
TEST_TABLE_2 (this is the blank table with no polygon)
TEST_TABLE_3

I keep getting the following error:
(changestylesuffe.mb:9) Variable or Field NewStyle not defined



I have wrapped each looping program in the IF statement as follows:





Include "MapBasic.def"

Dim oUpdateObj as Object
Dim b1 as Brush
Dim p1 as Pen
Dim x as Integer


If TableInfo(NewStyle,TAB_INFO_NROWS)>0 then
Select * from TEST_TABLE_1 where Str$(obj) = "Region" into NewStyle

Fetch first from NewStyle

x = 1

Do
        oUpdateObj = NewStyle.obj

        b1 = makebrush (2,16711680,-1) '<--- Change values for your style
        p1 = makepen (30,2,0) '<--- Change values for your style
        Alter Object oUpdateObj Info OBJ_INFO_BRUSH, b1
        Alter Object oUpdateObj Info OBJ_INFO_PEN, p1

        Update NewStyle set Obj = oUpdateObj where RowID = x

        x = x + 1

        Fetch next from NewStyle

Loop while x <= TableInfo(NewStyle,TAB_INFO_NROWS)
end if
'-------------------------------------------------------------------------------
If TableInfo(NewStyle,TAB_INFO_NROWS)>0 then
Select * from TEST_TABLE_2 where Str$(obj) = "Region" into NewStyle

Fetch first from NewStyle

x = 1

Do
        oUpdateObj = NewStyle.obj

        b1 = makebrush (2,16744448,-1) '<--- Change values for your style
        p1 = makepen (30,2,0) '<--- Change values for your style
        Alter Object oUpdateObj Info OBJ_INFO_BRUSH, b1
        Alter Object oUpdateObj Info OBJ_INFO_PEN, p1

        Update NewStyle set Obj = oUpdateObj where RowID = x

        x = x + 1

        Fetch next from NewStyle

Loop while x <= TableInfo(NewStyle,TAB_INFO_NROWS)
end if
'-------------------------------------------------------------------------------
If TableInfo(NewStyle,TAB_INFO_NROWS)>0 then
Select * from TEST_TABLE_3 where Str$(obj) = "Region" into NewStyle

Fetch first from NewStyle

x = 1

Do
        oUpdateObj = NewStyle.obj

        b1 = makebrush (2,65280,-1) '<--- Change values for your style
        p1 = makepen (30,2,0) '<--- Change values for your style
        Alter Object oUpdateObj Info OBJ_INFO_BRUSH, b1
        Alter Object oUpdateObj Info OBJ_INFO_PEN, p1

        Update NewStyle set Obj = oUpdateObj where RowID = x

        x = x + 1

        Fetch next from NewStyle

Loop while x <= TableInfo(NewStyle,TAB_INFO_NROWS)
end if

I also tried it with just one If statement wrapped around everything but that hasn't worked. Please could I ask you where in the prog the If statement should go? and do I need to put multiple If statements in?




Hi Peter,
I added in the three lines for each new table, as you mentioned. It seems to run when a table is empty, however it's not picking up the changes in colors as per each table that I define in the sub Main.
The bit that is also confusing me, is that there is a select all * from TEST_TABLE_1 in the second section of the code. However, i'm not clear if I should be repeating that section for each table or is this basically telling MapInfo
the table in the list I want to start running the program from?

This is the copy of your code I have been trying:

Include "MapBasic.def"

 

Declare Sub Main
Declare Sub ChangeRegionStyleForTable(ByVal sTab As String, ByVal penNew As Pen, ByVal brsNew As Brush)
Declare Function AlterRegionStyle(ByVal oRegion As Object, ByVal penNew As Pen, ByVal brsNew As Brush) As Object
'-------------------------------------------------------------------------------
Sub Main
Dim b1 As Brush
Dim p1 As Pen

      b1 = makebrush (2,16711680,-1) '<--- Change values for your style

      p1 = makepen (30,2,0) '<--- Change values for your style

      Call ChangeRegionStyleForTable("Test_Table_1", p1, b1)
 

      b1 = makebrush (2,16744448,-1) '<--- Change values for your style

      p1 = makepen (30,2,0) '<--- Change values for your style

      Call ChangeRegionStyleForTable("Test_Table_2", p1, b1)


 b1 = makebrush (2,65280,-1) '<--- Change values for your style

      p1 = makepen (30,2,0) '<--- Change values for your style

      Call ChangeRegionStyleForTable("Test_Table_3", p1, b1)


 End Sub
'-------------------------------------------------------------------------------

Sub ChangeRegionStyleForTable(ByVal sTab As String, ByVal penNew As Pen, ByVal brsNew As Brush)
Select * from Test_Table_1
where Str$(obj) = "Region" into NewStyle NoSelect Hide
If TableInfo(NewStyle, TAB_INFO_NROWS) > 0 then
Update NewStyle
Set OBJ = AlterRegionStyle(OBJ, penNew, brsNew)
End If
Close Table NewStyle
End Sub
'-------------------------------------------------------------------------------

Function AlterRegionStyle(ByVal oRegion As Object, ByVal penNew As Pen, ByVal brsNew As Brush) As Object
AlterRegionStyle = oRegion
'**Only modify area types
If ObjectInfo(oRegion, OBJ_INFO_TYPE) In (OBJ_TYPE_REGION, OBJ_TYPE_RECT, OBJ_TYPE_ROUNDRECT) Then
Alter Object oRegion Info OBJ_INFO_BRUSH, brsNew
Alter Object oRegion Info OBJ_INFO_PEN, penNew
AlterRegionStyle = oRegion
End If
End Function


Hope you don't mind the extra questions, and i do appreciate your help

thanks

Gav

Peter Horsbøll Møller

unread,
Jun 14, 2021, 5:34:47 AM6/14/21
to mapi...@googlegroups.com

Hi

 

You need to revert the order of the If and the Select statement:

 

From:

Include "MapBasic.def"

 

Dim oUpdateObj as Object

Dim b1 as Brush

Dim p1 as Pen

Dim x as Integer

 

If TableInfo(NewStyle,TAB_INFO_NROWS)>0 then

Select * from TEST_TABLE_1 where Str$(obj) = "Region" into NewStyle

 

To:

Include "MapBasic.def"

 

Dim oUpdateObj as Object

Dim b1 as Brush

Dim p1 as Pen

Dim x as Integer

 

If TableInfo(NewStyle,TAB_INFO_NROWS)>0 then

Select * from TEST_TABLE_1 where Str$(obj) = "Region" into NewStyle

 

The table NewStyle hasn’t been created when you check how many records it holds.

 

Peter Horsbøll Møller

www.precisely.com

 

Peter Horsbøll Møller
Principal Sales Engineer - Distinguished Engineer

 

Reply all
Reply to author
Forward
0 new messages