Reverse Instr()

jaydwest

JayW
Local time
Today, 13:30
Joined
Apr 22, 2003
Messages
340
For many years now (When was Access 1.0 Introduced) I have needed to find a character in a string starting with the last character NOT THE FIRST. I have kept the faith and with each new version I have hoped for another parameter in the Instr Function. Something Like

Instr([Start], String1, String2, [Order])

Where Order = acFirst or acLast

I have always just programmed this in code as needed. So today I decided that today is the first day of the rest of my life, and I created a function RinStr to find String2 in String1 from the back. I did it with a For Loop with step -1. This does the job but it's not very elequent and I suspect not very fast.

Does anyone have a better function for this? If anyone is interested in my function (why should you suffer too), I would be happy to share it.

I keep praying the the MS Access Group will add this option in their next version. But then again, I still believe in Santa Claus.

Thanks :mad:
 
Hi Jay -

See if this is of any assistance;
Code:
Function InStrRev(ByVal pStr As String, pItem As String) As Integer
'*******************************************
'Purpose:   Return location of last instance of a character or phrase.
'Inputs:    ? InStrRev("the quick brown fox jumped the lazy dog", "the")
'Output:    28 - Location of last occurence of "the"
'*******************************************

Dim i As Integer, n As Integer, tLen As Integer

    n = 0
    tLen = Len(pItem)
    For i = Len(RTrim(pStr)) To 1 Step -1
    
      If Mid(pStr, i, tLen) = pItem Then
          n = i
          Exit For
      End If
    Next i

    InStrRev = n

End Function

HTH - Bob
 
Is there something wrong with the native InStrRev() function?
 
Yup -- a gaping void if you're using A97. Sorry, should have clarified version.

Bob
 
I wouldn't name any function I created with the name of a VB function. How confusing? For yourself or for anyone else examining the code.
 
Pat -

In retrospect, agree completely. Problem being I originally wrote that prior to the release of A2000.

Best wishes, Bob
 

Users who are viewing this thread

Back
Top Bottom