Open Photographs in an Access Form

Anonymous
2023-01-26T23:26:10+00:00

I have a datatbase of photograph file names. In the DB I have a List Form that has a "Digital File Name" field. Question: Is it possable to create a command button on my List Form that will open the photo (that is stored in my Photos File on my C: drive) based on the digital file name for the selected record?

I am using Access 2016

Thanks Walt

Microsoft 365 and Office | Access | For home | Other

Locked Question. This question was migrated from the Microsoft Support Community. You can vote on whether it's helpful, but you can't add comments or replies or follow the question.

0 comments No comments

49 answers

Sort by: Oldest
  1. Anonymous
    2023-01-27T01:19:24+00:00

    Hello

    I am Abdal and I would be glad to help you with your question.

    Yes, it is possible to create a command button on your List Form that opens the photo based on the digital file name for the selected record. You can use VBA (Visual Basic for Applications) code to accomplish this. The code would use the FileSystemObject to open the file and the value of the "Digital File Name" field to construct the file path. You can then assign the code to the On Click event of the command button. Here is an example of code that you could use to open the file:

    Private Sub cmdOpenFile_Click() Dim fso As Object Dim filePath As String

    ' Get the value of the "Digital File Name" field filePath = Me! [Digital File Name]

    ' Construct the full file path filePath = "C:\Photos" & filePath

    ' Create a FileSystemObject Set fso = CreateObject("Scripting.FileSystemObject")

    ' Open the file fso. GetFile(filePath). Open End Sub

    You would need to replace 'Digital File Name' with the name of your field and the 'C:\Photos' with the path of your photos directory.

    I hope this information helps.

    Regards,

    Abdal

    Was this answer helpful?

    0 comments No comments
  2. ScottGem 68,840 Reputation points Volunteer Moderator
    2023-01-27T14:22:19+00:00

    Sure, it real easy. I'm assuming here that the Digital File Name includes the full path and filename. So what I would do is use the Double click event of the control bound that field and use this code:

    Application.FollowHyperlink Me.txtFilename

    Where txtFilename is the name of the control bound to the field. This will open the fiel in your default photo editor.

    If you only store the filename and not the full path, you sould need to do something like:

    Application.FollowHyperlink "C:\photos" & Me.txtFilename

    Alternatively, you could simply add an unbound image control to the form and set the Controlsource to the field.

    Was this answer helpful?

    1 person found this answer helpful.
    0 comments No comments
  3. Anonymous
    2023-01-28T23:28:07+00:00

    Abdal,

    Thank you for your reply. I tried the code you suggested and it would not work for me. When I clicked my Open Image command button nothing happened. Following is the code I wrote as copied from the Code bulider page of the Open Image command button:

    Private Sub cmdOpenFile_Click()

    Dim fso As Object 
    
    Dim filePath As String 
    

    'Get the value of the "Digital File Name" field

    filePath = Me![Digital File Name] 
    

    ' Construct the full file Path

    filePath = "D:\Documents\Family Tree Maker\Wright-Jagoe(1) Media" & filePath 
    

    ' Create a FileSystemObject

    Set fso = CreateObject("Scripting.FileSystemObject') 
    

    ' Open the file

    fso.GetFile(filePath).Open 
    

    End Sub

    I must have made some mistake as I am not to far advanced in Access, although I do follow directions pretty well.

    Hope you can help - Walt

    Was this answer helpful?

    0 comments No comments
  4. ScottGem 68,840 Reputation points Volunteer Moderator
    2023-01-29T00:18:09+00:00

    Did you try the suggestion I made?

    While Abdal's solution can work, it is using a cannon to swat a fly.

    Was this answer helpful?

    0 comments No comments
  5. Anonymous
    2023-01-29T00:41:35+00:00

    Hello

    Thanks for the update.

    You may want to include error handling in case the file is not found or the path is incorrect. You can use the On Error Resume Next statement to catch any errors and then use the On Error GoTo statement to handle them.

    For example:

    Private Sub cmdOpenFile_Click() On Error Resume Next Dim fso As Object Dim filePath As String

    ' Get the value of the "Digital File Name" field filePath = Me! [Digital File Name]

    ' Construct the full file path filePath = "C:\Photos" & filePath

    ' Create a FileSystemObject Set fso = CreateObject("Scripting.FileSystemObject")

    ' Open the file fso. GetFile(filePath). Open

    ' Error handling If Err.Number <> 0 Then MsgBox "Error opening file. Please check the file name and path.", vbExclamation Exit Sub End If End Sub

    This will display a message box if an error occurs while trying to open the file, allowing the user to check the file name and path.

    It is also recommended to add a button caption to the button in the design view so the user knows what the button does.

    Lastly, you can also add a filter to the file path if the user has a different file type other than the default one. This will ensure that the file opens in the correct application.

    For example:

    filePath = "C:\Photos" & filePath & ".jpg"

    This will add .jpg to the file path, ensuring that the file opens in the correct application for jpeg files.

    While the code you gave seems to have some typo error.

    On this line:

    Set fso = CreateObject("Scripting.FileSystemObject')

    There is an extra single quote at the end of the string that should be removed. It should be:

    Set fso = CreateObject("Scripting.FileSystemObject")

    Also, please make sure that the directory "D:\Documents\Family Tree Maker\Wright-Jagoe(1) Media" exists and that the file name you have in the "Digital File Name" field matches the file name in the directory.

    Another thing that you can do is to add a MsgBox command before the fso. GetFile(filePath). Open command, so that you can check the value of the filePath variable. This will help you to understand if the path is correct or not.

    For example:

    MsgBox filePath

    This will display the filePath value in a message box, so you can see if it is what you expect.

    Finally, you can also include error handling as I mentioned before, so that if the file is not found or the path is incorrect, a message box will be displayed, allowing the user to check the file name and path.

    I hope this information helps.

    Regards,

    Abdal

    Was this answer helpful?

    0 comments No comments