Instrrev code

  • Thread starter Thread starter John
  • Start date Start date
John said:
Hi

Has anyone written instrrev function for access97 or before?

Thanks

Regards

This was originally written by John Viescas (all bow down!) and has been
revised slightly by yours truly.

'----- 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 -----
 
For the purists..

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

InStrRev = 0 ' Not neccesarry but clarifies ...

' Do some initial checks
If (Compare < 0) Or (Compare 2) Then
Err.Raise 5
Exit Function
End If
If Len(StringMatch) = 0 Then
InStrRev = Start
Exit Function
End If
If Len(StringCheck) = 0 or Start > Len(StringCheck) or Len(StringMatch)
Len(StringCheck) Then
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
 
Back
Top