Newsgroup: it.comp.lang.visual-basic
Titolo: HELP: UNC path in ADO Connection string

Hi All!
I have a little problem with ADODB.Connection.
I want to connect to mdb file passing its path\name in connection
string. But the problem is that Open connection failed if I try to use
UNC path: \\server\shared_folder\file.mdb instead of c:\...\file.mdb.
Have someone solved this problem before? Please let me know!
Regards,
Kornelii Shnaps
mailto:
korn...@my-deja.com
Sent via Deja.com 
http://www.deja.com/
Before you buy.

You have to connect your network resource writing this code:
    sDrive = ConnectNetworkDrive("\\ServerName\ShareName", "UserName",
"Password")
    Than, write Ado connection code using:
    sDrive & "\YourFile.mdb"
    and  don't forget:
    DisconnectNetworkDrive sDrive
Insert this code in a .Bas module:
Public Declare Function WNetAddConnection2 Lib "mpr.dll" _
    Alias "WNetAddConnection2A" _
   (lpNetResource As NETRESOURCE, _
    ByVal lpPassword As String, _
    ByVal lpUserName As String, _
    ByVal dwFlags As Long) As Long
Public Declare Function WNetCancelConnection2 Lib "mpr.dll" _
    Alias "WNetCancelConnection2A" _
   (ByVal lpName As String, _
    ByVal dwFlags As Long, _
    ByVal fForce As Long) As Long
Public Declare Function WNetConnectionDialog Lib "mpr.dll" _
   (ByVal hwnd As Long, ByVal dwType As Long) As Long
Public Declare Function WNetDisconnectDialog Lib "mpr.dll" _
   (ByVal hwnd As Long, ByVal dwType As Long) As Long
'Public Const RESOURCE_CONNECTED = &H1
'Public Const RESOURCE_REMEMBERED = &H3
'Public Const RESOURCEDISPLAYTYPE_DOMAIN = &H1
'Public Const RESOURCEDISPLAYTYPE_GENERIC = &H0
'Public Const RESOURCEDISPLAYTYPE_SERVER = &H2
'Public Const RESOURCEUSAGE_CONTAINER = &H2
Public Const NO_ERROR = 0
Public Const CONNECT_UPDATE_PROFILE = &H1
Public Const RESOURCETYPE_DISK = &H1
Public Const RESOURCETYPE_PRINT = &H2
Public Const RESOURCETYPE_ANY = &H0
Public Const RESOURCE_GLOBALNET = &H2
Public Const RESOURCEDISPLAYTYPE_SHARE = &H3
Public Const RESOURCEUSAGE_CONNECTABLE = &H1
Public Declare Function ShellExecute Lib "shell32.dll" _
    Alias "ShellExecuteA" _
   (ByVal hwnd As Long, _
    ByVal lpOperation As String, _
    ByVal lpFile As String, _
    ByVal lpParameters As String, _
    ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long
Public Const SW_SHOWNORMAL = 1
Public Function ConnectNetworkDrive(ServerPath As String, UserName As
String, Optional Password As String) As String
    Dim NETR As NETRESOURCE
    Dim errinfo As Long
    Dim x As Long
    Dim TestDrive As String
    x = 67
    Do
        x = x + 1
        TestDrive = Chr$(x) & ":"
        With NETR
           .dwScope = RESOURCE_GLOBALNET
           .dwType = RESOURCETYPE_DISK
           .dwDisplayType = RESOURCEDISPLAYTYPE_SHARE
           .dwUsage = RESOURCEUSAGE_CONNECTABLE
           .lpRemoteName = ServerPath
           .lpLocalName = TestDrive
        End With
        errinfo = WNetAddConnection2(NETR, Password, UserName,
CONNECT_UPDATE_PROFILE)
    Loop Until x = 90 Or errinfo = NO_ERROR   '90 = "z"
    If (errinfo = NO_ERROR) Then
        ConnectNetworkDrive = TestDrive
    Else
        ConnectNetworkDrive = ""
    End If
End Function
Public Function DisconnectNetworkDrive(DriveLetter As String) As Boolean
    Dim errinfo As Long
    errinfo = WNetCancelConnection2(DriveLetter, CONNECT_UPDATE_PROFILE,
True)
    DisconnectNetworkDrive = errinfo = NO_ERROR
End Function
Ciao
--
      Pino Saffioti
-----------------------------------
pino.s...@asl1.to.it