Link to home
Start Free TrialLog in
Avatar of Dustin Stanley
Dustin Stanley

asked on

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

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

ASKER CERTIFIED SOLUTION
Avatar of crystal (strive4peace) - Microsoft MVP, Access
crystal (strive4peace) - Microsoft MVP, Access

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dustin Stanley
Dustin Stanley

ASKER

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

Thanks!
you're welcome, Dustin ~ happy to help
... 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 ;)
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.
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!