Results 1 to 9 of 9

Thread: [RESOLVED] Is there something in brackets?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Resolved [RESOLVED] Is there something in brackets?

    Hello!
    I need some help again!

    1. I have a text and I want to check is there an word or words in [brackets] in that text.
    2. And if there is something in brackets then need to display it in TextBox.

    So maybe someone have some good ideas how to do this?
    Last edited by Lauriux1; Sep 21st, 2007 at 03:35 PM.

  2. #2
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: Is there something in brackets?

    Here is a little function that should work.

    Code:
    Public Function GetText(Text As String) As String
        Dim lngStart As Long
        lngStart = InStr(1, Text, "[")
        GetText = Mid(Text, lngStart + 1, InStr(lngStart, Text, "]") - lngStart - 1)
    End Function

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Re: Is there something in brackets?

    It works fine and almost exactly like I want.
    Code:
    Private Sub Command1_Click()
    Text2.Text = GetText(Text1.Text)
    End Sub
    
    Public Function GetText(Text As String) As String
        Dim lngStart As Long
        lngStart = InStr(1, Text, "[")
        GetText = Mid(Text, lngStart + 1, InStr(lngStart, Text, "]") - lngStart - 1)
    End Function
    But there is one problem if TextBox is empty or there is no brackets then I'm geting "Run-time error '5' "

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Is there something in brackets?

    Just do two checks before calling the function 'GetText'.

    Tip:
    - Check if textbox is empty (Use If/Then)
    - Check if there are brackets (Use the 'InStr' command and see if it returns a value > 0)

    You should be able to figure it out.

  5. #5
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: Is there something in brackets?

    Here it is with basic error handling.

    Code:
    Public Function GetText(Text As String) As String
        Dim lngStart As Long
    If Trim(Text) = "" Then MsgBox ("Error: No text."), vbInformation, "Error": Exit Function
    If InStr(1, Text, "[") = 0 Or InStr(1, Text, "]") = 0 Then MsgBox ("Error: No text in brackets."), vbInformation, "Error": Exit Function
        lngStart = InStr(1, Text, "[")
        GetText = Mid(Text, lngStart + 1, InStr(lngStart, Text, "]") - lngStart - 1)
    End Function

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Re: Is there something in brackets?

    Quote Originally Posted by NickThissen
    Just do two checks before calling the function 'GetText'.

    Tip:
    - Check if textbox is empty (Use If/Then)
    - Check if there are brackets (Use the 'InStr' command and see if it returns a value > 0)

    You should be able to figure it out.
    Ohhh!!! Yes!!! Thanks!!!
    Sorry for stupid question because I'm very tired now.

    Thanks for help Mxjerrett and NickThissen!

    RESOLVED!

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Re: Is there something in brackets?

    Quote Originally Posted by Mxjerrett
    Here it is with basic error handling.

    Code:
    Public Function GetText(Text As String) As String
        Dim lngStart As Long
    If Trim(Text) = "" Then MsgBox ("Error: No text."), vbInformation, "Error": Exit Function
    If InStr(1, Text, "[") = 0 Or InStr(1, Text, "]") = 0 Then MsgBox ("Error: No text in brackets."), vbInformation, "Error": Exit Function
        lngStart = InStr(1, Text, "[")
        GetText = Mid(Text, lngStart + 1, InStr(lngStart, Text, "]") - lngStart - 1)
    End Function
    Nice! But if TextBox = "][" then we will get "Run-time error '5' "

  8. #8
    Fanatic Member Mxjerrett's Avatar
    Join Date
    Apr 2006
    Location
    Oklahoma
    Posts
    939

    Re: [RESOLVED] Is there something in brackets?

    Here is some more slightly better error handling

    Code:
    Public Function GetText(Text As String) As String
    On Error GoTo Err
        Dim lngStart As Long
    If Trim(Text) = "" Then MsgBox ("Error: No text."), vbInformation, "Error": Exit Function
    If InStr(1, Text, "[") = 0 Or InStr(1, Text, "]") = 0 Then MsgBox ("Error: No text in brackets."), vbInformation, "Error": Exit Function
        lngStart = InStr(1, Text, "[")
        GetText = Mid(Text, lngStart + 1, InStr(lngStart, Text, "]") - lngStart - 1)
    Exit Function
    Err:
        MsgBox ("An unplanned error has triggered. Please check the format of your text"), vbInformation, "Error"
    End Function

    If a post has been helpful please rate it.
    If your question has been answered, pull down the tread tools and mark it as resolved.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    May 2007
    Posts
    278

    Re: [RESOLVED] Is there something in brackets?

    Quote Originally Posted by Mxjerrett
    Here is some more slightly better error handling

    Code:
    Public Function GetText(Text As String) As String
    On Error GoTo Err
        Dim lngStart As Long
    If Trim(Text) = "" Then MsgBox ("Error: No text."), vbInformation, "Error": Exit Function
    If InStr(1, Text, "[") = 0 Or InStr(1, Text, "]") = 0 Then MsgBox ("Error: No text in brackets."), vbInformation, "Error": Exit Function
        lngStart = InStr(1, Text, "[")
        GetText = Mid(Text, lngStart + 1, InStr(lngStart, Text, "]") - lngStart - 1)
    Exit Function
    Err:
        MsgBox ("An unplanned error has triggered. Please check the format of your text"), vbInformation, "Error"
    End Function
    Not bad idea!
    I think it will be useful for me.

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