从服务器上保存的文件中检索当前路径

时间:2015-06-20 08:40:29

标签: vba ms-access

如何检索当前数据库的当前路径? 我有一个AC07程序,要分发它我在内网服务器上保存了一个副本,如何将这个程序复制到我们的PC然后使用它? 总是有些人直接在服务器上打开文件。

当文件自动打开一个表单星形时,我在此表单中输入以下代码:


Private Sub Form_Load()
On Error GoTo ErrorHandler
Dim StrServer As String
StrServer = "\\itbgafs01\Comune\Dashboard\"
If GetDBPath() = StrServer Then
    MsgBox "You can't open this file from server" & vbCrLf & _
            "save one copy on you PC, and use those", vbCritical, "Dashboard.info"
    Application.Quit
End If
Public Function GetDBPath() As String
    Dim strFullPath As String
    Dim I As Integer

    strFullPath = CurrentDb().Name

    For I = Len(strFullPath) To 1 Step -1
        If Mid(strFullPath, I, 1) = "\" Then
            GetDBPath = left(strFullPath, I)
            Exit For
        End If
    Next
End Function

我的问题是:某些PC映射到驱动器H:服务器目录,然后路径结果为H:\Comune\Dashboard\而不是\\itbgafs01\\Dashboard\。 我怎样才能找到绝对路径? 首先,我认为如果喜欢使用更多:


Private Sub Form_Load()
On Error GoTo ErrorHandler
Dim StrServer As String
Dim StrMaph As String
StrServer = "\\itbgafs01\Comune\Dashboard\"
StrMaph = "H:\Comune\Dashboard\"
MsgBox StrServer & vbCrLf & _
        StrMaph & vbCrLf & _
        GetDBPath()
If GetDBPath() = StrServer Or GetDBPath() = StrMaph Then
    MsgBox "Non puoi aprire il file sul server" & vbCrLf & _
            "copialo sul tuo pC ed avvia il programma da li", vbCritical, "Dashboard.info"
    Application.Quit
End If

还有其他办法吗?

1 个答案:

答案 0 :(得分:1)

您可以使用Scripting Runtime获取驱动器的UNC路径,然后将其替换为currentDb.Name。

E.g:

Sub blah()
    Debug.Print GetUNCPath(CurrentDb.Name)
End Sub


Function GetUNCPath(path As String) As String
    Dim fso As Object, shareName
    Set fso = CreateObject("Scripting.FileSystemObject")

    shareName = fso.GetDrive( _
                            fso.GetDriveName(path)).shareName

    'sharename is empty if it wasn't a network mapped drive (e.g. local C: drive)
    If shareName <> "" Then
        GetUNCPath = shareName & Right(path, Len(path) - InStr(1, path, "\"))
    Else
        GetUNCPath = path
    End If

End Function

编辑:或者您可以使用对WinAPI的调用来获取信息:https://support.microsoft.com/en-us/kb/160529