How to point to a specific Windows location in code?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sueb
    Contributor
    • Apr 2010
    • 379

    How to point to a specific Windows location in code?

    We just moved our database to a more secure volume, and everything is great except for one thing: I have a button on a form that opens a specific folder based on the record in hand. The path is pretty simple (it used to be "V:\Pre-Admitting\eChar ts\"), and it worked like a charm. The new location is "V:\IUR\eCharts ", but when I replaced the string in my code, I get a "Path not found" error.

    I can't figure out what's wrong, since the permissions are correct (I can navigate to the path outside of the database), and the path does actually exist, and it worked great with the old address.

    Here's the code:

    Code:
    Private Sub Open_eChart_Click()
    On Error GoTo Err_Open_eChart_Click
    
        Dim retVal As Variant
        Dim strPartialPath As String
        Dim intCounter As Integer
        Dim strThisChar As String
        Const conPAth As String = "V:\IUR\eCharts\"
      
        If Not IsNull(Me![ChartNum]) Then
          If Left$(Me![ChartNum], 1) = "\" Then
            strPartialPath = Mid$(Me![ChartNum], 2)
          Else
            strPartialPath = Me![ChartNum]
          End If
        Else
          Exit Sub
        End If
        
        'Replace any commas with underscores
        For intCounter = 1 To Len(strPartialPath)
            strThisChar = Mid(strPartialPath, intCounter, 1)
            If strThisChar = "," Then
               strPartialPath = Left(strPartialPath, intCounter - 1) & _
                    "_" & _
                    Mid(strPartialPath, intCounter + 1, 255)
            End If
        Next
      
        If Dir$(conPAth & strPartialPath, vbDirectory) <> "" Then
          retVal = Shell("Explorer.exe " & conPAth & strPartialPath, vbNormalFocus)
        Else
          MkDir (conPAth & strPartialPath)
          retVal = Shell("Explorer.exe " & conPAth & strPartialPath, vbNormalFocus)
        End If
    
    Exit_Open_eChart_Click:
       Exit Sub
    
    Err_Open_eChart_Click:
        MsgBox Err.Description
        Resume Exit_Open_eChart_Click
    
    End Sub
    Any help will be greatly appreciated!
  • ADezii
    Recognized Expert Expert
    • Apr 2006
    • 8834

    #2
    Not sure about the PATH Error, but I do believe that your Code can be simplified and made more efficient:
    Code:
    Dim retVal As Variant
    Dim strPartialPath As String
    Const conPAth As String = "V:\IUR\eCharts\"
    
    If IsNull(Me![ChartNum]) Then Exit Sub
    
    strPartialPath = Replace(Replace(Me![ChartNum], "\", ""), ",", "_")
      
    If Dir$(conPAth & strPartialPath, vbDirectory) = "" Then MkDir (conPAth & strPartialPath)
    
    retVal = Shell("Explorer.exe " & conPAth & strPartialPath, vbNormalFocus)

    Comment

    • sueb
      Contributor
      • Apr 2010
      • 379

      #3
      This IS nicer, thanks!

      It turns out that the "path not found" is a function of how the new location was set up, so IT is fixing that. But I'm really glad I asked, since it garnered me this nice code simplification. :)

      Comment

      • NeoPa
        Recognized Expert Moderator MVP
        • Oct 2006
        • 32645

        #4
        With just a quick glance I can only say that the formats of the old and new folder strings are different - the newer one being without a closing "\".

        Good code in this area should handle that by using something like :
        Code:
        FullPath = PathString & "\" & FileName
        FullPath = Replace(FullPath, "\\", "\")
        That way such oversights don't cause issues.

        I expect ADezii's code does something on similar lines.

        Comment

        Working...