none
Store path to file location RRS feed

  • Question

  • Hi

    Rather than attach files to the database, I would rather just store the path to file location.  I've done plenty of research on the internet and tried various methods but just cannot get it to work.  So many methods seem way too complicated for what I want and maybe it's because I'm just using the wrong search terms.

    So just a simple FileOpen button, choose the location and store the path in the field as a hyperlink.  It can't be that difficult but I seem to be making it that difficult.

    Any guidance would be much appreciated.  Thanks.


    Chris | UK

    Friday, June 24, 2016 12:03 PM

Answers

  • I just want to select the file and store the path in the relevant field.
    Take a look at the Images_Simple.accdb file in Images.zip in my public databases folder at:

    https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169

    Note that if you are using an earlier version of Access you might find that the colour of some form objects such as buttons shows incorrectly and you will need to  amend the form design accordingly.  

    If you have difficulty opening the link copy its text (NB, not the link location) and paste it into your browser's address bar.

    In this little demo file the path to an image file is assigned to a text column in the table with:

    Private Sub cmdAddImage_Click()

        On Error GoTo Err_Handler
        
        Dim strPath As String
        
        ' open 'file open' dialogue and get path to selected file
        strPath = GetFilePath()
        
        If Len(strPath) > 0 Then
            Me.ImagePath = strPath
            Me.ctrlImage.Visible = True
            Me.lblNoImage.Visible = False
        End If
        
    Exit_here:
        Exit Sub
        
    Err_Handler:
        MsgBox Err.Description, vbExclamation, "Error"
        Resume Exit_here
        
    End Sub

    Which calls:

    Function GetFilePath() As String

        With Application.FileDialog(1)
            .Title = "Select File"
            If .Show Then
                GetFilePath = .SelectedItems(1)
            End If
        End With
        
    End Function


    Ken Sheridan, Stafford, England

    Sunday, June 26, 2016 11:17 AM
  • What I would now like to do is make the field clickable to open the document.
    See BrowseDemo.zip in my same OneDrive folder.  The methods illustrated allow you to open a file either by clicking on the control as a pseudo-hyperlink or via a separate button.  Another button allows you to print the file.  In my demo the form is unbound, but that's not relevant.

    Ken Sheridan, Stafford, England

    • Marked as answer by ChrisParkin Monday, June 27, 2016 11:47 AM
    Monday, June 27, 2016 10:36 AM

All replies

  • Maybe OpenFileDialog can serve your purposes.

    https://msdn.microsoft.com/en-us/library/system.windows.forms.openfiledialog.openfile.aspx

    Friday, June 24, 2016 12:59 PM
    Moderator
  • Thanks for the reply.  That command seems to refer to actually opening the file which is not what I am looking for.  I just want to select the file and store the path in the relevant field.

    Chris | UK

    Friday, June 24, 2016 1:10 PM
  • You may try it.  :)

            private void button1_Click(object sender, EventArgs e)
            {
                OpenFileDialog OpenFileDialog1 = new OpenFileDialog();
                OpenFileDialog1.Filter = "All Files|*.*";
                if (OpenFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    textBox1.Text=OpenFileDialog1.FileName;
                }
            }

    Friday, June 24, 2016 1:35 PM
    Moderator
  • There are different ways to go about it, but FileDialog is the simplest.

    https://support.microsoft.com/en-us/kb/824272

    http://stackoverflow.com/questions/14915179/ms-access-browse-for-file-and-get-file-name-and-path

    https://msdn.microsoft.com/en-us/library/office/aa219843%28v=office.11%29.aspx?f=255&MSPPError=-2147217396

    You use the file picker to allow the user to select the file, then capture the path and filename and store it in your field/table.


    Daniel Pineault, 2010-2012 Microsoft MVP
    http://www.cardaconsultants.com
    MS Access Tips and Code Samples: http://www.devhut.net

    Saturday, June 25, 2016 12:12 AM
  • So just a simple FileOpen button, choose the location and store the path in the field as a hyperlink.  It can't be that difficult but I seem to be making it that difficult.

    Hi Chris,

    Did you try to store the path just as text, and not as hyperlink? That is the way I Always work, without any problems.

    Imb.

    Saturday, June 25, 2016 7:24 PM
  • I just want to select the file and store the path in the relevant field.
    Take a look at the Images_Simple.accdb file in Images.zip in my public databases folder at:

    https://onedrive.live.com/?cid=44CC60D7FEA42912&id=44CC60D7FEA42912!169

    Note that if you are using an earlier version of Access you might find that the colour of some form objects such as buttons shows incorrectly and you will need to  amend the form design accordingly.  

    If you have difficulty opening the link copy its text (NB, not the link location) and paste it into your browser's address bar.

    In this little demo file the path to an image file is assigned to a text column in the table with:

    Private Sub cmdAddImage_Click()

        On Error GoTo Err_Handler
        
        Dim strPath As String
        
        ' open 'file open' dialogue and get path to selected file
        strPath = GetFilePath()
        
        If Len(strPath) > 0 Then
            Me.ImagePath = strPath
            Me.ctrlImage.Visible = True
            Me.lblNoImage.Visible = False
        End If
        
    Exit_here:
        Exit Sub
        
    Err_Handler:
        MsgBox Err.Description, vbExclamation, "Error"
        Resume Exit_here
        
    End Sub

    Which calls:

    Function GetFilePath() As String

        With Application.FileDialog(1)
            .Title = "Select File"
            If .Show Then
                GetFilePath = .SelectedItems(1)
            End If
        End With
        
    End Function


    Ken Sheridan, Stafford, England

    Sunday, June 26, 2016 11:17 AM
  • Thanks all for your responses.

    Ken, as you provided an example it helped me to understand better so thank you for that.

    What I would now like to do is make the field clickable to open the document.  I'm not sure what search terms to use for this to get the help I need - could you point me in the right direction please.

    Many thanks.


    Chris | UK

    Monday, June 27, 2016 8:52 AM
  • What I would now like to do is make the field clickable to open the document.
    See BrowseDemo.zip in my same OneDrive folder.  The methods illustrated allow you to open a file either by clicking on the control as a pseudo-hyperlink or via a separate button.  Another button allows you to print the file.  In my demo the form is unbound, but that's not relevant.

    Ken Sheridan, Stafford, England

    • Marked as answer by ChrisParkin Monday, June 27, 2016 11:47 AM
    Monday, June 27, 2016 10:36 AM
  • Brilliant Ken, thanks very much.

    I know you may have helped me a little too much by giving me the solution but it really does help me to understand what is going on when I can see a working example.

    Much appreciated as always.


    Chris | UK

    Monday, June 27, 2016 11:47 AM