Results 1 to 5 of 5
  1. #1
    James_S is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2022
    Posts
    2

    VBA to refresh an imageframe that gets picture from image path field


    Hi, i have a database for recording animal details for a shelter. The main form has the details of the dogs and an imageframe to display the picture. There is a button to add an image which enables you to browse to a folder on the computer to attach the image. The code is below. I had an issue as i couldn't get the form to refresh the picture once it was selected and stay on the same record. So i added the code highlighted in green to remember the record ID number and find it after refreshing the form data. This works fine, but the problem is once used the usual Ctrl+F used to find data in other records doesn't work and seems to do nothing. Can anyone help with correcting the code, or improve the below so that once an image is selected the record refreshes and stays on the current record.
    Many thanks


    Private Sub cmdAddImage_Click()
    Me.Imageframe.Visible = True
    On Error GoTo cmdAddImage_Err
    Dim strFilter As String
    Dim lngflags As Long
    Dim varFileName As Variant


    strFilter = "All Files (*.*)" & vbNullChar & "*.*" _
    & vbNullChar & "All Files (*.*)" & vbNullChar & "*.*"


    lngflags = tscFNPathMustExist Or tscFNFileMustExist _
    Or tscFNHideReadOnly


    varFileName = tsGetFileFromUser( _
    fOpenFile:=True, _
    strFilter:=strFilter, _
    rlngflags:=lngflags, _
    strDialogTitle:="Please choose a file...")


    If IsNull(varFileName) Then
    Else
    Me![imagepath] = varFileName
    Me.Recalc
    Dim strBookmark As String
    strBookmark = Me.ID
    Me.Requery
    DoCmd.FindRecord strBookmark, , True, , True
    End If


    cmdAddImage_End:
    On Error GoTo 0
    Exit Sub


    cmdAddImage_Err:
    Beep
    MsgBox Err.Description, , "Error: " & Err.Number _
    & " in file"
    Resume cmdAddImage_End


    If Me.imagepath Is Null Then
    Me.Imageframe.Visible = False
    End If
    If Not IsNull(Me.imagepath) Then
    Me.Imageframe.Visible = True
    Me.Imageframe.Requery
    End If


    End Sub

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Try:

    Me.Recordset.Requery
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    There are a couple of issues with your code.
    (see bottom of the code)
    Code:
    Private Sub cmdAddImage_Click()
       Me.Imageframe.Visible = True
       On Error GoTo cmdAddImage_Err
       Dim strFilter As String
       Dim lngflags As Long
       Dim varFileName As Variant
    
       strFilter = "All Files (*.*)" & vbNullChar & "*.*" _
          & vbNullChar & "All Files (*.*)" & vbNullChar & "*.*"
    
       lngflags = tscFNPathMustExist Or tscFNFileMustExist _
          Or tscFNHideReadOnly
    
       varFileName = tsGetFileFromUser( _
          fOpenFile:=True, _
          strFilter:=strFilter, _
          rlngflags:=lngflags, _
         strDialogTitle:="Please choose a file...")
    
       If IsNull(varFileName) Then
       Else
          Me![imagepath] = varFileName
          Me.Recalc
          Dim strBookmark As String
          strBookmark = Me.ID
          Me.Requery
          DoCmd.FindRecord strBookmark, , True, , True
       End If
    
    cmdAddImage_End:
    On Error GoTo 0
    Exit Sub
    
    cmdAddImage_Err:
       Beep
       MsgBox Err.Description, , "Error: " & Err.Number _
          & " in file"
       Resume cmdAddImage_End      '<<-- jump to cmdAddImage_End:
    
    
    
    -----------------------------------------------------------------
    '------ The code below here will NEVER be executed --------------
    '------ because it is in the error handler code "cmdAddImage_Err:"
    
       If Me.imagepath Is Null Then         '<<-- should be IsNull(Me.imagepath)
          Me.Imageframe.Visible = False
       End If
       If Not IsNull(Me.imagepath) Then
          Me.Imageframe.Visible = True
          Me.Imageframe.Requery
       End If
    
    End Sub

    I kludged together a form and image control and modified your code (I don't have "tsGetFileFromUser" or the flags).
    But every time I changed the image manually, the image control updated immediately. (But I don't know what control you are using for image display)

    Research: Me.Refresh and Me.Repaint

    I suggest using Me.Refresh instead of Me.Requery.



    Maybe you could/would make a copy of the dB with just a few records (change any sensitive data) for analysis??

  4. #4
    James_S is offline Novice
    Windows 10 Access 2010 32bit
    Join Date
    Jun 2022
    Posts
    2
    Hi Ssanfu, thank you for getting back to me, i have attached a zipped copy of the basic form with a little data in it for you to check out. What it doesnt do is refresh the image after its selected
    Attached Files Attached Files

  5. #5
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,815
    Set a breakpoint in the imagepath_AfterUpdate() procedure - it does not trigger with the field update therefore the Picture property is not modified.

    Have to move the line into cmdAddImage_Click() procedure.

    Me.imagepath = varFileName
    Me.Imageframe.Picture = Me![imagepath]

    However, then every record shows the same image. And when form closes and reopens, back to original photos. Need code in Current event to dynamically change the Picture property when navigating records.

    I have never used Picture property for dynamic display of images. I use ControlSource. Picture property and VBA was necessary before ControlSource property was added to Image control with Access 2007. I have never had difficulties with using ControlSource property. No VBA code is needed to display image in Image control. Just code to select file and save path. Simply bind ControlSource property to field with path.

    Might find this discussion of interest https://www.accessforums.net/showthread.php?t=73766
    Last edited by June7; 06-27-2022 at 07:49 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 05-27-2019, 11:51 PM
  2. Replies: 6
    Last Post: 03-22-2018, 12:02 PM
  3. browsing too and for path to picture
    By dyhunter in forum Forms
    Replies: 1
    Last Post: 02-13-2018, 01:15 PM
  4. Replies: 10
    Last Post: 01-09-2014, 03:00 AM
  5. path for the picture
    By white_flag in forum Access
    Replies: 7
    Last Post: 09-20-2011, 05:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums