Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Change Drive Letter to Full Path

Posted on 2007-03-26
10
Medium Priority
?
1,033 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public 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 2000 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

As tax season makes its return, so does the increase in cyber crime and tax refund phishing that comes with it
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

715 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