Link to home
Start Free TrialLog in
Avatar of Whing Dela Cruz
Whing Dela CruzFlag for Anguilla

asked on

reading on textbox

Hi! I've got a problem when reading my textbox when my group of words is less than Ubound.
the code is like this :

Private Sub Command1_Click()
   Dim astrSplitItems() As String
   astrSplitItems = Split(Replace(Text1.Text, "  ", " "), " ")
   
   If UBound(astrSplitItems) >= 2 Then
      MsgBox astrSplitItems(0)
      MsgBox astrSplitItems(1)
      MsgBox astrSplitItems(2)
   End If
End Sub

The problem is the msgbox cannot read the  Text1.Text if the group of words in the textbox is less than 3 groups. Is there any solution? How? Please...
ASKER CERTIFIED SOLUTION
Avatar of VBClassicGuy
VBClassicGuy
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ahh....sorry...
day late and dollar short again :(
Oh yeah, forgot the -1 is not needed...my bad. Thx for the correction.
Avatar of Whing Dela Cruz

ASKER

Thanks a lot ! its working... More power to all of you...
Hi! I've got an error here; "Subscript out of range" this will appear when the text1.text is less than 3 words... Pls help me more...

 Private Sub Command1_Click()
 Dim icode As String, isign As String, iuse As String
   Dim astrSplitItems() As String
   Dim i As Single
   astrSplitItems = Split(Replace(Text1.Text, "  ", " "), " ")
   
   For i = 0 To UBound(astrSplitItems)
      icode = astrSplitItems(0)
      isign = astrSplitItems(1)
      iuse = astrSplitItems(2)
   Next
   MsgBox icode
   MsgBox isign
   MsgBox iuse
End Sub
If there are less than three words then you will be missing some of the items!  What do you want to do then?...do you want the program to make up a value?  If the user enters invalid input then squawk at them and make them try again!

Your original code had this check:

    If UBound(astrSplitItems) >= 2 Then

which would ensure that the minimum number of words was met.

Why don't you add an Else block on there like this?
Dim icode As String, isign As String, iuse As String
    Dim str As String
    str = Trim(Text1.Text)
    While Instr(str, "  ") > 0
        str = Replace(str, "  ", " ")
    Wend
    astrSplitItems = Split(str, " ")
    If UBound(astrSplitItems) >= 2 Then
        icode = astrSplitItems(0)
        isign = astrSplitItems(1)
        iuse = astrSplitItems(2)

        MsgBox icode & vbCrLf & isign & vbCrLf & iuse

        ' ... other code using your variables here ...

    Else
        MsgBox "Invalid Number of Parameters!  Please enter three words separated by spaces."
    End If

Open in new window

thx Idle_Mind, Its working... More power!