VERIFIED SOLUTION i

Resolve the MapBasic error: "Invalid Dialog preserve command. There is no active dialog" 

Products Affected: MapBasic
 

Issue

The below MapBasic/similar code results in the following error message: "Invalid Dialog preserve command. There is no active dialog" 

' * Include mapbasic.def - required for various MapBasic commands including "CMD_INFO_XXX" 

Include "mapbasic.def" 

' * OK button handler for Dialog 

Declare Sub gmeu_goToGridReference ( gmeu_userEnteredGridRef As String ) 

Sub gmeu_goToGridReference ( gmeu_userEnteredGridRef As String ) 

Dialog Preserve 
Print "You entered: " + gmeu_userEnteredGridRef 

End Sub 

Declare Sub Main 

Sub Main 

' * Create a custom dialog box to obtain the grid reference from the user 

Dim gmeu_userEnteredGridRef As String 

Dialog 
Title "Go to Grid Reference" 
Control StaticText 
Title "Enter a grid reference:" 
Control EditText 
Into gmeu_userEnteredGridRef 
Control OKButton 
Control CancelButton 
If CommandInfo( CMD_INFO_DLG_OK ) Then 
Call gmeu_goToGridReference( gmeu_userEnteredGridRef ) 
End If 

End Sub 

Cause

The MapBasic documentation of the Dialog Preserve statement states that the statement "may only be issued from within a sub procedure that acts as a handler for an OKButton or CancelButton control." In other words, when defining the custom dialog's OKButton or CancelButton the "Calling..." clause must be included . 

As a regular call to the Call statement was made, by the time the program reached the Call statement then the dialog was gone and the error message was expected. 

 

Resolution

UPDATED: November 28, 2017
Include a Calling..." clause when defining OKButton or CancelButton.

For example:

Declare Sub okHandler 
Sub okHandler 
Dialog Preserve 
Print "In okHandler; you entered: " + ReadControlValue(1)
End Sub 

Declare Sub Main
Sub Main
Dim gmeu_userEnteredGridRef As String
Dialog
Title "Go to Grid Reference"
Control StaticText
Title "Enter a grid reference:"
Control EditText ID 1 
Into gmeu_userEnteredGridRef
Control OKButton Calling okHandler 
Control CancelButton
End Sub