Run Command statement - MapBasic - 2023

MapInfo MapBasic Reference

Product type
Software
Portfolio
Locate
Product family
MapInfo
Product
MapInfo > MapBasic
Version
2023
Language
English
Product name
MapBasic
Title
MapInfo MapBasic Reference
First publish date
1985
Last updated
2023-09-12
Published on
2023-09-12T16:32:32.686312

Purpose

Executes a MapBasic command represented by a string. You can issue this statement from the MapBasic window in MapInfo Pro.

Syntax

Run Command command 

command is a character string representing a MapBasic statement.

Description

The Run Command statement interprets a character string as a MapBasic statement, then executes the statement.

The Run Command statement has some restrictions, due to the fact that the command parameter is interpreted at run-time, rather than being compiled. You cannot use a Run Command statement to issue a Dialog statement. Also, variable names may not appear within the command string; that is, variable names may not appear enclosed in quotes. For example, the following group of statements would not work, because the variable names x and y appear inside the quotes that delimit the command string:

' this example WON'T work
Dim cmd_string As String 
Dim x, y As Float

cmd_string = " x = Abs(y) "
Run Command cmd_string 

However, variable names can be used in the construction of the command string.

In the following example, the command string is constructed from an expression that includes a character variable.

'this example WILL work
Dim cmd_string As String 
Dim map_it, browse_it As Logical 

Open Table "world"
If map_it Then
	cmd_string = "Map From "
	Run Command cmd_string + "world" 
End If
If browse_it Then
	cmd_string = "Browse * From " 
	Run Command cmd_string + "world" 
End If 

Example

The Run Command statement provides a flexible way of issuing commands that have variable-length argument lists. For example, the Map From statement can include a single table name, or a comma-separated list of two or more table names. An application may need to decide at run time (based on feedback from the user) how many table names should be included in the Map From statement. One way to do this is to construct a text string at run time, and execute the command through the Run Command statement.

Dim cmd_text As String
Dim cities_wanted, counties_wanted As Logical

Open Table "states"
Open Table "cities"
Open Table "counties"

cmd_text = "states" ' always include STATES layer 

If counties_wanted Then 
	cmd_text = "counties, " + cmd_text
End If

If cities_wanted Then
	cmd_text = "cities, " + cmd_text
End If 

Run Command "Map From " + cmd_text 

The following example shows how to duplicate a Map window, given the window ID of an existing map. The WindowInfo() function returns a string containing MapBasic statements; the Run Command statement executes the string.

Dim i_map_id As Integer 

' First, get the ID of an existing Map window 
' (assuming the Map window is the active window): 
i_map_id = FrontWindow() 

' Now clone the active map window: 
Run Command WindowInfo(i_map_id, WIN_INFO_CLONEWINDOW)

See Also:

Run Application statement, Run Menu Command statement, Run Program statement