Solved

Change Drive Letter to Full Path

Posted on 2007-03-26
10
995 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
 

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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 119

Expert Comment

by:Rey Obrero
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

When you are entering numbers in a speadsheet, and don't remember what 6×7 is, you just type “=6*7" instead. It works in every cell! This is not so in Access. To enter the elusive 42 in a text box, you have to find a calculator, and then copy the re…
Describes a method of obtaining an object variable to an already running instance of Microsoft Access so that it can be controlled via automation.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.

707 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now