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:
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.
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."
ThisIbmTerminal.HostAddress = "[Host IP address]"
If ThisIbmTerminal.IsConnected = False Then Err.Raise 513, ,"Connection failed."
theErrorMsg = Err.Description
theErrorText = theErrorMsg
theErrorText = theErrorText & vbCr & vbLf & vbLf & "Try again?"
theResult = MsgBox(theErrorText, vbOKCancel,"Connection Error")
If theResult = vbCancel Then