[RESOLVED] Is there something in brackets?
Hello!
I need some help again! :wave:
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? :confused:
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
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' " :(
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.
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
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. :sick:
Thanks for help Mxjerrett and NickThissen!
RESOLVED! :thumb:
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' " :(
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
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. :thumb: