Below is a coding example showing how FileOpenDlg() can be used to prompt the user to select a file to open in MapInfo. The user in this case is not prompted to open a specific file. Note that even if a default path and name are offered the user is free to choose a different path and file name.
' Purpose: Demonstrate the FileOpenDlg() function
' Includes
Include "MAPBASIC.DEF"
' Declares
Declare Sub Main
Declare Sub WorkWithTable(ByVal sTargetTable As String)
' Function: Main()
' Purpose: Main Control
Sub Main()
' Create a variable to hold the chosen filename and assign the user's choice to this variable
Dim s_filename As String
s_filename = FileOpenDlg("","","","")
' Check to see if a filename has been chosen
If (s_filename <> "") Then
Open Table s_filename
Call WorkWithTable(s_filename)
Else
Note "No table chosen. Closing the program."
End Program
End If
End Sub
' Procedure: WorkWithTable()
' Purpose: Carry out tasks on the table
Sub WorkWithTable(ByVal sTargetTable As String)
If (sTargetTable <> "") Then
' Use TableInfo to obtain the tablename
sTargetTable = TableInfo(0, TAB_INFO_NAME)
Map From sTargetTable ‘Open a map
Browse * From sTargetTable 'Open a browser
Else
Note "No Table Found"
End Program
End If
End Sub
If you run this code a dialogue box opens similar to the one below:-
There is further information on the FileOpenDlg() function in the MapBasic Help. Here you will find a complete description of all the fuctionality avaiable including the following code snippet :-
Dim s_filename As String
s_filename = FileOpenDlg("","","TAB","Open Table")
As discussed in previous blogs not all code snippets are suitable for copying and pasting into MapBasic for running on their own (even though they may successfully compile). The above code snippet, like other examples within MapBasic Help, is only given as a reminder of the syntax needed within your program should you want, as in this case, to prompt the user to open a file.