Getting data from FileDialog

K

Kevin

I'm using Access 2003's new FileDialog object to allow a
user to select a CSV file to import. I've used the
common dialog Active X before, but would like to try this
since it doesn't require the seperate OCX file. I'm
kinda of scared of API calls too.

When data is imported, it typically creates an import
error file. I would like to progmatically open the error
table after import, but I won't always know what the name
of the new table is- unless I can extract the CSV
filename from the full path.

To make a long story short, does anyone know how to pull
just the file name from the FileDialog object and not the
full path. Any other info on learning about the rest of
the collection would be appreciated too!

Thanks in advance!
Kevin
 
M

Mark

Hi,

I use the following function to get the various parts os a selected filename
& path.

An example call would be:

strFileName = ParsePath(strSelectedFile, 1)

Function ParsePath(strPath As String, lngPart As Integer) As String

' This procedure takes a file path and returns
' the path (everything but the file name), the
' file name, the drive letter, or the file extension,
' depending on which constant was passed in.

Dim lngPos As Long
Dim strPart As String
Dim blnIncludesFile As Boolean
Dim intLenofExt As Integer
' Check that this is a file path.
' Find the last path separator.
lngPos = InStrRev(strPath, "\")
'If not fullstop add one
If InStr(strPath, ".") = 0 Then strPath = strPath & "."
' Determine whether portion of string after last backslash
' contains a period.

blnIncludesFile = InStrRev(strPath, ".") > lngPos

If lngPos > 0 Then
Select Case lngPart
' Return file name.
Case 1
If blnIncludesFile Then
intLenofExt = Len(Mid(strPath, InStrRev(strPath, ".") +
1))
strPart = Right$(strPath, Len(strPath) - lngPos)
' do not include the file ext in the filename
strPart = Left(strPart, Len(strPart) - (intLenofExt +
1))
Else
strPart = Right$(strPath, Len(strPath) - lngPos)
End If
' Return path.
Case 2
If blnIncludesFile Then
strPart = Left$(strPath, lngPos)
Else
strPart = strPath
End If
' Return drive.
Case 3
strPart = Left$(strPath, 3)
' Return file extension.
Case 4
If blnIncludesFile Then
' Take three characters and period.
strPart = Mid(strPath, InStrRev(strPath, "."), 4)
Else
strPart = ""
End If
Case Else
strPart = ""
End Select
End If
ParsePath = strPart

ParsePath_End:
Exit Function
End Function

HTH

--

Cheers
Mark

Free Access/Office Add-Ins at:
http://mphillipson.users.btopenworld.com/
 
K

Kevin

Mark,

Instead of creating the function, I just pulled out what
I needed and it works great.

For others' reference, here is what I came up with for
opening the newly created table progmatically. The
variant vrtSelectedItem is how the variable was set up in
Microsoft's FileDialog example in the knowledgebase. For
my purposes, I just passed the values through to change
less code:
Dim strPart As String
Dim intLenofExt As Integer
strFileName = vrtSelectedItem
Dim lngPos As Long

' Check that this is a file path.
' Find the last path separator.
lngPos = InStrRev(strFileName, "\")
intLenofExt = Len(Mid(strFileName, InStrRev
(strFileName, ".") + 1))
strPart = Right$(strFileName, Len(strFileName) -
lngPos)
' do not include the file ext in the filename
strPart = Left(strPart, Len(strPart) -
(intLenofExt + 1))
strPart = strPart & "_ImportErrors"
DoCmd.OpenTable strPart



Thank you very much for the help!
Kevin
 

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