Using UNC Path

B

BenL712

I am trying to write some code to let a user select a file on a network
location by browsing to it. I need to store the file path in a table field
(memo or text). I have been using the code suggested here
http://www.mvps.org/access/api/api0003.htm to get the UNC path, and the code
from the Access 2003 help system to get the file path (with drive letter). I
was attempting to just concatenate the file path starting at the 3rd
character to the UNC path, but unfortunately it appears that I cannot
concatenate anything to the UNC path. I've tried everything I can think of,
from just using a Trim() function to finding the first blank character
(Chr(32)) and taking everything to the left of that position to using
multiple string variables. Nothing so far has worked. Any help with this is
greatly appreciated!
 
B

BenL712

Here is the code I am using, copied from the Access help documentation with
modifications to use the fGetUncPath(x) function from Dev Ashish and Terry
Kreft.

'Declare a variable as a FileDialog object.
Dim fd As FileDialog
Dim UNCPath As String
Dim FilePath As String
Dim strDocLink As String

'Create a FileDialog object as a File Picker dialog box.
Set fd = Application.FileDialog(msoFileDialogFilePicker)

'Declare a variable to contain the path
'of each selected item. Even though the path is a String,
'the variable must be a Variant because For Each...Next
'routines only work with Variants and Objects.
Dim vrtSelectedItem As Variant

'Use a With...End With block to reference the FileDialog object.
With fd

'Use the Show method to display the File Picker dialog box and
return the user's action.
'The user pressed the action button.
If .Show = -1 Then

'Step through each string in the FileDialogSelectedItems
collection.
For Each vrtSelectedItem In .SelectedItems

'vrtSelectedItem is a String that contains the path of each
selected item.

FilePath = Mid(vrtSelectedItem, 3, 999)

UNCPath = fGetUNCPath(Left(vrtSelectedItem, 2))

strDocLink = Left(UNCPath, InStr(1, UNCPath, Chr(32)) - 1) &
FilePath

MsgBox strDocLink

Me.DocLink = strDocLink

Next vrtSelectedItem
'The user pressed Cancel.
Else
End If
End With

'Set the object variable to Nothing.
Set fd = Nothing


When I check the length of the UNCPath variable without removing
non-printing characters, it shows up as 256. When I use the Trim() command,
it still shows up as 256. If I use a MsgBox to show the contents of the
UNCPath and FilePath variables, they both look okay, but they won't
concatenate together.

Thanks again for all the help!
 
D

Douglas J. Steele

Yeah, Dev and Terry seem to have forgotten that WNetGetConnection pads the
string with Null characters.

Replace

fGetUNCPath = Left$(lpszRemoteName, cbRemoteName)

with

fGetUNCPath = Left$(lpszRemoteName, InStr(lpszRemoteName, vbNullChar) - 1)

(just in case there's wordwrap with this post, that's supposed to be a
single line of code)
 
B

BenL712

That worked! Thanks so much!

Douglas J. Steele said:
Yeah, Dev and Terry seem to have forgotten that WNetGetConnection pads the
string with Null characters.

Replace

fGetUNCPath = Left$(lpszRemoteName, cbRemoteName)

with

fGetUNCPath = Left$(lpszRemoteName, InStr(lpszRemoteName, vbNullChar) - 1)

(just in case there's wordwrap with this post, that's supposed to be a
single line of code)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top