Go Premium for a chance to win a PS4. Enter to Win

x
?
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
Medium Priority
?
109 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 23

Accepted Solution

by:
crystal (strive4peace) - Microsoft MVP, Access earned 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 23
ID: 41898435
you're welcome, Dustin ~ happy to help
0
 
LVL 23
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 23
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 23
ID: 41899821
thanks for sharing, Dustin ~
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Traditionally, the method to display pictures in Access forms and reports is to first download them from URLs to a folder, record the path in a table and then let the form or report pull the pictures from that folder. But why not let Windows retr…
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, 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…

877 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