Link to home
Start Free TrialLog in
Avatar of MDauphinais1
MDauphinais1

asked on

Change Drive Letter to Full Path

I have a form where people enter the path to a network folder. The problem is that many people will enter the path of their mapped drive which is not the same on every system. So if someone else trys to click on the link they get path not found because they are not mapped to the same drive letter.

Is there a way to automatically change the path from a drive letter to the full path using After Update?

Say someone enters the path M:\Folder1\folder2 I want to automatically detect the full path that M: drive is mapped to and change the users entry to \\server1\folder1\folder2

Can this be done?
Avatar of sah18
sah18
Flag of United States of America image

Could you show the code where they are prompted for the path?
Avatar of stevbe
stevbe

sorry ... that article explains nad has code to cover mapped drive letter to UNC path, you then would stitch the pieces together
Avatar of MDauphinais1

ASKER

Yeah I was noticing that... so if someone enters M:\folder1\folder2

How do I then take that and convert it to \\server1\folder1\folder2  ?

I don't know how to take it apart just to grab the drive letter and then put it back again...
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have a similar issue. If youare using SQL as a back end you can use a stored procedure to change the value and use a trigger to call th SP when a user inserts a new record. The SP and trigger are below:

Stored Procedure:

USE [db name]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE PROCEDURE [dbo].[Fix_All_ImagePath_UNCs]
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

Update dbo.tblTD_Pics SET ImagePath_1 = REPLACE(ImagePath_1, LEFT(Imagepath_1, 3), '\\ServerName\Folder\')  
Where ImagePath_1 LIKE '_:\%'

END

Trigger:

USE [DatabaseName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[Fix_ImagePaths]
   ON  [dbo].[tblTD_Pics]
   AFTER INSERT
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;

exec dbo.Fix_All_ImagePath_UNCs
END


I hope this helps.

-a

Create a module friom the code below and call it like this

Me.txtFolderLocation = ConvertToUNC(me.txtFolderLocation)

==========================================================


Option Explicit

' This API declaration is used to return the
' UNC path from a drive letter.
Declare Function WNetGetConnection Lib "mpr.dll" Alias "WNetGetConnectionA" (ByVal lpszLocalName As String, ByVal lpszRemoteName As String, cbRemoteName As Long) As Long

' These represent the possible returns errors from API.
Public Const ERROR_BAD_DEVICE = 1200&
Public Const ERROR_CONNECTION_UNAVAIL = 1201&
Public Const ERROR_EXTENDED_ERROR = 1208&
Public Const ERROR_MORE_DATA = 234
Public Const ERROR_NOT_SUPPORTED = 50&
Public Const ERROR_NO_NET_OR_BAD_PATH = 1203&
Public Const ERROR_NO_NETWORK = 1222&
Public Const ERROR_NOT_CONNECTED = 2250&
Public Const NO_ERROR = 0



Public Function ConvertToUNC(ByVal sFileName As String) As String
    Dim sDrive As String, sPath As String, sUNC As String
On Error GoTo HandleErr
    If Mid$(sFileName, 2, 1) = ":" Then
        sDrive = left$(sFileName, 2)
        sPath = Mid$(sFileName, 3)
        sUNC = GetUNCPath(sDrive)
        If sUNC <> sDrive Then sFileName = sUNC & sPath
    End If
    ConvertToUNC = sFileName
ExitHere:
    Exit Function

HandleErr:
    Select Case Err.Number
        Case Else
            MsgBox "Error " & Err.Number & ": " & Err.Description, vbCritical, "ConvertToUNC"  
    End Select
' End Error handling block.
End Function

Function GetUNCPath(strDriveLetter As String) As String
    On Local Error GoTo GetUNCPath_Err

    Dim Msg As String, lngReturn As Long
    Dim lpszLocalName As String
    Dim lpszRemoteName As String
    Dim cbRemoteName As Long
    lpszLocalName = strDriveLetter
    lpszRemoteName = String$(255, Chr$(32))
    cbRemoteName = Len(lpszRemoteName)
    lngReturn = WNetGetConnection(lpszLocalName, lpszRemoteName, cbRemoteName)
    Select Case lngReturn

    Case NO_ERROR
        GetUNCPath = left$(lpszRemoteName, InStr(lpszRemoteName, Chr$(0)) - 1)
    Case Else
        GetUNCPath = strDriveLetter
    End Select

GetUNCPath_End:
    Exit Function
GetUNCPath_Err:
    MsgBox Err.Description, vbInformation
    Resume GetUNCPath_End

End Function
sirbounty, that works perfectly. Thanks.
Glad i could be of assistance. :^)