Show Contents / Index / Search

Handling Connection Errors

When you write a macro that makes a connection, you should include an error handling routine to trap any runtime errors that may occur when Reflection 2008 attempts the connection. In the event of an error, the error handler prevents the macro from stopping at the point where the error occurs, and proceeds with the macro, as appropriate.

By using the On Error statement and the Number property of the Err object (Err.Number), you can trap connection-related errors and retrieve the error code. After you trap the error, you can retrieve a predefined text string that describes the error by using the Err.Description or the ConnectionErrorMessage property.

Note: Visual Basic supports the older Error function in addition to the Err object. The Err object's default property is Number, but may be represented by the object name Err. Earlier code that uses the Err function or Err statement doesn't require modification.

The following guidelines can help you determine the best method for retrieving an error description:


If this occurs

Use this method


Err.Number returns the constant rcErrConnectionError

A general connection failure occurred. Use the ConnectionErrorMessage property.


Err.Number does not return rcErrConnectionError

Use Err.Description. To handle the error more specifically, use the constant returned by Err.Number. For example, if Err.Number returns the constant rcErrAlreadyConnected, indicating that a connection already exists, you can add an option that prompts the user to disconnect and try again.

Note: If you use the ConnectionErrorMessage property to retrieve the error text string, you will retrieve incorrect information. The ConnectionErrorMessage property always contains the text of the most recent general connection failure—or an empty string if no general connection failure occurred.

To see a list of the constants returned by Err.Number when a connection- related error occurs, open the Reflection 2008 Programming Help and search for Connections, error handling in the index.

Error constants are also listed in Rwinapi.txt and can also be viewed using the Visual Basic Editor's Object Browser.


The following example configures a Telnet connection and then prompts you for a host name, because none is specified. After you enter a host name and click OK, the connection is attempted. If the connection fails, the macro's error handler displays a message box which offers you the opportunity to try the connection again. Without the error handler, the macro simply stops if the connection cannot be established.

Sub ConnectionErrorDemo

Dim theErrorMsg As String ' To hold the error message.

Dim theErrorText As String ' To hold longer error text.

Dim theResult As Integer ' Value from Try Again dialog.


On Error GoTo Handler

If ThisIbmTerminal.IsConnected = True Then

MsgBox "You are already connected."

Exit Sub

End If

ThisIbmTerminal.HostAddress = "[Host IP address]"


If ThisIbmTerminal.IsConnected = False Then Err.Raise 513, ,"Connection failed."

Exit Sub


theErrorMsg = Err.Description

theErrorText = theErrorMsg

theErrorText = theErrorText & vbCr & vbLf & vbLf & "Try again?"

theResult = MsgBox(theErrorText, vbOKCancel,"Connection Error")

If theResult = vbCancel Then

Exit Sub


Resume TryToConnect

End If

End Sub