Office中国论坛/Access中国论坛

标题: 用vba可以实现映射网络驱动器吗? [打印本页]

作者: jyf    时间: 2003-1-14 23:01
标题: 用vba可以实现映射网络驱动器吗?
我们单位的MDB程序放在服务器上,客户端映射服务器为T:盘(启动时重连接),然后通过快捷方式运行服务器MDB程序。

有时候,客户端启动时网络不通等原因,映射驱动器被删掉了。

能不能在MDB启动时判断映射驱动器是否存在,如果不存在就映射一个呢?
作者: zhengjialon    时间: 2003-1-14 23:13
下面的代码,试过不是太理想,请有兴趣的试一下:
Option Compare Database
Private Declare Function WNetAddConnection Lib "mpr.dll" Alias "WNetAddConnectionA" (ByVal lpszNetPath As String, ByVal lpszPassword As String, ByVal lpszLocalName As String) As Long
Private Declare Function WNetCancelConnection Lib "mpr.dll" Alias "WNetCancelConnectionA" (ByVal lpszName As String, ByVal bForce As Long) As Long
Const WN_SUCCESS = 0 ' The function was successful.
Const WN_NET_ERROR = 2 ' An error occurred on the network.
Const WN_BAD_PASSWORD = 6 ' The password was invalid.
Function AddConnection(MyShareName As String, MyPWD As String, UseLetter As String) As Integer
    On Local Error GoTo AddConnection_Err
    AddConnection = WNetAddConnection(MyShareName, MyPWD, UseLetter)
AddConnection_End:
    Exit Function
AddConnection_Err:
    AddConnection = Err
    MsgBox Error$
    Resume AddConnection_End
End Function
Function CancelConnection(DriveLetter As String, Force As Integer) As Integer
    On Local Error GoTo CancelConnection_Err
    CancelConnection = WNetCancelConnection(DriveLetter, Force)
CancelConnection_End:
    Exit Function
CancelConnection_Err:
    CancelConnection = Err
    MsgBox Error$
    Resume CancelConnection_End
End Function

Private Sub 命令0_Click()
    'to add a connection call by:
    AddConnection "\\4IN1\DATA", vbNullString, "z"
    'AddConnection "\\Master\2000资料", vbNullString, "g"
End Sub

Private Sub 命令1_Click()
    'To cancel a connection type:
    CancelConnection "\\4IN1\DATA", 0
End Sub



[此贴子已经被zhengjialon于2003-1-14 15:13:09编辑过]


作者: jyf    时间: 2003-1-14 23:30
谢谢版主,我先试试。
作者: jyf    时间: 2003-1-15 23:13
我试过了,稍微改一下就成功了,挺好用的。再一次谢谢!
版主说“不是太理想“是什么意思?

映射:AddConnection "\\server\exp_tae", vbNullString, "Q:"
断开:CancelConnection "Q:", 1
作者: aronliu    时间: 2009-10-24 16:24
thanks
作者: wmok    时间: 2010-6-12 18:25
请教:这个映射的网络驱动器Q可以隐藏不显示出来吗?




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3