Solved

Change Drive Letter to Full Path

Posted on 2007-03-26
10
1,023 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Microsoft Access is a place to store data within tables and represent this stored data using multiple database objects such as in form of macros, forms, reports, etc. After a MS Access database is created there is need to improve the performance and…
Familiarize people with the process of utilizing SQL Server views 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 Access…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …

728 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