Hi everyone.
I have a two fields on a form, strImagePath and strImageTitle. strImagePath contains a file location path and strImageTitle is for the name of the file.
I am trying to build an OnExit event for strImagePath that will update strImageTitle with all of the characters in strImagePath from the right until the first \ is encountered.
So for example C:\Project Documents\Budget\Staffing\Resource Staffing Plan.xlsx would trim and put Resource Staffing Plan.xlsx into strImageTItle.
I am trying :
Private Sub strImagePath_Exit(Cancel As Integer)
strImageTitle = Trim(Right([strImagePath], (InStr(1, [strImagePath], "\"))
End Sub
But can’t get it to work.
Thank you for any help.
-Scott
Trim(right) until backslash is encountered
-
- 4StarLounger
- Posts: 402
- Joined: 14 Apr 2010, 15:59
-
- Administrator
- Posts: 78577
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Trim(right) until backslash is encountered
Instead of the On Exit event, I'd use the After Update event - strImageTitle only needs to be updated when strImagePath has been changed.
Try this:
Try this:
Code: Select all
Private Sub strImagePath_AfterUpdate()
Dim arr() As String
If IsNull(Me.strImagePath) Then
Me.strImageTitle = Null
Else
arr = Split(Me.strImagePath, "\")
Me.strImageTitle = arr(UBound(arr))
End If
End Sub
Best wishes,
Hans
Hans
-
- 4StarLounger
- Posts: 402
- Joined: 14 Apr 2010, 15:59
Re: Trim(right) until backslash is encountered
Thanks Hans. AfterUpdate makes more sense. Works great. How does arr = Split(Me.strImagePath, " \") "know" to work from the right to the left ?
-
- Administrator
- Posts: 78577
- Joined: 16 Jan 2010, 00:14
- Status: Microsoft MVP
- Location: Wageningen, The Netherlands
Re: Trim(right) until backslash is encountered
Split takes the value of strImagePath and splits it into parts based on "\" as delimiter. The result is an array where each part is an element.
For example, splitting "C:\Project Documents\Budget\Staffing\Resource Staffing Plan.xlsx" on "\" will result in an array consisting of 5 elements:
"C:", "Project Documents", "Budget", "Staffing", and "Resource Staffing Plan.xlsx".
arr(0) is the first element: "C:".
arr(1) is the second element: "Project Documents".
arr(UBound(arr)) returns the last element of the array, in this example "Resource Staffing Plan.xlsx".
For example, splitting "C:\Project Documents\Budget\Staffing\Resource Staffing Plan.xlsx" on "\" will result in an array consisting of 5 elements:
"C:", "Project Documents", "Budget", "Staffing", and "Resource Staffing Plan.xlsx".
arr(0) is the first element: "C:".
arr(1) is the second element: "Project Documents".
arr(UBound(arr)) returns the last element of the array, in this example "Resource Staffing Plan.xlsx".
Best wishes,
Hans
Hans
-
- 4StarLounger
- Posts: 402
- Joined: 14 Apr 2010, 15:59
Re: Trim(right) until backslash is encountered
Thank you for the explanation. Very good to learn about this. :-)
-
- 3StarLounger
- Posts: 331
- Joined: 11 Feb 2010, 03:55
- Location: Land O Lakes, FL
Re: Trim(right) until backslash is encountered
Now that you've learned about Split(), you might also want to know about Join(). The does just the opposite, it builds a string from an array with a designated delimiter!
Mark Liquorman
Land O Lakes, FL
see my website http://www.liquorman.net for Access Tips and Tricks, and for my Liquorman Utilities.
Land O Lakes, FL
see my website http://www.liquorman.net for Access Tips and Tricks, and for my Liquorman Utilities.
-
- 4StarLounger
- Posts: 402
- Joined: 14 Apr 2010, 15:59
Re: Trim(right) until backslash is encountered
Very cool. Thank you Mark.