Results 1 to 4 of 4

Thread: please help newbie - SIMPLE Trim/Left question...

  1. #1
    Guest

    Wink

    Can someone please help me? I have a Label1.Caption that is set = to a textbox (TextBox1) on my form. The value in the textbox is "c:\myfiles\The Matrix.doc" I want to set the value of my Label1.Caption equal to just "The Matrix". I think I can do something like this....

    label1.Caption = Left(TextBox1, "/") and then just trim off the ".doc" and the file path info?

    Thanks in advance for any help!!!

  2. #2
    Frenzied Member HarryW's Avatar
    Join Date
    Jan 2000
    Location
    Heiho no michi
    Posts
    1,827
    Try this:

    Label1.Caption = Mid(Text1.Text, InStrRev(Text1.Text, "\"), Instr(Text1.Text, ".") - InStrRev(Text1.Text, "\"))

    That should pick out any text between the last backslash and the first dot (full-stop character). I've not used InStrRev before so it might not quite work the way I think it does. Try it anyway.
    Harry.

    "From one thing, know ten thousand things."

  3. #3
    Hyperactive Member
    Join Date
    Jun 2000
    Location
    Auckland, NZ
    Posts
    411

    Small correction to Harry's

    It was out by a little bit.

    Code:
    Label1.Caption = Mid(Text1.Text, InStrRev(Text1.Text, "\") + 1, Instr(Text1.Text, ".") - InStrRev(Text1.Text, "\") -1 )
    HarryW's solution is a nice one line answer for you especially if the text is always going to be formatted correctly etc. Remember though, that filenames do not always have an extension so there may not be any dot in the name.

    This alternative (and long winded) method will do the job but should not fail in any circumstance.

    Code:
    Option Explicit
    
    Private Sub Command1_Click()
      Label1.Caption = GetText(Text1)
    End Sub
    
    
    Public Function GetText(t As String) As String
    ' extracs text between the last "\" character and the first "." character following the first "\"
      Dim firstDot As Integer
      Dim lastSlash As Integer
      
      'get the last slash
      lastSlash = InStrRev(t, "\")
      If lastSlash = 0 Or lastSlash = Len(t) Then
        ' there are no slashes at all or the last slash
        ' is at the end of the string so bail out
        Exit Function
      End If
      
      ' get the first dot after the last slash
      firstDot = InStrRev(Mid(t, lastSlash + 1), ".")
      If firstDot = 0 Then
        GetText = Mid(t, lastSlash + 1, Len(t) - 1 - lastSlash)
      Else
        GetText = Mid(t, lastSlash + 1, firstDot - 1)
      End If
    End Function
    Cheers
    Paul Lewis

  4. #4
    Guest

    Thanks!

    Actually it worked like a charm!! THANKS! I just added in a -1 in a couple places.....thanks again!

    Mid(txtFile, InStrRev(txtFile, "\") + 1, InStr(txtFile, ".") - InStrRev(txtFile, "\") - 1)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width