Solved

reading on textbox

Posted on 2011-09-07
9
298 Views
Last Modified: 2012-08-13
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...
0
Comment
Question by:Whing Dela Cruz
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 14

Accepted Solution

by:
VBClassicGuy earned 167 total points
Comment Utility
How about replacing:

   If UBound(astrSplitItems) >= 2 Then
      MsgBox astrSplitItems(0)
      MsgBox astrSplitItems(1)
      MsgBox astrSplitItems(2)
   End If

 with:

For i = 0 to UBound(astrSplitItems) - 1
   MsgBox astrSplitItems(i)
Next i
0
 
LVL 85

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 167 total points
Comment Utility
Get rid of the "- 1" part though!  ;)

*UBound() returns the literal upper bound of the array and is included in the indices that can be accessed.
0
 
LVL 14

Assisted Solution

by:Brook Braswell
Brook Braswell earned 166 total points
Comment Utility
Why not try this instead...
Private Sub Command1_Click()
   Dim astrSplitItems() As String
   dim i as single
   astrSplitItems = Split(Replace(Text1.Text, "  ", " "), " ")
   
   For i = 0 to UBound(astrSplitItems)
      msgbox astrSplitItems(i)
   Next
End Sub

Open in new window

0
 
LVL 14

Expert Comment

by:Brook Braswell
Comment Utility
ahh....sorry...
day late and dollar short again :(
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 14

Expert Comment

by:VBClassicGuy
Comment Utility
Oh yeah, forgot the -1 is not needed...my bad. Thx for the correction.
0
 

Author Closing Comment

by:Whing Dela Cruz
Comment Utility
Thanks a lot ! its working... More power to all of you...
0
 

Author Comment

by:Whing Dela Cruz
Comment Utility
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
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
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

0
 

Author Comment

by:Whing Dela Cruz
Comment Utility
thx Idle_Mind, Its working... More power!
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now