InStr right to left

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Thanks for taking the time to read my question.

How can I search for a character in a string from right to left. InStr goes
left to right.

Brad
 
Use the InstrRev function but note that the paramaters are not in the same
position as the Instr function.
InStrRev(string1,string2,start,textcompare)
(Start equalling 1 means start from the first character at the back)
Check the help on InStrRev if you need more info.
 
There is a InStrRev() function
Returns the position of an occurrence of one string within another, from the
end of string.
 
Thanks Dennis!

I tried searching the help for anything like that, and it didn't come up!

Oh well, I know how to do it now thanks to you.

Have a great weekend,

Brad
 
Brad said:
Hi Dennis,

I tried it... is that available in Access 97?

Brad

No, InStrRev was introduced in Access 2000. Here's a lookalike function
that you can paste into a standard module and use in Access 97:

'----- start of code -----
Public Function InStrRev( _
StringCheck As String, _
StringMatch As String, _
Optional Start As Long = -1, _
Optional Compare As Integer = 2) _
As Long
'-----------------------------------------------------------
' Inputs: String to check,
' match string,
' optional starting position (default = -1),
' optional string compare value (default vbDatabaseCompare)
' Outputs: Position of match string, starting from the end
' Original code by: John L. Viescas 15-Nov-2001
' Revised by: Dirk Goldgar 21-Jan-2002
' Last Revision: Dirk Goldgar 21-Jan-2002
' ** Duplicates the functionality of the VB 6 INSTRREV function.
'-----------------------------------------------------------
Dim lngS As Long, lngI As Long
Dim lngLenC As Long, lngLenM As Long

' Do some initial checks
If (Compare < 0) Or (Compare > 2) Then
Err.Raise 5
Exit Function
End If
If Len(StringCheck) = 0 Then
InStrRev = 0
Exit Function
End If
If Len(StringMatch) = 0 Then
InStrRev = Start
Exit Function
End If
If Start > Len(StringCheck) Then
InStrRev = 0
Exit Function
End If
If Len(StringMatch) > Len(StringCheck) Then
InStrRev = 0
Exit Function
End If

' OK, have some work to do!
lngS = Start
lngLenC = Len(StringCheck)
lngLenM = Len(StringMatch)
If lngS = -1 Then lngS = lngLenC
lngS = (lngS - lngLenM) + 1
' Set default not found
InStrRev = 0
' Now loop to see if we can find it
For lngI = lngS To 1 Step -1
If StrComp(Mid$(StringCheck, lngI, lngLenM), _
StringMatch, _
Compare) _
= 0 _
Then
InStrRev = lngI
Exit For
End If
Next lngI

End Function
'----- end of code -----
 
Thanks Dirk,

Does this do the same thing?

x = 1
Do Until TheChar = "\"
If x = 1 Then
TheChar = Right(strInputFileName, x)
Else
TheChar = Left(Right(strInputFileName, x), 1)
End If
x = x + 1
Loop

Me.FileName = Right(strInputFileName, x - 2)

Brad
 
Brad said:
Thanks Dirk,

Does this do the same thing?

x = 1
Do Until TheChar = "\"
If x = 1 Then
TheChar = Right(strInputFileName, x)
Else
TheChar = Left(Right(strInputFileName, x), 1)
End If
x = x + 1
Loop

Me.FileName = Right(strInputFileName, x - 2)

No. What is it supposed to do? At the very least, that code runs the
risk of an infinite loop if there is no "\" character in
strInputFileName. If your purpose is to get the filename alone out of a
path specification, then *if* you can be sure that the path
specification contains a "\" character, you can use the InStrRev
function I posted as follows:

Me.FileName = _
Mid(strInputFileName, InStrRev(strInputFileName, "\") + 1)

An alternative, if you know the file exists, is to use the Dir()
function:

Me.FileName = Dir(strInputFileName)
 
Back
Top