Find the directory part (minus the filename) of a full path in access 97

前端 未结 10 525
太阳男子
太阳男子 2020-12-10 23:38

For various reasons, I\'m stuck in Access 97 and need to get only the path part of a full pathname.

For example, the name

c:\\whatever dir\\another d         


        
10条回答
  •  南方客
    南方客 (楼主)
    2020-12-11 00:14

    If you're just needing the path of the MDB currently open in the Access UI, I'd suggest writing a function that parses CurrentDB.Name and then stores the result in a Static variable inside the function. Something like this:

    Public Function CurrentPath() As String
      Dim strCurrentDBName As String
      Static strPath As String
      Dim i As Integer
    
      If Len(strPath) = 0 Then
         strCurrentDBName = CurrentDb.Name
         For i = Len(strCurrentDBName) To 1 Step -1
           If Mid(strCurrentDBName, i, 1) = "\" Then
              strPath = Left(strCurrentDBName, i)
              Exit For
           End If
        Next
      End If
      CurrentPath = strPath
    End Function
    

    This has the advantage that it only loops through the name one time.

    Of course, it only works with the file that's open in the user interface.

    Another way to write this would be to use the functions provided at the link inside the function above, thus:

    Public Function CurrentPath() As String
      Static strPath As String
    
      If Len(strPath) = 0 Then
         strPath = FolderFromPath(CurrentDB.Name)
      End If
      CurrentPath = strPath
    End Function
    

    This makes retrieving the current path very efficient while utilizing code that can be used for finding the path for any filename/path.

提交回复
热议问题