?
Solved

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

Posted on 2016-09-15
6
Medium Priority
?
61 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 53

Accepted Solution

by:
Ryan Chong earned 2000 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 53

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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 

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 53

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

In earlier versions of Windows (XP and before), you could drag a database to the taskbar, where it would appear as a taskbar icon to open that database.  This article shows how to recreate this functionality in Windows 7 through 10.
If you need a simple but flexible process for maintaining an audit trail of who created, edited, or deleted data from a table, or multiple tables, and you can do all of your work from within a form, this simple Audit Log will work for you.
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

719 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