Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

reading on textbox

Posted on 2011-09-07
9
Medium Priority
?
307 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 14

Accepted Solution

by:
VBClassicGuy earned 668 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 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 668 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 664 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 86

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…
Suggested Courses

670 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