Solved

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

Posted on 2016-09-15
6
28 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 49

Accepted Solution

by:
Ryan Chong earned 500 total points
Comment Utility
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
Comment Utility
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 49

Expert Comment

by:Ryan Chong
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:thao-nhi
Comment Utility
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 49

Expert Comment

by:Ryan Chong
Comment Utility
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
Comment Utility
Thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Introduction The Visual Basic for Applications (VBA) language is at the heart of every application that you write. It is your key to taking Access beyond the world of wizards into a world where anything is possible. This article introduces you to…
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…
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…

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now