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
Solved

reading on textbox

Posted on 2011-09-07
9
301 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
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…
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 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…

856 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