MapInfo Pro Developers User Group

Welcome to the MapInfo Pro Developers community!  We are a group dedicated to the discussion and understanding of MapBasic and .Net MapInfoPro AddIn development. Bring your questions and ideas here. This group includes several members of the Pro development team from around the world.

Please feel free to start a discussion in the discussion tab or join in a conversation.

Product Information  Ideas Portal  MapInfo Community Downloads

Discussions

Members

Resources

Events

 View Only
  • 1.  MapBasic table updates using conditions

    Posted 09-04-2017 06:18

    Hi,

    I have a table with several fields, and each field needs to be set to a specific number value IF it currently has a certain number value.

    That is, if any value in any field is -0.1 it needs to be changed to -0.2.

    So the logic is :

    if Item2 = -0.1 then set Item2 = -0.2

    elseif Item2 <> -0.2

    then Item2 = Item2

    end if

    Can any one put this into correct Mapbasic syntax?

    Thanks.

     



  • 2.  RE: MapBasic table updates using conditions

    Employee
    Posted 09-04-2017 05:16

    If you are using MapBasic, you can create a custom function and use this in your update statement:

     

    Function CustomIIF(ByVal fInputValue As Float, ByVal fCompareValue As Float, ByVal fOutputValue As Float) As Float

    If fInputValue = fCompareValue then

    CustomIIF = fOutputValue

    Else

    CustomIIF = fInputValue

    End if

    End Function

     

    Use the function like this:

     

    Update MYTABLE Set MYCOLUMN = CustomIFF(MYCOLUMN, -0.1, -0.2)



  • 3.  RE: MapBasic table updates using conditions

    Posted 09-06-2017 07:59

    Here is the complete mapbasic code that will serve your purpose ... Hope this helps ....

    ========================================================================================================

    include "mapbasic.def"

    include "Icons.def"

    include "Menu.def"

    Declare Sub Main

     

    Sub Main

    Dim Tabname,sColName,currcolumn,cmdstr as string

    Dim x,y as integer

    Dim currvalue as alias

    //Opening up your table

    Tabname = "C:\test.tab"

    open table Tabname as inputtable

    //Iterating through the table

    If Tableinfo(inputtable,TAB_INFO_NROWS) <> 0 then

    For x = 1 to TableInfo(inputtable,TAB_INFO_NROWS)

    Fetch rec x from inputtable

    For y=1 to TableInfo(inputtable, TAB_INFO_NCOLS)

    sColName = ColumnInfo(inputtable, "col"+str$(y), COL_INFO_NAME)

    currvalue = inputtable & "." & sColName

     

    if currvalue = "-0.1" then

    Update inputtable set sColName = -0.2 where rowid = x

    elseif currvalue <> "-0.1" then

    Update inputtable set sColName = currvalue where rowid = x

    End If

    Next

    Next

    Else

    Note "No records in the input table, pls check "

    End If

    End Sub



  • 4.  RE: MapBasic table updates using conditions

    Employee
    Posted 10-05-2017 12:54

    IIF() and COND() are built in functions that will be included with MapBasic 17.0.



  • 5.  RE: MapBasic table updates using conditions

    Posted 01-30-2018 22:07

    thanks, there were some errors with field types, so i will probably just do the updates manually



  • 6.  RE: MapBasic table updates using conditions

    Employee
    Posted 01-31-2018 08:14

    Try the new IIF() or COND() function out in the current Beta. Hopefully it will suit your purpose. Since you are looking for specific Item values, I might go with a Case statement rather than an If\Then\Else. Just a suggestion.

    Thx,

    -Bill