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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding and displaying images on a form, image not found problem

Status
Not open for further replies.

dedo8816

Programmer
Oct 25, 2006
94
0
0
GB
Hi, i have a program that a user enters a serial number to search, the program then displays all the information about that item. Yesterday i added a function that the program now shows a picture of the item being searched, problem is, if the serial number is not on the system and the picture does not exist it throws up the debug error.
I want to know how do i code this, that if the image file does not exist, the program moves on without it?

If opin Then 'Option for search criteria on form
DataEnvironment1.rsCommand1.MoveFirst
DataEnvironment1.rsCommand1.Find ("[No] ='" & (TxtSearch.Text) & "'")
Image1.Picture = LoadPicture("\\Jupiter\Shared\Labeller\Pictures\" & (TxtSearch.Text) & ".jpg")
'the above line is where the program stops if no image exists...
If DataEnvironment1.rsCommand1.EOF Then
DataEnvironment1.rsCommand1.MoveFirst
End If
End If

TxtLotR.Text = "PR-"
txtrev.Text = lblrev1.Caption
TxtLotR.SetFocus

Thanks in advance
 
Insert the line [tt]On Error Resume Next[/tt] before LoadPicture statement. This tells VB to ignore any errors that occur during execution.

You might want to include an [tt]On Error Goto 0[/tt] statement after LoadPicture which will enable error trapping again.
 
Ah, i tried that earlier but put it after the load picture line, stupid mistake, thank you!!

Works great now

Cheers
D
 
Not sure I'd go with ignoring errors here (and I'm normally a big fan On Error Resume Next in the right context). Think I'd go with changing
Code:
[blue]Image1.Picture = LoadPicture("\\Jupiter\Shared\Labeller\Pictures\" & (TxtSearch.Text) & ".jpg")[/blue]
to
Code:
[blue]myFile=("\\Jupiter\Shared\Labeller\Pictures\" & (TxtSearch.Text) & ".jpg"
If FileExists(myFile) then Image1.Picture = LoadPicture(myFile)[/blue]
 
>Not sure I'd go with ignoring errors here

That's why I suggested 'On Error Goto 0' right after LoadPicture.

Generally, I always use some kind of error trapping with LoadPicture and similar operations involving reading/writing external files. This is because a variety of other errors can throw up besides a missing file. For example, a empty or corrupt file, an unsupported format or a file exclusively locked by some other application.

FileExists function will not prevent these problems and will still let LoadPicture throw an uptrapped error.

And also, FileExists is not a VB/VBA library function and might look like the one in thread222-490636. (I found it with search.)
 
Sorry, so used to using the the FileSystemObject in VB for a variety of the tricks it can perform that I tend top forget that FileExists in an FSO method and not a native VB method.




 
Easy to hack one up though:
Code:
Function FileExists(ByVal File As String) As Boolean
    On Error Resume Next
    GetAttr File
    FileExists = Err.Number = 0
End Function
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top