Query Expression (1 Viewer)

kitty77

Registered User.
Local time
Today, 15:40
Joined
May 27, 2019
Messages
712
How can I create an expression in a query that brings back only after the last \?
Also, if the field has no value then return a blank or null.

\6002.pdf (should look like this)
\\SERVER\Plus\Spec\Results\ABC\ABC\Results\6002.pdf

Thanks.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 12:40
Joined
Aug 30, 2003
Messages
36,128
Check out the InStrRev() function. That along with the Mid() function should get what you want. You'll have to first test for blank/null/no slash.
 

kitty77

Registered User.
Local time
Today, 15:40
Joined
May 27, 2019
Messages
712
Any examples you could provide?
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 12:40
Joined
Aug 30, 2003
Messages
36,128
Not offhand, just start with the InStrRev() and work your way out a step at a time. Post back if you get stuck.
 

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:40
Joined
May 21, 2018
Messages
8,555
It can be done with just the mid and instrrev, but you likely in a query will have to handle a Null value and a value without a slash
You can do it in line with an iif but easier to do a simple UDF.
Code:
Public Function AfterSlash(TheVal As Variant) As Variant
  AfterSlash = TheVal
  If Not IsNull(TheVal) Then
    If InStr(AfterSlash, "\") > 0 Then
      AfterSlash = Mid(AfterSlash, InStrRev(AfterSlash, "\") + 1)
    End If
  End If
End Function

Or the painful inline
Code:
IIf(InStr([field1],"\")>0,Mid([field1],InStrRev([field1],"\")+1),[field1])
 
Last edited:

MajP

You've got your good things, and you've got mine.
Local time
Today, 15:40
Joined
May 21, 2018
Messages
8,555
See
which is almost the exact opposite question. The OP wanted to remove everything after the last slash.
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 12:40
Joined
Aug 30, 2003
Messages
36,128
There's your fish. ;)
 

kitty77

Registered User.
Local time
Today, 15:40
Joined
May 27, 2019
Messages
712
It can be done with just the mid and instrrev, but you likely in a query will have to handle a Null value and a value without a slash
You can do it in line with an iif but easier to do a simple UDF.
Code:
Public Function AfterSlash(TheVal As Variant) As Variant
  AfterSlash = TheVal
  If Not IsNull(TheVal) Then
    If InStr(AfterSlash, "\") > 0 Then
      AfterSlash = Mid(AfterSlash, InStrRev(AfterSlash, "\") + 1)
    End If
  End If
End Function

Or the painful inline
Code:
IIf(InStr([field1],"\")>0,Mid([field1],InStrRev([field1],"\")+1),[field1])
Perfect!
 

Users who are viewing this thread

Top Bottom