Examples: GetErrorMessage method

  1. This agent checks for an error in the ODBCConnection object after calling the ListTables method. If the name of the data source is wrong, an error occurs.
    Uselsx "*LSXODBC"
    Sub Initialize
      Dim con As New ODBCConnection
      Dim msg As String
      tables = con.ListTables("ATDC")
      If con.GetError <> DBstsSuccess Then
        Messagebox con.GetExtendedErrorMessage,, _
        con.GetErrorMessage
        Exit Sub
      End If
      msg = "ATDB contains the following tables:" & Chr(10)
      For n% = Lbound(tables) To Ubound(tables)
        msg = msg & Chr(10) & tables(n%)
      Next
      Messagebox msg,, "Tables for ATDB"
      con.Disconnect
    End Sub
  2. This agent enables an error handler, which is called if any ODBC error occurs. The error handler displays the error message associated with the ODBCConnection object.
    Sub Initialize
      On Error Goto errorHandler
      Dim con As New ODBCConnection
      Dim msg As String
      tables = con.ListTables("ATDC")
      msg = "ATDB contains the following tables:" & Chr(10)
      For n% = Lbound(tables) To Ubound(tables)
        msg = msg & Chr(10) & tables(n%)
      Next
      Messagebox msg,, "Tables for ATDB"
      con.Disconnect
      Exit Sub
    errorHandler:
      Messagebox con.GetExtendedErrorMessage,, _
        con.GetErrorMessage
      con.Disconnect
      Exit Sub
    End Sub
  3. This agent enables an error handler. The error handler searches for an ODBC object that returns an error status (not success) and displays its error message.
    Sub Initialize
      Dim con As New ODBCConnection
      Dim qry As New ODBCQuery
      Dim result As New ODBCResultSet
      On Error Goto errorHandler
      Set qry.Connection = con
      Set result.Query = qry
      con.ConnectTo("ATDB")
      REM This causes an error during result.Execute
      qry.SQL =  _
      "SELECT * FROM STUDENTS WHERE ?STUDENT_ID? = ?1234?"
      result.Execute
      result.Close(DB_CLOSE)
      con.Disconnect
      Exit Sub
    errorHandler:
      If con.GetError <> DBstsSuccess Then 
        con.Disconnect
        Messagebox con.GetErrorMessage,, "Connect"
      Elseif qry.GetError <> DBstsSuccess Then
        con.Disconnect
        Messagebox qry.GetErrorMessage,, "Query"
      Elseif result.GetError <> DBstsSuccess Then
        result.Close(DB_CLOSE)
        con.Disconnect
        Messagebox result.GetErrorMessage,, "Result"
      End If
      Exit Sub
    End Sub
  4. This agent tests for DBstsODBC on a connection error. If DBstsODBC is the error, the agent tries to establish the connection again. Otherwise, the agent exits.
    Uselsx "*LSXODBC"
    Sub Initialize
      Dim con As New ODBCConnection
      Dim dsn As String
      Dim msg As String
      On Error Goto errorHandler
    getDsn:
      dsn = Inputbox("ODBC data source name", "DSN")
      If dsn = "" Then Exit Sub
      con.ConnectTo(dsn)
      tables = con.ListTables(dsn)
      msg = dsn & " contains the following tables: _
      & Chr(10)
      For n% = Lbound(tables) To Ubound(tables)
        msg = msg & Chr(10) & tables(n%)
      Next
      Messagebox msg,, "Tables for " & dsn
      con.Disconnect
      Exit Sub
    errorHandler:
      If con.GetError = DBstsODBC Then
        Messagebox "Enter correct DSN or nothing",, _
        "Bad data source name"
        Resume getDsn
      Else
        Messagebox con.GetExtendedErrorMessage,, _
        con.GetErrorMessage
        Exit Sub
      End If
    End Sub
  5. This agent contains a function that returns the name of an error number, given the number.
    Uselsx "*LSXODBC"
    Sub Initialize
      Dim con As New ODBCConnection
      Dim qry As New ODBCQuery
      Dim result As New ODBCResultSet
      On Error Goto errorHandler
      Set qry.Connection = con
      Set result.Query = qry
      con.ConnectTo("ATDB")
      REM This causes an error during result.Execute
      qry.SQL =  _
      ("SELECT * FROM STUDENTS WHERE ?STUDENT_ID? = 
      ?1234?")
      result.Execute
      Exit Sub
    errorHandler:
      If con.GetError <> DBstsSuccess Then
        Messagebox con.GetErrorMessage,, _
        WhatIsError(con.Error)
      Elseif qry.GetError <> DBstsSuccess Then
        Messagebox qry.GetErrorMessage,, _
        WhatIsError(qry.Error)
      Elseif result.GetError <> DBstsSuccess Then
        Messagebox result.GetErrorMessage,, _
        WhatIsError(result.Error)
      End If
      Exit Sub
    End Sub
    Function WhatIsError(number As Integer) As String
      Select Case number
      Case DBSTSACCS : WhatIsError = "DBSTSACCS"
      Case DBSTSAHVR : WhatIsError = "DBSTSAHVR"
      Case DBSTSBADP : WhatIsError = "DBSTSBADP"
      Case DBSTSCANF : WhatIsError = "DBSTSCANF"
      Case DBSTSCARR : WhatIsError = "DBSTSCARR"
      Case DBSTSCCON : WhatIsError = "DBSTSCCON"
      Case DBSTSCNVD : WhatIsError = "DBSTSCNVD"
      Case DBSTSCNVR : WhatIsError = "DBSTSCNVR"
      Case DBSTSCOAR : WhatIsError = "DBSTSCOAR"
      Case DBSTSCPAR : WhatIsError = "DBSTSCPAR"
      Case DBSTSCXIN : WhatIsError = "DBSTSCXIN"
      Case DBSTSENTR : WhatIsError = "DBSTSENTR"
      Case DBSTSEOFD : WhatIsError = "DBSTSEOFD"
      Case DBSTSFAIL : WhatIsError = "DBSTSFAIL"
      Case DBSTSHSTMT : WhatIsError = "DBSTSHSTMT"
      Case DBSTSILLG : WhatIsError = "DBSTSILLG"
      Case DBSTSINTR : WhatIsError = "DBSTSINTR"
      Case DBSTSINVC : WhatIsError = "DBSTSINVC"
      Case DBSTSINVR : WhatIsError = "DBSTSINVR"
      Case DBSTSMEMF : WhatIsError = "DBSTSMEMF"
      Case DBSTSNAFI : WhatIsError = "DBSTSNAFI"
      Case DBSTSNCOJ : WhatIsError = "DBSTSNCOJ"
      Case DBSTSNCOL : WhatIsError = "DBSTSNCOL"
      Case DBSTSNCON : WhatIsError = "DBSTSNCON"
      Case DBSTSNODA : WhatIsError = "DBSTSNODA"
      Case DBSTSNOEX : WhatIsError = "DBSTSNOEX"
      Case DBSTSNQOJ : WhatIsError = "DBSTSNQOJ"
      Case DBSTSNUNQ : WhatIsError = "DBSTSNUNQ"
      Case DBSTSODBC : WhatIsError = "DBSTSODBC"
      Case DBSTSPMIS : WhatIsError = "DBSTSPMIS"
      Case DBSTSRCHG : WhatIsError = "DBSTSRCHG"
      Case DBSTSRDON : WhatIsError = "DBSTSRDON"
      Case DBSTSROWD : WhatIsError = "DBSTSROWD"
      Case DBSTSRUNC : WhatIsError = "DBSTSRUNC"
      Case DBSTSSNFD : WhatIsError = "DBSTSSNFD"
      Case DBSTSUBLE : WhatIsError = "DBSTSUBLE"
      Case DBSTSUNIM : WhatIsError = "DBSTSUNIM"
      End Select
      Mid(WhatIsError, 3, 5) = "sts"
    End Function