Solved

Loading default image if the image is not found in external folder

Posted on 2016-09-15
6
40 Views
Last Modified: 2016-09-16
I want to load a default image if the specified image is not found. Below are my code in form load event.
The image file name will be the Workorder_ID & the file extension. Images in the external folder are named that way. In the event that the images are not available, I want the controls ImgHolder1.Picture & ImgHolder2.Picture  to load the same default photo.  Help with vba code preferred. Thanks


Dim ImgPath As String
Dim ImgFile As String
Dim ImgFile1 As String
Dim ImgFile2 As String

ImgPath = "D:\Img\"
    ImgFile = Me.Workorder_ID
   
    ImgFile1 = ImgFile & "F.png"
   
    ImgFile2 = ImgFile & "B.png"
     
   
    ImgHolder1.Picture = ImgPath & ImgFile1
    ImgHolder2.Picture = ImgPath & ImgFile2
0
Comment
Question by:thao-nhi
  • 3
  • 3
6 Comments
 
LVL 50

Accepted Solution

by:
Ryan Chong earned 500 total points
ID: 41800895
you can use FSO's FileExists function to check the existence of a file:
Function isFileExist(yourFile)
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   isFileExist = fso.FileExists(yourFile)
End Function

Open in new window

so apply this into your codes..
...
ImgFileDefault = "yourDefault.png"

if  isFileExist(ImgPath & ImgFile1) then
     ImgHolder1.Picture = ImgPath & ImgFile1
else
     ImgHolder1.Picture = ImgPath & ImgFileDefault
end if
...

Open in new window

0
 

Author Comment

by:thao-nhi
ID: 41800907
I would have to place the function in a module and call the function from the sub? If so, please elaborate a bit more on where in the sub I would call the function. Thanks
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41800913
yes, you can place that function into a Module.

>>If so, please elaborate a bit more on where in the sub I would call the function.

Thought, this is clear enough... just similar to your codes, like:
Dim ImgPath As String
Dim ImgFile As String
Dim ImgFile1 As String
Dim ImgFile2 As String
Dim ImgFileDefault As String

ImgPath = "D:\Img\"
    ImgFile = Me.Workorder_ID
 
ImgFileDefault = "yourDefault.png"  
    ImgFile1 = ImgFile & "F.png" 
    ImgFile2 = ImgFile & "B.png"
     
   if  isFileExist(ImgPath & ImgFile1) then
     ImgHolder1.Picture = ImgPath & ImgFile1
else
     ImgHolder1.Picture = ImgPath & ImgFileDefault
end if

if  isFileExist(ImgPath & ImgFile2) then
     ImgHolder2.Picture = ImgPath & ImgFile2
else
     ImgHolder2.Picture = ImgPath & ImgFileDefault
end if

Open in new window

0
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 

Author Comment

by:thao-nhi
ID: 41800933
I did not know about the FSO before. I have 2 files to check for ImgFile1 and ImgFile2. What do I put in the function? Thanks again

Function isFileExist(yourFile)
   Dim fso
   Set fso = CreateObject("Scripting.FileSystemObject")
   isFileExist = fso.FileExists(yourFile)
End Function
0
 
LVL 50

Expert Comment

by:Ryan Chong
ID: 41800983
hi, as per the example, you need to pass in the full file path to the function, like:

if  isFileExist(ImgPath & ImgFile2) then

it will return True if the file path is valid (file exists)
or it will return False if the file path is invalid (file does not exist)
0
 

Author Closing Comment

by:thao-nhi
ID: 41801974
Thanks
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
VBA script to check for duplicate primary keys on lost focus 18 30
Auto-generated ID's in MS Access 9 46
Modal form 11 30
Access summarise unique values in tables 2 15
Introduction When developing Access applications, often we need to know whether an object exists.  This article presents a quick and reliable routine to determine if an object exists without that object being opened. If you wanted to inspect/ite…
Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question