Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Error handling for Runtime Error '50003'

Status
Not open for further replies.

progolf069

Programmer
Jun 13, 2001
37
US
Can anybody help me out with this runtime error of: Runtime Error '50003' Unexpected Error. I know it relates to a file not being found over the internet. The purpose of the program that I have is this: I have a simple application that pulls an image off of the internet, and places the image in a picture box. Well, if the image is not avalible to be placed in the picture box (because it is not avalible online -- either not on the server, or the server is down/not avalible) you will receive this error. Does anybody want to try giving me ideas on how to set up an error handler, that might display a message box that mentions something about "server is unavalible right now, try later" If you have any ideas, please post them! Here is the code that I have so far For this application:

Code:
Option Explicit

Private Declare Function GetTempPath Lib "kernel32" _
         Alias "GetTempPathA" (ByVal nBufferLength As Long, _
         ByVal lpBuffer As String) As Long
         
Private Declare Function GetTempFileName Lib "kernel32" _
         Alias "GetTempFileNameA" (ByVal lpszPath As String, _
         ByVal lpPrefixString As String, ByVal wUnique As Long, _
         ByVal lpTempFileName As String) As Long
         
         
         
Private Function GetPicFromHTTP(strURL As String) As Variant
    Dim strTempPath As String * 512
    Dim strTempFileBuff As String * 576
    Dim strTempFile As String
    
    Dim hFile As Long
    
    Dim result As Long
    
    Dim bytearray() As Byte
    Dim strData As String
    
    ' Generate unique temporary filename
    result = GetTempPath(512, strTempPath)
    GetTempFileName strTempPath, "VBT", 0, strTempFileBuff
    strTempFile = Left$(strTempFileBuff, InStr(strTempFileBuff, vbNullChar) - 1) + ".jpg"
    
    bytearray() = Inet1.OpenURL(strURL, icByteArray)
    'strData = Inet1.OpenURL("[URL unfurl="true"]http://www.wandtv.com/rdrimg.jpg";,[/URL] icString)
    hFile = FreeFile
    
    Open strTempFile For Binary As hFile
    Put hFile, , bytearray
    Close hFile
    
    Set GetPicFromHTTP = LoadPicture(strTempFile)
    Kill strTempFile
End Function
         
Private Sub cmdExit_Click()
    End
    
End Sub

Private Sub cmdReload_Click()
    picRadar.Visible = False
    picRadar.Picture = GetPicFromHTTP("[URL unfurl="true"]http://www.wandtv.com/rdrimg.jpg")[/URL]
    picRadar.Visible = True
    
End Sub

Private Sub Form_Load()
    picRadar.Picture = GetPicFromHTTP("[URL unfurl="true"]http://www.wandtv.com/rdrimg.jpg")[/URL]
        
End Sub
 
Why not at the start of the sub...

On Error GoTo ImageError

Than at the bottom after Exit Sub...

Exit Sub

ImageError:

If err = "50003" Then
MsgBox "There was no image to download"
Else
etc etc
End If

End Sub
 
The error message is caused by the fact that the temporary file that we are attempting to load as a picture isn't actually a picture if the website has been off-line/unavailable. You probably also want to deal ith the fact that the image may be inaccessible for some other reason.

The technique I demo below requires that your form has an image control on it containing a picture that will represent an unavailable image. The image control's .visible property should be set to FALSE.

The GetPicFromHTTP function can then be slightly rewritten as follows:

Private Function GetPicFromhttp(strURL As String) As Variant
Dim strTempPath As String * 512
Dim strTempFileBuff As String * 576
Dim strTempFile As String

Dim hFile As Long

Dim result As Long

Dim bytearray() As Byte
Dim strData As String

' Generate unique temporary filename
result = GetTempPath(512, strTempPath)
GetTempFileName strTempPath, "VBT", 0, strTempFileBuff
strTempFile = Left$(strTempFileBuff, InStr(strTempFileBuff, vbNullChar) - 1) + ".jpg"

On Error Resume Next
strData = Inet1.OpenURL(strURL, icString)
On Error GoTo 0
bytearray() = Inet1.OpenURL(strURL, icByteArray)
hFile = FreeFile

If UBound(bytearray) > 0 And strData = "" Then
Open strTempFile For Binary As hFile
Put hFile, , bytearray
Close hFile


Set GetPicFromhttp = LoadPicture(strTempFile)
Kill strTempFile
Else
Set GetPicFromhttp = Image1.Picture
End If

End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top