Solved

Change Drive Letter to Full Path

Posted on 2007-03-26
10
1,005 Views
Last Modified: 2009-06-25
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?
0
Comment
Question by:MDauphinais1
  • 2
  • 2
  • 2
  • +4
10 Comments
 
LVL 9

Expert Comment

by:sah18
ID: 18793143
Could you show the code where they are prompted for the path?
0
 
LVL 39

Expert Comment

by:stevbe
ID: 18793196
0
 
LVL 39

Expert Comment

by:stevbe
ID: 18793203
sorry ... that article explains nad has code to cover mapped drive letter to UNC path, you then would stitch the pieces together
0
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 

Author Comment

by:MDauphinais1
ID: 18793263
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...
0
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 18793266
I'm not sure, but can you get at wmi in VBA code?
Something like this would work...

TextDrive="M:\folder1\folder2"
strDrive=Left(TextDrive,2)

Dim objWMI
Set objWMI=GetObject("winmgmts:\\.\root\CIMV2")
Set colDisk=objWMI.ExecQuery ("Select * From Win32_LogicalDisk Where DeviceID='" & strDrive & "'")
For Each Item in colDisk
  strPath=Replace(TextDrive,strDrive,Item.ProviderName)
Next

'strPath should hold the full path now..
0
 
LVL 13

Expert Comment

by:adraughn
ID: 18793322
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

0
 
LVL 15

Expert Comment

by:cquinn
ID: 18793346
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
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 18793358
0
 

Author Comment

by:MDauphinais1
ID: 18793428
sirbounty, that works perfectly. Thanks.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 18793434
Glad i could be of assistance. :^)
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

QuickBooks® has a great invoice interface that we were happy with for a while but that changed in 2001 through no fault of Intuit®. Our industry's unit names are dictated by RUS: the Rural Utilities Services division of USDA. Contracts contain un…
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question