|
-
Sep 21st, 2007, 02:45 PM
#1
Thread Starter
Hyperactive Member
-
Sep 21st, 2007, 02:59 PM
#2
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.
-
Sep 21st, 2007, 03:17 PM
#3
Thread Starter
Hyperactive Member
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' "
-
Sep 21st, 2007, 03:21 PM
#4
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.
-
Sep 21st, 2007, 03:28 PM
#5
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.
-
Sep 21st, 2007, 03:34 PM
#6
Thread Starter
Hyperactive Member
Re: Is there something in brackets?
 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!
-
Sep 21st, 2007, 03:40 PM
#7
Thread Starter
Hyperactive Member
Re: Is there something in brackets?
 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' "
-
Sep 21st, 2007, 03:43 PM
#8
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.
-
Sep 21st, 2007, 03:50 PM
#9
Thread Starter
Hyperactive Member
Re: [RESOLVED] Is there something in brackets?
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|