Open File Location

Gismo

Registered User.
Local time
Today, 13:35
Joined
Jun 12, 2017
Messages
1,298
Hi all,

I am using the below code to open a specific file location on the network but it opens "My Documents" on the local C:\ drive
Any suggestions please

Code:
Private Sub cmdBrowse_Click()

    On Error GoTo Err_Handler
    
    Dim SB_Attachment As String
    Dim strFolder As String
    Dim strFile As String
    Dim intPos As Integer
    
    SB_Attachment = BrowseFile
    
    If SB_Attachment <> "" Then
        ' get folder and file names
        intPos = InStrRev(SB_Attachment, "\\Sjo2054\shared\Data_Files\")
        strFolder = Left(SB_Attachment, intPos - 1)
        strFile = Mid(SB_Attachment, intPos + 1)
        
        ' populate text boxes on form
        Me.SB_Attachment = SB_Attachment
      
    End If
    
Exit_Here:
    Exit Sub
    
Err_Handler:
    MsgBox Err.Description
    Resume Exit_Here

End Sub
 
What happens when you hit Windows + R and paste in \\Sjo2054\shared\Data_Files\ and hit ok?
 
What does Browsefile do?
Please show the code.
 
What does Browsefile do?
Please show the code.
Public Function BrowseFile() As String

Dim strFile As String
With Application.FileDialog(1)
.Title = "Select File"
If .Show Then
strFile = .SelectedItems(1)
Else
MsgBox "No file selected", vbInformation
Exit Function
End If
End With

BrowseFile = strFile

End Function
 
See the link.
As you are using it within a function, pass your desired start path into that function.
 
See the link.
As you are using it within a function, pass your desired start path into that function.
I llooked at the link you send, still not sure how implement it or to adapt the code to mine
 
I llooked at the link you send, still not sure how implement it or to adapt the code to mine
Hi. Pardon me for jumping in. @Gasman must be offline. I think this is what he meant.

Code:
Public Function BrowseFile() As String

    Dim strFile As String
    With Application.FileDialog(1)
        .Title = "Select File"
        .InitialFileName = "c:\foldername" '<<== put your specific folder path here
        If .Show Then
            strFile = .SelectedItems(1)
        Else
            MsgBox "No file selected", vbInformation
            Exit Function
        End If
    End With
    
    BrowseFile = strFile
    
End Function
Hope that helps...
 
Hi. Pardon me for jumping in. @Gasman must be offline. I think this is what he meant.

Code:
Public Function BrowseFile() As String

    Dim strFile As String
    With Application.FileDialog(1)
        .Title = "Select File"
        .InitialFileName = "c:\foldername" '<<== put your specific folder path here
        If .Show Then
            strFile = .SelectedItems(1)
        Else
            MsgBox "No file selected", vbInformation
            Exit Function
        End If
    End With
   
    BrowseFile = strFile
   
End Function
Hope that helps...
Perfect, thank you very much
 
Sorry, sunbathing in the garden. :D

Not quite. This is what I was thinking of, however I have found that the FileDialog will use the last folder opened if no path supplied.

Code:
Private Sub cmdBrowse_Click()

    On Error GoTo Err_Handler
 
    Dim SB_Attachment As String
    Dim strFolder As String
    Dim strFile As String
    Dim intPos As Integer
 
    SB_Attachment = BrowseFile("C:\Temp\")
 
    If SB_Attachment <> "" Then
        ' get folder and file names
        intPos = InStrRev(SB_Attachment, "\\Sjo2054\shared\Data_Files\")
        strFolder = Left(SB_Attachment, intPos - 1)
        strFile = Mid(SB_Attachment, intPos + 1)
     
        ' populate text boxes on form
        SB_Attachment = SB_Attachment
   
    End If
 
Exit_Here:
    Exit Sub
 
Err_Handler:
    MsgBox Err.Description
    Resume Exit_Here

End Sub
Public Function BrowseFile(Optional strStartPath As String) As String

Dim strFile As String
With Application.FileDialog(1)
    .Title = "Select File"
    .InitialFileName = strStartPath
    If .Show Then
        strFile = .SelectedItems(1)
    Else
        MsgBox "No file selected", vbInformation
        Exit Function
    End If
End With

BrowseFile = strFile

End Function

Edit: ALso if you are just using a folder, walways complete the path with a "\", else the Filedailog puts the last folder name as the filename.
 
Last edited:

Users who are viewing this thread

Back
Top Bottom