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
20 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 18

Accepted Solution

by:
crystal (strive4peace) - Microsoft MVP, Access earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks!
0
 
LVL 18
Comment Utility
you're welcome, Dustin ~ happy to help
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 18
Comment Utility
... 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
Comment Utility
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 18
Comment Utility
:)
0
 

Author Comment

by:Dustin Stanley
Comment Utility
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 18
Comment Utility
thanks for sharing, Dustin ~
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

Experts-Exchange is a great place to come for help with solutions for your database issues, and many problems are resolved within minutes of being posted.  Others take a little more time and effort and often providing a sample database is very helpf…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

771 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

8 Experts available now in Live!

Get 1:1 Help Now