Form Button to browse for a default folder location (1 Viewer)

sonu_vyas

New member
Local time
Today, 19:04
Joined
Sep 14, 2019
Messages
1
I have a Form that has a button named 'Hyperlink'. After clicking on this, I wanted 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). I used the below code to do that and it worked for me, but I have an issue here. Once I select the file, the complete file path reflects in the text box as a hyperlink but once I click it, I am expecting the file should open but it is not.

I tried using a different code to do that, with the new code, the file open after clicking on it from the text box but it does not go a default folder. I had to browse to the desired path every time.

Both the approaches have some or other issue. Can someone help?

Code 1 (Opens the default folder but the text box link does not work):-

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

Code 2 (Does not open a default folder by the text box link opens the file):-

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

Thank You,
Sonu
 

Attachments

  • MS Access 2016 - Form Button for Hyperlink.png
    MS Access 2016 - Form Button for Hyperlink.png
    3.8 KB · Views: 26

June7

AWF VIP
Local time
Today, 04:34
Joined
Mar 9, 2014
Messages
3,386
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.
 
Last edited:

arnelgp

error reading drive A:
Local time
Today, 21:34
Joined
May 7, 2009
Messages
11,514
Code:
Private Sub cmdHyperlink_Click()
'Open hyperlink dialog box

On Error GoTo Err_cmdHyperlink_Click

    'Set focus in txtDocLocation
    Me.txtDocLocation.SetFocus
    'DoCmd.RunCommand acCmdInsertHyperlink
    [COLOR="Blue"]Application.FollowHyperlink (Me.txtDocLocation)[/COLOR]
    '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
 

Users who are viewing this thread

Top Bottom