Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract part of a text string 2

Status
Not open for further replies.

twsjd

Technical User
Sep 18, 2002
5
US
Say if i have a string of text that is actually a file name.

Like C:\File.txt

how can i use access 2000 and vb to extract just the filename so i would have "File.txt" or just the file extension "txt"?

Thanks
Jon
 
Use InStrRev to find the last "\" like so:

Dim strFileName As String
Dim strFileExt As String
Dim lngWhereAt As Long

lngWhereAt = InStrRev(YourString, "\")
If lngWhereAt > 0 Then
strFileName = Mid$(YourString, lngWhereAt + 1)
Else
strFileName = YourString
End If

lngWhereAt = InStr(strFileName, ".")
If lngWhereAt > 0 Then
strFileExt = Mid$(strFileName, lngWhereAt + 1)
End If

Good Luck!

 
Thanks SBendBuckeye i got that to work. But can you kind of explain how it works? I am having a hard time figuring out how it works.

Thanks Jon
 
InStr(Search String, Find String)

The instr function returns a number indicating the first position of Find String in Search String (as an example, instr("ABCDE", "CD") = 3. InStrRev works the same way except it starts on the right and works its way back to the left. If the string is not found, it returns zero.

In the above, it looks for the last "\". Then the Mid$ function returns a piece of the string starting at X for Y characters. If y is not specified it returns the rest of the file. In the example above, it gets the full file name by taking everything after the last "\".

It then finds the extension by doing the same thing with the file name but taking everything past the "."

Good LucK!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top