Solved

reading on textbox

Posted on 2011-09-07
9
302 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
ID: 36496844
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
ID: 36496906
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
ID: 36497242
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:Brook Braswell
ID: 36497254
ahh....sorry...
day late and dollar short again :(
0
 
LVL 14

Expert Comment

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

Author Closing Comment

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

Author Comment

by:Whing Dela Cruz
ID: 36499784
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
ID: 36499815
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
ID: 36499924
thx Idle_Mind, Its working... More power!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

685 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