Solved

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

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

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 49

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

In the previous article, Using a Critera Form to Filter Records (http://www.experts-exchange.com/A_6069.html), the form was basically a data container storing user input, which queries and other database objects could read. The form had to remain op…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

862 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

24 Experts available now in Live!

Get 1:1 Help Now