UtterAccess HomeUtterAccess Wiki

Welcome Guest ( Log In | Register )

Custom Search
Edit Discussion
> InStrRev    



Contents

Synopsis

Replacement for InStrRev function for older versions of Access (Access 97 and before). Use of this function allows those versions to use other functions in this library, such as GetFileExtension, GetFilePath, and GetFilenameFromPath.

CODE
' InStrRev
' http://www.utteraccess.com/wiki/InStrRev
' Code courtesy of UtterAccess Wiki
' Licensed under Creative Commons License
' http://creativecommons.org/licenses/by-sa/3.0/
'
' You are free to use this code in any application,
' provided this notice is left unchanged.
'
' rev  date                          brief descripton
' 1.0  2010-09-11                    Returns the position of an occurrence of one string within another, from the end of string.
'
'NOTE:
'   Help file is wrong that vbUseCompareOption is defined as -1. At least in A2010 it is undefined.
'   Help file is wrong that if start > Len(stringmatch) InStrRev returns 0. It does not. It would be illogical if it did. I am assuming they meant "if start > Len(stringcheck)".
'AUTHOR:
'   Tom van Stiphout, Microsoft Access MVP
Public Function InStrRev(ByVal stringCheck As Variant, ByVal stringMatch As Variant, Optional ByVal start As Integer = -1, Optional ByVal compare As Integer = -1) As String
   Dim varReturn As Variant
   Dim lenCheck As Integer
   Dim lenMatch As Integer
   
   If IsNull(stringCheck) Or IsNull(stringMatch) Then
       varReturn = Null
   ElseIf Len(stringCheck) = 0 Then
       varReturn = 0
   ElseIf Len(stringMatch) = 0 Then
       varReturn = start
   ElseIf start > Len(stringCheck) Then
       varReturn = 0
   ElseIf start = 0 Then
       Err.Raise 5
   Else
       lenCheck = Len(stringCheck)
       lenMatch = Len(stringMatch)
       If lenMatch > lenCheck Then
           varReturn = 0
       Else
           If start = -1 Then start = lenCheck - lenMatch + 1 Else start = start - lenMatch + 1
           For varReturn = start To 1 Step -1
               If compare = -1 Then 'Help file says vbUseCompareOption is -1, but it is not defined (A2010).
                   If StrComp(Mid$(stringCheck, varReturn, lenMatch), stringMatch) = 0 Then Exit For 'found it.
               Else
                   If StrComp(Mid$(stringCheck, varReturn, lenMatch), stringMatch, compare) = 0 Then Exit For 'found it.
               End If
           Next
       End If
   End If
   InStrRev = varReturn
End Function

Creative Commons License
InStrRev by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.


Synopsis

Another and smaller flavor of a replacement for InStrRev function for older versions of Access (Access 97 and before).

CODE
' InStrRev
' http://www.utteraccess.com/wiki/InStrRev
' Code courtesy of UtterAccess Wiki
' Licensed under Creative Commons License
' http://creativecommons.org/licenses/by-sa/3.0/
'
' You are free to use this code in any application,
' provided this notice is left unchanged.
'
' rev  date                          brief descripton
' 1.0  2010-09-13                    Returns the position (right-most) of an occurrence of one string within another, from the end of string. Eg: InStrRev("abc-hijklm-no","-") renders 3
'
Public Function InStrRev(sText As String, sFindStr As String) As Long
'AUTHOR:
'   Mark Davis (aka: Cybercow), Microsoft Access MVP
'   Eg: InStrRev("abc-hijklm-no","-") renders 3
Dim nFindStrLen As Long, X As Long, Z As Long

   InStrRev = 0
   Z = 0
   nFindStrLen = Len(sFindStr)
   For X = Len(sText) To 1 Step -1
   Z = Z + 1
   If mid$(sText, X, nFindStrLen) = sFindStr Then
       InStrRev = Z
       Exit For
   End If
   Next X

End Function

Creative Commons License
InStrRev by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.


Synopsis

Yet another and small flavor of an InStrRev function for older versions of Access (Access 97 and before). This one has a 'curve' to it, in that instead of returning the positional value of the first found string from the end (starting from the right side of the source string), it returns the first positional value of string to be found, but from the left-most position as it counted from the end.

CODE
' InStrRev
' http://www.utteraccess.com/wiki/InStrRev
' Code courtesy of UtterAccess Wiki
' Licensed under Creative Commons License
' http://creativecommons.org/licenses/by-sa/3.0/
'
' You are free to use this code in any application,
' provided this notice is left unchanged.
'
' rev  date                          brief descripton
' 1.0  2010-09-13                    Returns the first (left-most) position of an occurrence of one string within another, from the end of string.  Eg: InStrR("abc-hijkl-mno","-",1) renders 10
'
Function InStrR(ByVal sTarg As String, ByVal sFind As String, ByVal iComp As Long) As Long
'AUTHOR:
'   Mark Davis (aka: Cybercow), Microsoft Access MVP
'   Eg: InStrR("abc-hijkl-mno","-",1) renders 10
Dim p As Long, lastP As Long

   p = InStr(1, sTarg, sFind, iComp)
   Do While p
       lastP = p
       p = InStr(lastP + 1, sTarg, sFind, iComp)
   Loop
   InStrR = lastP

End Function

Creative Commons License
InStrRev by UtterAccess Wiki is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Editing and revision of the content is freely encouraged; for details, see Expected Usage.

Edit Discussion
Custom Search
Thank you for your support!
This page has been accessed 246,272 times.  This page was last modified 08:44, 6 April 2011 by Jack Leach. Contributions by Cpetermann, Ydecavoqex, Mark Davis and tvanstiphout  Disclaimers