Need a working sample code for StrReverse and InStr

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imrosie
    New Member
    • May 2007
    • 222

    Need a working sample code for StrReverse and InStr

    Hello,

    I don't completely understand the working of these functions, but it's been suggested that these will give me what I need.
    I have a database that pulls in image files (stores the absolute path in database). The image shows up in my subform. The file name is displayed in a form control called 'image description'. The problem is it displays the entire link, such as c:\imageCats\Gi zmotheCat.jpg (absolute path)....

    End user cah 'browse' to any folder they may have stored pictures to retrieve an image. So the absolute path will change with each end-user. I don't want to hardcode a specific path. However, I do need the absolute path to an image stored and only display in the control a name, i.e. GizmotheCat.

    I'm looking for real working example code for StrReverse (InStr), so I can somehow reverse the string, cut off the 'GizmotheCat.jp g and display only the GizmotheCat in this control. I also like the .jpg to go into another control box. I've received some examples, but can't get them working in my form.

    Does anyone have something like this? Thanks in advance.

    Rosie
  • MMcCarthy
    Recognized Expert MVP
    • Aug 2006
    • 14387

    #2
    This function should do what you want.

    [CODE=vb]
    Function getFName(path As String) As String
    Dim fName As String
    Dim pos1 As Integer
    Dim pos2 As Integer
    Dim tmpPos As Integer
    pos1 = 1
    Do Until (InStr(pos1, path, "\") = 0)
    tmpPos = InStr(pos1, path, "\")
    pos1 = tmpPos + 1
    Loop
    pos2 = InStr(path, ".") - pos1
    fName = Mid(path, pos1, pos2)
    getFName = fName

    End Function
    [/CODE]

    Comment

    • ADezii
      Recognized Expert Expert
      • Apr 2006
      • 8834

      #3
      Originally posted by imrosie
      Hello,

      I don't completely understand the working of these functions, but it's been suggested that these will give me what I need.
      I have a database that pulls in image files (stores the absolute path in database). The image shows up in my subform. The file name is displayed in a form control called 'image description'. The problem is it displays the entire link, such as c:\imageCats\Gi zmotheCat.jpg (absolute path)....

      End user cah 'browse' to any folder they may have stored pictures to retrieve an image. So the absolute path will change with each end-user. I don't want to hardcode a specific path. However, I do need the absolute path to an image stored and only display in the control a name, i.e. GizmotheCat.

      I'm looking for real working example code for StrReverse (InStr), so I can somehow reverse the string, cut off the 'GizmotheCat.jp g and display only the GizmotheCat in this control. I also like the .jpg to go into another control box. I've received some examples, but can't get them working in my form.

      Does anyone have something like this? Thanks in advance.

      Rosie
      Rosie, Rosie, I thought we covered all this?

      Comment

      • FishVal
        Recognized Expert Specialist
        • Jun 2007
        • 2656

        #4
        Originally posted by imrosie
        Hello,

        I don't completely understand the working of these functions, but it's been suggested that these will give me what I need.
        I have a database that pulls in image files (stores the absolute path in database). The image shows up in my subform. The file name is displayed in a form control called 'image description'. The problem is it displays the entire link, such as c:\imageCats\Gi zmotheCat.jpg (absolute path)....

        End user cah 'browse' to any folder they may have stored pictures to retrieve an image. So the absolute path will change with each end-user. I don't want to hardcode a specific path. However, I do need the absolute path to an image stored and only display in the control a name, i.e. GizmotheCat.

        I'm looking for real working example code for StrReverse (InStr), so I can somehow reverse the string, cut off the 'GizmotheCat.jp g and display only the GizmotheCat in this control. I also like the .jpg to go into another control box. I've received some examples, but can't get them working in my form.

        Does anyone have something like this? Thanks in advance.

        Rosie
        Hi, Rosie.

        Now I've got how irrelevant was my advice to use custom class. Remember? ;)

        Here I'm suggesting you the following solution.

        1. Put the code below to a public module.

        [CODE=vb]

        Public Function ExtractFileExt( varFullPath As Variant) As String
        If IsNull(varFullP ath) Then Exit Function
        ExtractFileExt = StrReverse(Left (StrReverse(var FullPath), _
        InStr(1, StrReverse(varF ullPath), ".")))
        End Function

        Public Function ExtractFileName (varFullPath As Variant) As String
        If IsNull(varFullP ath) Then Exit Function
        ExtractFileName = StrReverse(Mid( StrReverse(varF ullPath), _
        InStr(1, StrReverse(varF ullPath), ".") + 1, _
        InStr(1, StrReverse(varF ullPath), "\") - _
        InStr(1, StrReverse(varF ullPath), ".") - 1))
        End Function

        [/CODE]

        Specially for you 4 "Instr"s and 8 "StrReverse "s. ;)

        The following assumes your form field with picture full path has name "txtPictureFull Path".

        2. Set DataSource of the field displaying file extension to
        =ExtractFileExt ([txtPictureFullP ath])

        3. Set DataSource of the field displaying file name to
        =ExtractFileNam e([txtPictureFullP ath])

        Hope that this will help you.

        Goooooogluck.

        Comment

        • imrosie
          New Member
          • May 2007
          • 222

          #5
          Originally posted by ADezii
          Rosie, Rosie, I thought we covered all this?

          We did and it's working fine...I posted this days ago and I don't know how to close the discussion other than to tell everyone, that I've got a working solution.

          Thanks ADezii. take care

          Rosie

          Comment

          • imrosie
            New Member
            • May 2007
            • 222

            #6
            Originally posted by FishVal
            Hi, Rosie.

            Now I've got how irrelevant was my advice to use custom class. Remember? ;)

            Here I'm suggesting you the following solution.

            1. Put the code below to a public module.

            [CODE=vb]

            Public Function ExtractFileExt( varFullPath As Variant) As String
            If IsNull(varFullP ath) Then Exit Function
            ExtractFileExt = StrReverse(Left (StrReverse(var FullPath), _
            InStr(1, StrReverse(varF ullPath), ".")))
            End Function

            Public Function ExtractFileName (varFullPath As Variant) As String
            If IsNull(varFullP ath) Then Exit Function
            ExtractFileName = StrReverse(Mid( StrReverse(varF ullPath), _
            InStr(1, StrReverse(varF ullPath), ".") + 1, _
            InStr(1, StrReverse(varF ullPath), "\") - _
            InStr(1, StrReverse(varF ullPath), ".") - 1))
            End Function

            [/CODE]

            Specially for you 4 "Instr"s and 8 "StrReverse "s. ;)

            The following assumes your form field with picture full path has name "txtPictureFull Path".

            2. Set DataSource of the field displaying file extension to
            =ExtractFileExt ([txtPictureFullP ath])

            3. Set DataSource of the field displaying file name to
            =ExtractFileNam e([txtPictureFullP ath])

            Hope that this will help you.

            Goooooogluck.
            Thanks so much FishVal....I will test this out and if it works, I'll use it in a future project. ADezii gave me one that I've got working. so thanks again. I'll post back to let you know if it also fixed my issue. thanks and take care.

            Comment

            • NeoPa
              Recognized Expert Moderator MVP
              • Oct 2006
              • 32633

              #7
              Rosie, In future, simply reply to the thread that you have a solution. That will let everyone know that further solutions are not required. I will not close the thread, as it seems obvious now that no further advice is required anyway.

              Thanks to our expert MissingLinq for bringing this to my attention.

              Comment

              • ADezii
                Recognized Expert Expert
                • Apr 2006
                • 8834

                #8
                Originally posted by imrosie
                We did and it's working fine...I posted this days ago and I don't know how to close the discussion other than to tell everyone, that I've got a working solution.

                Thanks ADezii. take care

                Rosie
                It was a pleasure working with you.

                Comment

                Working...