We help IT Professionals succeed at work.

Access VBA change an image on a different form

Murray Brown
Murray Brown asked
on
Hi. I have two forms. The first loads an image in the following event. The second has a button where the image can be edited. My problem
is that when I do this and go back to the first form the old image is there. I have tried to reload the image and refresh the form but it doesn't work


Private Sub Form_Current()
   Dim oDB_Folder As String: oDB_Folder = CurrentProject.Path
    Dim oSavedPath As String: oSavedPath = [PhotoFile]
    Dim oActualPath, F As String
    F = Replace(oSavedPath, "DatabaseFolder", oDB_Folder)
   Me.imgPhoto.Picture = F
End Sub

Open in new window


Private Sub btnSelectPhoto_Click()

    Dim strFile As String
    Dim PhotoFile As String
    strFile = GetFileName(CurrentProject.Path)
    If strFile > "" Then
        PhotoFile = Filenm(strFile)
        If Filepath(strFile) <> CurrentProject.Path & "\Media\ID Photos\" Then
            FileCopy strFile, CurrentProject.Path & "\Media\ID Photos\" & PhotoFile
        End If
        imgPhoto.Picture = CurrentProject.Path & "\Media\ID Photos\" & PhotoFile
        Me.PhotoFile = "DatabaseFolder" & "\Media\ID Photos\" & PhotoFile
        Form_f_LicenseeDetailsEdit.PhotoFile = "DatabaseFolder" & "\Media\ID Photos\" & PhotoFile
        Form_f_LicenseeDetailsEdit.Refresh
        Form_f_LicenseeDetailsEdit.Repaint
    End If
    
End Sub

Open in new window

Comment
Watch Question

John TsioumprisSoftware & Systems Engineer
Distinguished Expert 2019

Commented:
Probably something is missing
I think this
imgPhoto.Picture = CurrentProject.Path & "\Media\ID Photos\" & PhotoFile

Open in new window

should be
Form_Name_Of_Form_That_Holds_ThePhoto.imgPhoto.Picture = CurrentProject.Path & "\Media\ID Photos\" & PhotoFile

Open in new window

e.g.
Form_MyForm.imgPhoto.Picture = CurrentProject.Path & "\Media\ID Photos\" & PhotoFile

Open in new window

Of course you can take a look at my Article and go the way of the BLOB : https://www.experts-exchange.com/articles/33716/Defeating-the-device-independent-bitmap-dib-format.html
Senior Developer
Commented:
Clean code is our friend.

Use the event procedures to call your methods. Thus create a load image method with appropriate error handling and logging. E.g.

Private Sub Form_Current()

  On Local Error Resume Next

  Dim ImagePath As String  
  
  ImagePath = Replace(Me![PhotoFile], "DatabaseFolder", CurrentProject.Path )   
  lblImage.Caption = DisplayImage(imgPhoto, ImagePath) 
  
End Sub

Private Function DisplayImage(ByVal CImage As Access.Image, ByVal CImagePath As Variant) As String

  On Local Error GoTo LocalError

  Dim Result As String
  
  If IsNull(CImagePath) Then
    CImage.Visible = False
    Result = "No image path specified."
  Else
    CImage.Visible = True
    CImage.Picture = CImagePath
    Result = "" 
  End If
    
  DisplayImage = Result
  Exit Function

LocalError:
  CImage.Visible = False
  Select Case Err.Number
  Case 2220
    DisplayImage = "Image not found."
  Case Else
    MsgBox Err.Number & " " & Err.Description
    DisplayImage = "Error while displaying image."
  End Select
    
End Function

Open in new window

Where lblImage is a label in the same area of the image control to show possible error messages.

Now call form requery after changing the path:

Private Sub btnSelectPhoto_Click()

  Dim FilePath As String
  Dim PhotoFile As String

  FilePath = GetFileName(CurrentProject.Path)
  PhotoFile = Filenm(FilePath)
  If Filepath(FilePath) <> CurrentProject.Path & "\Media\ID Photos\" Then
    FileCopy FilePath, CurrentProject.Path & "\Media\ID Photos\" & PhotoFile
  End If

  imgPhoto.Picture = CurrentProject.Path & "\Media\ID Photos\" & PhotoFile
  Me![PhotoFile] = "DatabaseFolder" & "\Media\ID Photos\" & PhotoFile
  Form_f_LicenseeDetailsEdit![PhotoFile] = "DatabaseFolder" & "\Media\ID Photos\" & PhotoFile
  Form_f_LicenseeDetailsEdit.Requery

End Sub

Open in new window

But the question is do you use the correct variables? E.g. line 8 in what case should this condition be False?

p.s. I always use ![fieldName] when referencing fields and I only use square brackets for them. Thus makes code reading simpler.
Murray BrownASP.net/VBA/VSTO Developer

Author

Commented:
Thanks for the help