Solved

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

Posted on 2016-09-15
6
50 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 51

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 51

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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 

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 51

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Phishing attempts can come in all forms, shapes and sizes. No matter how familiar you think you are with them, always remember to take extra precaution when opening an email with attachments or links.
Access custom database properties are useful for storing miscellaneous bits of information in a format that persists through database closing and reopening.  This article shows how to create and use them.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

685 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