0

I've been trying to invoke my first MB function to do some error handling and I keep getting an "Unrecognised command: OpenTab".

We have

Function OpenTab(ByVal TableName as String, ByVal sAlias as String) As Logical

OnError Goto NotOpen  

Open Table TableName+".TAB" As sAlias Interactive
Print "Requested table:" + sAlias
OpenTab = TRUE
Exit Function

NotOpen:
OpenTab = FALSE
print "---------------------------------------"
print "Failed to open table "+TableName
print "---------------------------------------"
End Function

and

'Open Table "G:\GIS_Tables\Vector_Data\Administrative\Boundaries\DSC\Boundary_DSC" As Boundary_DSC Interactive
OpenTab ("G:\GIS_Tables\Vector_Data\Administrative\Boundaries\DSC\Boundary_DSC", Boundary_DSC)

The code is from Avoid MapBasic crash when unable to open file

What do I need to do to get this working?

1

Have you got OpenTab declared as a function at the top of your code or in an included .def file?

eg. Declare Function OpenTab(ByVal TableName as String, ByVal sAlias as String) As Logical

Also, you want to store (or use directly) the return value, a logical, from the function.

eg.

Dim bOpened as Logical

bOpened = OpenTab("G:\GIS_Tables\Vector_Data\Administrative\Boundaries\DSC\Boundary_DSC", "Boundary_DSC")

If Not bOpened then
    ' failed to open table...
End if

Note the double quotes around the second function argument where you are passing the alias as a string. If you are passing a string literal it must be within quotations.

* EDIT *

In response to your comment, here is some code that should do the job for you. It will compile as it is, you just need to change the myPath variable to a valid file path on your system. I have added comments so you can see what's going on.

Include "MapBasic.def"

Declare Sub Main
Declare Function OpenTab(ByVal strTablePath as String, ByVal strAlias as String) as Logical

Define ERR_INDENT Chr$(10) & Chr$(10) & Chr$(9)

Sub Main()

Dim myAlias, myPath as String

    myAlias = "test"
    myPath = "..\some table path\.."

    If Not OpenTab(myPath, myAlias) then    '// check if OpenTab function was successful
        Note "Failed to open table" & myPath & " as " & myAlias
    Else
        Note "Table " & myPath & " was opened successfully as " & myAlias
    End if

End Sub

Function OpenTab(ByVal strTablePath as String, ByVal strAlias as String) as Logical

Dim nTables, i as Integer

    '// if path does not end in TAB file extension then append it
    If Right$(strTablePath, 4) <> ".TAB" then
        strTablePath = strTablePath & ".TAB"
    End if

    '// check that tab file actually exists
    If Not FileExists(strTablePath) then
        Note "Table " & strTablePath & " does not exist."
        OpenTab = FALSE
        Exit Function
    End if

    '// check if tab file is currently open, if it is then close it
    nTables = NumTables()
    i = nTables
    While i > 0
        If TableInfo(i, TAB_INFO_TABFILE) = strTablePath then
            Close Table TableInfo(i, TAB_INFO_NAME)
        End if
        i = i - 1
    WEnd

    '// open tab file using the given alias. if there is an error go to error handling
    OnError GoTo CaughtEx
    Open Table strTablePath as strAlias
    OpenTab = TRUE

Exit Function

'// error handling
CaughtEx:
    Note "Error while attempting to open table " & strTablePath & ":" & ERR_INDENT & Error$()
    OpenTab = FALSE

End Function
  • Thanks. That worked but the system still crashes. I was trying to find a code that would close the open table (if it's already open) and then open it again with the properties of the function. – GeorgeC Feb 13 '17 at 23:40
  • @GeorgeC I've edited my answer with some code that should give you the functionality you are after. – T_Bacon Feb 14 '17 at 9:43
  • Thanks. It works but I need to run this test it on hundreds of files that are opened in our custom mapbasic based interface. Is it possible to just be able to call it in one line so >> OpenTab ('path of file','name of table')? – GeorgeC Feb 20 '17 at 7:00
  • Sure, just create a Logical variable (e.g. bOpened) to store the return value. e.g. bOpened = OpenTab('path of file', 'name of table') – T_Bacon Feb 20 '17 at 10:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.