2015-06-20 40 views
1

如何检索当前数据库的当前路径? 我有一个AC07程序,分发它我在Intranet服务器上保存一个副本,如何将该程序复制到我们的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 

是否有另一种方式做到这一点?

+0

您可以使用API​​调用来检索映射驱动器的服务器。但是你现在使用双重检查的方法很简单,所以为什么不坚持呢? – Gustav

+0

@Gustav,是的双重选择的方法是好的,但你知道一个有1000个用户的公司很容易有人在S:或Z:或X上找到服务器目录:......我的是感谢 – Fabrizio

+0

好吧,您只需要命名H驱动器。那么您应该使用API​​方法 - 就像Cor提供的那样。 – Gustav

回答

1

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

例如为:

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

+0

这是正确的,你的提示是完美的。谢谢。 – Fabrizio

+0

@Fabrizio谢谢 - 很高兴它帮助。请将此标记为其他人未来参考的答案。 –