Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

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
?
92 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 22

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 Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

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

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

688 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