Using the Internet Transfer Control: Part 2
When looking at the ErrorConstants, there are quite a lot. Well, don't worry, we don't need all of them. I have outlined the important ones below:
- icBadUrl
- icCannotConnect
- icConnectFailed
- icConnectionAborted
- icNoRemoteHost
Most of them are self explanatory, although I do want to point out one thing. The difference between icConnectFailed and icConnectionAborted is that icConnectionAborted shows that we were previously connected, but the remote host disconnected us.
Well, now that we have all these nice constants, how can we use them to detect and find out what's gone wrong?
You might have guessed that we need to work in the StateChanged event. We need to use the Select Case statement to detect the State, because it's a real pain to use lots of If statements:
Select Case State Case icError ' An error has occurred End Select
A good way to find out what's going on is to use the Debug.Print method in the StateChanged event. For example, when using the code in the first part of the series to get a web page, you would see the following displayed in the Immediate window:
1 7 8 9 10
Uh?? What's all this? Am I supposed to memorize these numbers? No. The constants listed earlier with the numbers in brackets refer to these numbers. Here is what the numbers above translate to:
- 1 - icResolvingHost
- 7 - icReceivingResponse
- 8 - icResponseReceived
- 9 - icDisconnecting
- 10 - icDisconnected
As we can see the Inet control works fairly logically, first resolving the host name from the URL given, then getting a response and then disconnecting. If we provide an incorrect url, as in "http://" then we need to use standard error handling to pick up the error:
Private Function GetPage(strURL As String) As String On Error Goto vbErrHand ' Code here Exit Function vbErrHand: Msgbox Err.Description End Function
Page 3 of 4
This article was originally published on November 20, 2002