Solved

MS Access Image Control On Form File Path IF File Not Found in File Path Then Look In Different File Path

Posted on 2016-11-22
9
33 Views
Last Modified: 2016-11-23
On my form I have a image control and it is bound to a default image path of where the databse backend is and the Image file name in a folder called images. I use the file dialog to collect the file name for the image I am needing.

When I take pictures they go to a folder called ImagesTemp. This is a temporary holding folder as I have to rename the image file before putting it in the main folder called Images by the BackEnd. After taking the image I open the file dialog and while in there I just change the name quickly and then select the file for the file name to be put into my form for the database.

So while the image is in the ImagesTemp Folder and has not been sent over to the main folder yet my image control is blank. This is very confusing for the user as they do not rember if they put an image in yet or not. I could put up some checks to make sure they put images in but that is not what i would like to do. Sometimes it could take hours to a day or so to transfer the images so if the user wants to check back on an image to compare products the image is still blank.....


SO.... How can I get the database image control to source a certain path to ImagesTemp Folder and if the image file is not found there then source the main folder OR Vice Versa which I truly think I need. Thanks.

Private Sub Form_Current()
     On Error GoTo ErrProc
    Dim ImagePath As String
    
  
    ImagePath = GetProductImageFilePath & Forms!frmSkusEntry!sbfrmProductImages.Form!ProductImageFileNm
    
    If Len(Forms!frmSkusEntry!sbfrmProductImages.Form!ProductImageFileNm) > 0 And Len(Dir(ImagePath)) > 0 Then
        Forms!frmSkusEntry!Image126.Picture = ImagePath
        Forms!frmSkusEntry!Text160 = ImagePath
        Forms!frmImageViewerPopUp.Image0.Requery
    Else
        Forms!frmSkusEntry!Image126.Picture = "\\1-PC\OneDrive\Database\Images\Images Available Upon Request.jpg"
        Forms!frmSkusEntry!Text160 = ""
    End If
    
ExitProc:
    Exit Sub
ErrProc:
    Select Case Err.Number
        Case Else
            Resume ExitProc
    End Select

Public Function GetProductImageFilePath() As String
    GetProductImageFilePath = GetDBPath & "images\"
End Function


    Public Function GetDBPath() As String
    GetDBPath = Replace(CurrentDb.TableDefs("Assemblies").Connect, ";DATABASE=", "")
'Remove db name
GetDBPath = Left(GetDBPath, InStrRev(GetDBPath, "\"))

End Function
    
End Sub

Open in new window

0
Comment
Question by:Dustin Stanley
  • 5
  • 4
9 Comments
 
LVL 19

Accepted Solution

by:
crystal (strive4peace) - Microsoft MVP, Access earned 500 total points
ID: 41898419
It is best to create a function to return the image path and only save the actual filename in the table. A function can be used to return the path\file to render the image on your forms so if the path is not found, an alternate path(s) can be used.
0
 

Author Comment

by:Dustin Stanley
ID: 41898424
I got it! I do store just the file name.  I guess I really didn't think that one over to much. Thanks Crystal!

Private Sub Form_Current()
     On Error GoTo ErrProc
    Dim ImagePath As String
    
  
    ImagePath = GetProductImageFilePath & Forms!frmSkusEntry!sbfrmProductImages.Form!ProductImageFileNm
    
    If Len(Forms!frmSkusEntry!sbfrmProductImages.Form!ProductImageFileNm) > 0 And Len(Dir(ImagePath)) > 0 Then
        Forms!frmSkusEntry!Image126.Picture = ImagePath
        Forms!frmSkusEntry!Text160 = ImagePath
        Forms!frmImageViewerPopUp.Image0.Requery
    Else
        Forms!frmSkusEntry!Image126.Picture = "C:\Users\Shipper Station\OneDrive\Images\ImagesTemp" & "\" & Forms!frmSkusEntry!sbfrmProductImages.Form!ProductImageFileNm
        Forms!frmSkusEntry!Text160 = ""
    End If
    
ExitProc:
    Exit Sub
ErrProc:
    Select Case Err.Number
        Case Else
            Resume ExitProc
    End Select
    
End Sub

Open in new window

0
 

Author Closing Comment

by:Dustin Stanley
ID: 41898425
Thanks!
0
 
LVL 19
ID: 41898435
you're welcome, Dustin ~ happy to help
0
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)

 
LVL 19
ID: 41898515
... and you really should rename the control ! perhaps imgProduct
Image126.Picture = ...

not too much code to change either -- just compile after you do it so you see what you may have not have gotten ;)
0
 

