MapBasic is as powerful as it is frustrating. Theres no built-in way to get a list of open tables, so Ive written some code that does just that. First Ill explain how to populate an array with a list of table names and numbers, then how to select a table from this list using a dialogue box, then Ill package it all nicely into a sub and a function so that you will only need a couple of lines of code. If you dont want to understand and just want to DO things then just skip to the last section! Getting a list of all open tables This is fairly simple. NumTables() returns the total number of tables opened. All open tables are numbered sequentially, starting from 1. Therefore we just need to loop through, from 1 to NumTables() and put all the names into an array, making each names array index value correspond with its MapInfo table number. Like so: 1 2 3 4 5 6 7 8 Dim i as integer Dim j as integer i = NumTables() Dim TableArray(1) as String ReDim TableArray(i) For j = 1 to i TableArray(j) = Tableinfo(j, TAB_INFO_NAME) Next Choosing a table via a dialog Now that you have a variable (TableArray) that contains a list of all open table names with corresponding numbers. This can then be used in dialog boxes, for example in a drop-down menu (or as MapBasic calls it, a pop-up menu): 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Dim TableNum as Integer Dim TableName as String Dialog Title "Choose table" Control PopupMenu Title From Variable TableArray Into TableNum Control OKButton Control Cancelbutton
If CommandInfo(CMD_INFO_DLG_OK) Then TableName = TableArray(TableNum) Note "You chose: " & TableName End If Choosing a table with a sub and a function MapBasic functions cannot return arrays. Therefore you must create an array variable to store the list and then use a sub to insert the data into the array. Here are is everything above packaged into a sub and a function. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 Sub GetTableList (TableList() as String) Dim i as integer Dim j as integer i = NumTables() ReDim TableList(i) For j = 1 to i TableList(j) = Tableinfo(j, TAB_INFO_NAME) Next End Sub
Function ChooseTable As String Dim TableNum As Integer Dim TableArray(1) As String
Call GetTableList(TableArray)
Dialog Title "Choose table" Control PopupMenu Title From Variable TableArray Into TableNum Control OKButton Control Cancelbutton
If CommandInfo(CMD_INFO_DLG_OK) Then ChooseTable = TableInfo(TableNum, TAB_INFO_NAME) End If End Function and here is how you would use them to choose a layer: 1 2 Dim TableName as String TableName = ChooseTable() and heres how youd get an array of table names. 1 2 Dim TableArray(1) as String Call GetTableList(TableArray) You cant get much easier than that and if you can Id like to know how you did it! You can use exactly the same procedure to get a window name or a list of windows. You just need to swap table for window and TAB for WIN (literally a search and replace will work).