I want to design a popupmenu and I use SQL to find the title I need but I need to convert the table to an array in order to use Title From Variable xxx
Always use Fetch First, Fetch Next when looping thru a table. Using the Fetch Rec No isn't recommended as that won't work if the record you try to fetch has been deleted.
Here is a function that does the trick:
Include "MapBasic.def" Function LoadValues(arrValues() As String, ByVal sTab As String, ByVal sCol As String) As Integer Dim aRow As Alias Dim i As Integer aRow = sTab & "." & sCol ReDim arrValues(TableInfo(sTab, TAB_INFO_NROWS) Fetch First From sTab Do Until EOT(sTab) i = i + 1 arrValues(i) = aRow Fetch Next From sTab Loop ReDim arrValues(i) LoadValues = i End Function
This could of course be extended to add a group by and an order by to the table and column to only use each unique value once and have the values ordered
PS: This works properly for a Char column. You would have to format your result if the column is of any other type.
Something like this:
Dim ArrayVarriable() as String Select count(*) Count from [SqlTable] into tmpCount Dim iCount as integer iCount = tmpCount.Count Redim ArrayVariable(iCount) Dim i as integer For i = 1 to iCount Select foo from [SqlTable] where RowID = [counterVariableName] into tmpTable ArrayVariable(i) = [SqlTable].foo Next