MapInfo Pro Developers User Group

 View Only
  • 1.  Add only certain tables to array

    Posted 12-27-2021 15:39
    Edited by Asko Poder 12-30-2021 06:18
    Hallo

    How can I add only certain table types (for example normal (i.e. not linked) tables) to array variable from all opened tables. Script below does not work when TableInfo(i, TAB_INFO_TYPE) is used as condition. Not working indicator for me --- when choosing table from list (although popup menu list shows only "normal" tables as desired) the final variable value is missing (attachment "NotWorking"). When uncommenting out red lines in code below, then final variable value is not missing (correct) (attachment "Without_type_condition"), but popup menu list is showing all opened tables (not desired).



    Include "mapbasic.def"
    Include "IMapInfoPro.def"
    Include "Enums.def"
    Include "Menu.def"
    Include "Icons.def"

    Declare Sub main


    Sub main

    Dim Array1(), SelectedTable as String
    Dim NumOfTables, i, j, SelectionFromArray1 as Integer


    NumOfTables = NumTables()
    ReDim Array1(NumOfTables)


    For i=1 to NumOfTables
    j=TableInfo(i, TAB_INFO_TYPE) '**when this line is commented out the script will work, but I'd like to have only normal tables in array variable.
    If j=1 then '**when this line is commented out the script will work, but I'd like to have only normal tables in array variable.
    Array1(i)=TableInfo(i, TAB_INFO_NAME)
    End If '**when this line is commented out the script will work, but I'd like to have only normal tables in array variable.
    Next


    Dialog
    Title "Dialog Title"

    Control StaticText
    Position 5,5
    Title "Select table from list"

    Control PopupMenu
    ID 1
    Position 5,15 Width 250 Height 100
    Title from variable Array1
    into SelectionFromArray1


    Control OKButton
    Control CancelButton

    If CommandInfo(CMD_INFO_DLG_OK) Then
    ' table_to_update=Loend1(NrLoendist1)
    SelectedTable=Array1(SelectionFromArray1)
    Note SelectedTable
    End If

    End Sub

    ------------------------------
    Asko Poder
    Knowledge Community Shared Account
    ------------------------------


  • 2.  RE: Add only certain tables to array

    Posted 12-30-2021 02:34
    Hi Asko,

    I noticed you are not populating your array properly.  You need to adjust as follows:

    Dim nArrayIndex As Integer
    nArrayIndex = 0  ' Initialisation
    For i=1 to NumOfTables
      j=TableInfo(i, TAB_INFO_TYPE) 
      If j=1 then
        nArrayIndex = nArrayIndex + 1
        Array1(nArrayIndex)=TableInfo(i, TAB_INFO_NAME)
      End If
    Next
    ReDim Array1(nArrayIndex)  ' Adjust array bounds based on the number of actual items.

    The reason is because the array is not going to include all of the open tables.  You might have 10 open tables, but only 5 of these are of table type 1.  You were ending up with blank entries in the array that was affecting the Popup Menu results.


    ------------------------------
    James Nolet
    Dooley Mitchell & Morrison Pty Ltd
    Mentone, VIC, Australia
    ------------------------------



  • 3.  RE: Add only certain tables to array

    Posted 12-30-2021 06:18
    Edited by Asko Poder 12-30-2021 06:26
    Thank you very much for your answer, that did the trick. As an unexperienced MapBasic user I could not have figured it out myself :). Now I can move forward with my script.

    Best

    Asko Põder

    ------------------------------
    Asko Poder
    Knowledge Community Shared Account
    ------------------------------



  • 4.  RE: Add only certain tables to array

    Posted 12-30-2021 20:35
    I'm happy to have been able to help.  Good luck with the rest of your script!

    ------------------------------
    James Nolet
    Dooley Mitchell & Morrison Pty Ltd
    Mentone, VIC, Australia
    ------------------------------