Author Comment

by:Dustin Stanley
ID: 41898517
I know I have been thinking about that but have been really busy. The code I posted earlier was not 100% correct. But I did get it and I will post it tomorrow here for future visitors.
0
 
LVL 19
ID: 41898533
:)
0
 

Author Comment

by:Dustin Stanley
ID: 41899713
Image Control "Image126" is on the main form called "frmSKUsEntry"
Then on the subform "sbfrmProductImages" Current Event

Private Sub Form_Current()
     On Error GoTo ErrProc
    Dim ImagePath As String
    
  
    ImagePath = GetProductImageFilePath & Forms!frmSkusEntry!sbfrmProductImages.Form!ProductImageFileNm
    
    If Len(Forms!frmSkusEntry!sbfrmProductImages.Form!ProductImageFileNm) > 0 Then
        GoTo ImageFileCheck
    Else
        Forms!frmSkusEntry!Image126.Picture = GetProductImageFilePath & " Logo Images Available Upon Request.jpg"
         Forms!frmSkusEntry!Text160 = ""
         Exit Sub
        
     End If
    
ImageFileCheck:
    If FileExists(ImagePath) = True Then
      Forms!frmSkusEntry!Image126.Picture = ImagePath
       Forms!frmSkusEntry!Text160 = ImagePath
      Forms!frmImageViewerPopUp.Image0.Requery
    End If
    
    If FileExists(ImagePath) = False Then
      Forms!frmSkusEntry!Text160 = "C:\Users\Bob\OneDrive\Images\ImagesTemp" & "\" & Forms!frmSkusEntry!sbfrmProductImages.Form!ProductImageFileNm
         Forms!frmSkusEntry!Image126.Picture = Forms!frmSkusEntry!Text160
      Forms!frmImageViewerPopUp.Image0.Requery
    End If
    
ExitProc:
    Exit Sub
ErrProc:
    Select Case Err.Number
        Case Else
            Resume ExitProc
    End Select
    
End Sub

Open in new window

The code above checks if the Image file name is even in the database or not. If it is not then "Image126" displays a default no image available photo. If the the file is in the database the next step is to check two different locations for the file image.

I found this other code online at http://allenbrowne.com/func-11.html and it checks for the file in the two folders:
Function FileExists(ByVal strFile As String, Optional bFindFolders As Boolean) As Boolean
    'Purpose:   Return True if the file exists, even if it is hidden.
    'Arguments: strFile: File name to look for. Current directory searched if no path included.
    '           bFindFolders. If strFile is a folder, FileExists() returns False unless this argument is True.
    'Note:      Does not look inside subdirectories for the file.
    'Author:    Allen Browne. http://allenbrowne.com June, 2006.
    Dim lngAttributes As Long

    'Include read-only files, hidden files, system files.
    lngAttributes = (vbReadOnly Or vbHidden Or vbSystem)

    If bFindFolders Then
        lngAttributes = (lngAttributes Or vbDirectory) 'Include folders as well.
    Else
        'Strip any trailing slash, so Dir does not look inside the folder.
        Do While Right$(strFile, 1) = "\"
            strFile = Left$(strFile, Len(strFile) - 1)
        Loop
    End If

    'If Dir() returns something, the file exists.
    On Error Resume Next
    FileExists = (Len(Dir(strFile, lngAttributes)) > 0)
End Function

Open in new window


This code below is how I check the Database backend Path and image folder:
Public Function GetProductImageFilePath() As String
    GetProductImageFilePath = GetDBPath & "images\"
End Function


    Public Function GetDBPath() As String
    GetDBPath = Replace(CurrentDb.TableDefs("Assemblies").Connect, ";DATABASE=", "")
'Remove db name
GetDBPath = Left(GetDBPath, InStrRev(GetDBPath, "\"))

End Function

Open in new window


I hope this helps someone later!
0
 
LVL 19
ID: 41899821
thanks for sharing, Dustin ~
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
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 …
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…

863 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

23 Experts available now in Live!

Get 1:1 Help Now