Results 1 to 9 of 9
  1. #1
    Sonu is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Aug 2019
    Posts
    35

    Form Button to browse for a default folder location


    I have a Form that has a button named 'Hyperlink'. After clicking on this, I want it to browse to a certain folder location for file selection (a default path to browse). After selection, the complete file path will be feed into the text box (left side of the button in picture below). Below is the code that is not doing the need:


    Private Sub cmdHyperlink_Click()
    'Open hyperlink dialog box


    On Error GoTo Err_cmdHyperlink_Click


    'Set focus in txtDocLocation
    Me.txtDocLocation.SetFocus
    DoCmd.RunCommand acCmdInsertHyperlink

    'Update date hyperlink was created
    Me!txtLinkDate = Date

    Exit_cmdHyperlink_Click:
    Exit Sub


    Err_cmdHyperlink_Click:
    If Err.Number <> 2501 And Err.Number <> 13 Then
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, _
    "sfrmCaseDocs cmdHyperlink_Click"
    End If
    Resume Exit_cmdHyperlink_Click


    End Sub


    Any lead in this will be very helpful.


    Thank you.
    Attached Thumbnails Attached Thumbnails MS Access 2016 - Form Button for Hyperlink.png  

  2. #2
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Had to read your post a few times and compare it to the code. Decided that it seems you need to use the msoFileDialogFilePicker .
    One can use a variation of that as well - Application.FileDialog
    You navigate and choose a file, the dialog returns the file name to your code. The path is also a property of the dialog IIRC, so you append the name to the path if need be.
    The more we hear silence, the more we begin to think about our value in this universe.
    Paraphrase of Professor Brian Cox.

  3. #3
    Sonu is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Aug 2019
    Posts
    35
    Hello,

    I was able to fix the need by putting the below code. However, it leaves with with another issue. With the previous code, after browsing to a certain folder/file, the complete path used to reflect in the text box (as per image given in my first post) and the file used to open if clicked.

    But with this new code, though the text box reflects the complete file path but the file does not open when clicked. Now sure what change is needed to fix this?

    Latest Code:-

    Private Sub cmdHyperlink_Click()
    'Open hyperlink dialog box


    'Declare a variable as a FileDialog object
    Dim fd As FileDialog

    'Create a FileDialog object as a File Picker dialog box
    Set fd = Application.FileDialog(msoFileDialogFilePicker)


    'Declare a variable to contain the path
    Dim sFolder As Variant


    On Error GoTo Err_cmdHyperlink_Click

    'Use a With...End With block to reference the FileDialog object
    With fd
    .InitialFileName = "<Desired Path Here>"
    If .Show = -1 Then ' if OK is pressed
    sFolder = .SelectedItems(1)
    End If
    End With

    If sFolder <> "" Then ' if a file was chosen
    Me.txtDocLocation = sFolder
    End If

    Set fd = Nothing

    Exit_cmdHyperlink_Click:
    Exit Sub


    Err_cmdHyperlink_Click:
    If Err.Number <> 2501 And Err.Number <> 13 Then
    MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, _
    "sfrmCaseDocs cmdHyperlink_Click"
    End If
    Resume Exit_cmdHyperlink_Click


    End Sub

    Thank You.

  4. #4
    Micron is offline Very Inert Person
    Windows 10 Access 2016
    Join Date
    Jun 2014
    Location
    Ontario, Canada
    Posts
    13,423
    Well, I confused. At first you said
    Below is the code that is not doing the need:
    Now you write
    With the previous code, after browsing to a certain folder/file, the complete path used to reflect in the text box (as per image given in my first post) and the file used to open if clicked.
    Either it worked or it didn't??

    In your new example you're not invoking the command to insert hyperlink (which I thought was for OLE type fields but that doesn't seem to be the case here?). Try that.
    I'm not too familiar with that command and don't have time to look at it until (maybe) tonight.

    P.S. please post code within code tags (# on forum menu bar) to make it easier to read.

  5. #5
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    Sonu,

    More info and example of FileDialog. and this file/folder dialog video https://www.youtube.com/watch?v=rNHLea359p4
    Do you really need the hyperlink?? Perhaps you could describe more of WHAT you are trying to accomplish rather than HOW you have approached it.
    Good luck with your project.
    Last edited by orange; 09-16-2019 at 08:08 AM.

  6. #6
    Sonu is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Aug 2019
    Posts
    35
    Thank you for response Micron and sorry for the confusion. As per my initial post, I was able to invoke hyperlink but not to the desired folder. In my second post, I am able to browse to a default folder but not at hyperlink. So, I think, I would need to mix both of these approaches, so that the button takes me to a default folder as insert hyperlink. I will try that and let know how does it go..

    Regards,
    Sonu

  7. #7
    orange's Avatar
    orange is online now Moderator
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    cross posted https://access-programmers.co.uk/for...d.php?t=306834

    Sonu,
    When you post the same or similar post on multiple sites, you should include the link to the other post(s).
    Here is why.

  8. #8
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,771
    acCmdInsertHyperlink does not allow to set a default start folder.

    A hyperlink is composed of 3 parts separated by # character
    http://allenbrowne.com/casu-09.html. So either concatenate the path with that character ("#" & sFolder & "#") or use intrinsic FollowHyperlink method to force the path to be treated as a hyperlink. Review http://allenbrowne.com/func-GoHyperlink.html

    For future, please post lengthy code within CODE tags to retain indentation and readability.
    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.

  9. #9
    Sonu is offline Advanced Beginner
    Windows 7 64bit Access 2016
    Join Date
    Aug 2019
    Posts
    35
    Thank you for sharing, I will take a note of this going forward.

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

Similar Threads

  1. Replies: 6
    Last Post: 06-06-2019, 02:29 PM
  2. Command Button - send a file to a network location folder
    By 9944pdx in forum Database Design
    Replies: 4
    Last Post: 02-12-2018, 10:53 PM
  3. Browse and Open Folder Based on Matching Form Field
    By Tomfernandez1 in forum Access
    Replies: 11
    Last Post: 02-26-2013, 01:04 PM
  4. Replies: 4
    Last Post: 09-18-2012, 11:30 PM
  5. Browse For Folder Example
    By pkstormy in forum Code Repository
    Replies: 6
    Last Post: 01-08-2012, 04:13 PM

Tags for this Thread

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