InStr right to left

Discussion in 'Microsoft Access VBA Modules' started by Guest, May 13, 2005.

  1. Guest

    Guest 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
     
    Guest, May 13, 2005
    #1
    1. Advertisements

  2. Guest

    Guest Guest

    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.

    "Brad" wrote:

    > 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
     
    Guest, May 13, 2005
    #2
    1. Advertisements

  3. Guest

    Alex Dybenko Guest

    There is a InStrRev() function
    Returns the position of an occurrence of one string within another, from the
    end of string.

    --
    Alex Dybenko (MVP)
    http://Alex.Dybenko.com
    http://www.PointLtd.com


    "Brad" <> wrote in message
    news:...
    > 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
     
    Alex Dybenko, May 13, 2005
    #3
  4. Guest

    Guest Guest

    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

    "Dennis" wrote:

    > 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.
    >
    > "Brad" wrote:
    >
    > > 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
     
    Guest, May 13, 2005
    #4
  5. Guest

    Guest Guest

    Hi Dennis,

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

    Brad

    "Dennis" wrote:

    > 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.
    >
    > "Brad" wrote:
    >
    > > 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
     
    Guest, May 13, 2005
    #5
  6. Guest

    Dirk Goldgar Guest

    "Brad" <> wrote in message
    news:
    > Hi Dennis,
    >
    > I tried it... is that available in Access 97?
    >
    > Brad
    >
    > "Dennis" wrote:
    >
    >> 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.


    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 -----

    --
    Dirk Goldgar, MS Access MVP
    www.datagnostics.com

    (please reply to the newsgroup)
     
    Dirk Goldgar, May 13, 2005
    #6
  7. Guest

    Guest Guest

    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

    "Dirk Goldgar" wrote:

    > "Brad" <> wrote in message
    > news:
    > > Hi Dennis,
    > >
    > > I tried it... is that available in Access 97?
    > >
    > > Brad
    > >
    > > "Dennis" wrote:
    > >
    > >> 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.

    >
    > 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 -----
    >
    > --
    > Dirk Goldgar, MS Access MVP
    > www.datagnostics.com
    >
    > (please reply to the newsgroup)
    >
    >
    >
     
    Guest, May 13, 2005
    #7
  8. Guest

    Dirk Goldgar Guest

    "Brad" <> wrote in message
    news:
    > 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)

    --
    Dirk Goldgar, MS Access MVP
    www.datagnostics.com

    (please reply to the newsgroup)
     
    Dirk Goldgar, May 13, 2005
    #8
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JohnV

    InStr Function

    JohnV, Aug 11, 2003, in forum: Microsoft Access VBA Modules
    Replies:
    2
    Views:
    172
    JohnV
    Aug 11, 2003
  2. David

    INSTR statement question

    David, Oct 13, 2003, in forum: Microsoft Access VBA Modules
    Replies:
    3
    Views:
    510
    Cheryl Fischer
    Oct 13, 2003
  3. JC

    InStr Function

    JC, Oct 25, 2003, in forum: Microsoft Access VBA Modules
    Replies:
    11
    Views:
    293
    Gary Schuldt
    Oct 29, 2003
  4. Rachel

    InStr Function for Access 97

    Rachel, Nov 6, 2003, in forum: Microsoft Access VBA Modules
    Replies:
    3
    Views:
    999
    Dirk Goldgar
    Nov 6, 2003
  5. SurveyorinVA via AccessMonster.com

    Right Function with InStr

    SurveyorinVA via AccessMonster.com, Oct 25, 2007, in forum: Microsoft Access VBA Modules
    Replies:
    2
    Views:
    506
    tobesurveyor via AccessMonster.com
    Oct 26, 2007
Loading...

Share This Page