Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Copy first 5 words of textbox into another textbox 4

Status
Not open for further replies.

Ergo30

IS-IT--Management
Jun 22, 2007
10
GB
Hi

I need help again. I am trying to get the code to be able to read the first 5 words from a text box and copy them into another textbox.

Please help

Cheers

Simon
 
Use the Split function. The Split function splits a string into an array, using a supplied character as a delimiter. In your case, use space. Use the resulting array to populate your second text box.
 
Bob

Thanks for replying so quick, I am pretty fresh to the visual basic thing, would you mind putting some code up to demonstrate what you mean.

Thanks

Simon
 




Code:
dim a, s as string
s = " "
a = split(textbox1.text, s)
for i = 0 to 4
  Textbox2.text = textbox2.text & a(i) & s
next
textbox2.text = trim(textbox2.text)


Skip,

[glasses] [red][/red]
[tongue]
 
Skip

Many thanks for the code, the only problem is when using your example with just two text boxes I get an error on the s = " " part, it returns a Complie error: 'Invalid outside procedure' message.

It may be better to just pick out the text up to the first full stop. any chance of adjusting the code for this.

Many Thanks
 
Try the following code.
___
[tt]
Dim v As Variant
v = Split(Text1.Text)
ReDim Preserve v(4)
Text2.Text Join(v)[/tt]
___

>It may be better to just pick out the text up to the first full stop.

This is a different requirement which you stated in your first post. Can you explain what are you trying to do?

You can get the first sentence (up to the first full-stop) using the following code.

[tt]Text2.Text = Split(Text1.Text, ".")(0)[/tt]
 
and a sentance could end with a fullstop, an exclamation mark or a question mark...

 
Hypetia

What I am trying to do is to capture the first part of text from a text box and get it to appear in either a label or another textbox. The textbox that you type in will be the description of the a problem, the first sentence will be a brief description of the problem. I want this first sentence to be replicated in either a label or textbox at the top of the page giving people who receive the word document a one liner decription of the problem

I could just get people to copy paste the sentence into the second text box/label but I am trying to make this as easy a possible for people to fill in. I hope someone can help as I cannot get the text to fill out another label/textbox without having to click a button, I want it to happen as the people type in a textbox so that the text transposes up to the label/textbox.

I really hope this makes some sense.

the 'Split' method would be great and does work but I have to allocate a button to get it to copy the text into the label. I need this to happen as I type the text into the first text box.

Cheers

Simon
 
Simon,

This should put the first five words into another text box (or if you would like a label). However, you would have to add more code to stop processing when you get your five words. Then another flag would have to detect when you are finished with this part and are ready to submit another record.


Code:
Private Sub Textbox1_Change()
   
   Dim cWords() as String
   
   cWords = Split(TextBox1.Text, " ")
   
   If (uBound(cWords) >= 4) Then
      
      TextBox2.Text = cWords(0)
      
      For i = 1 To 4
         
         TextBox2.Text = TextBox2.Text & " " & cWords(i)
         
      Next
      
   End If
   
End Sub

This will work for one record. To enter another record the user would have to clear TextBox1 first then input the record. This may flash TextBox2 if more than five words are inputted. But the inclusion of a flag could prevent that.


HyperEngineer
If it ain't broke, it probably needs improvement.
 
I'd probably call this from the KeyUp event

Public Function FirstWords(strSource As String, lWordCount As Long) As String
With CreateObject("vbscript.regexp")
.Pattern = "(\S+(\s|$)){1," & lWordCount & "}"
If .Test(strSource) Then
FirstWords = Trim$(.Execute(strSource)(0))
End If
End With
End Function
 
strongm, i am probably showing off my ignorance with regexp here, but how does your code get the first five words?

 
1) It matches a block of non-whitespace character followed by a whitespace character or by the end of the string:

(\S+(\s|$))

2) It must find this match occurring a minimum of 1 time and a maximum of lWordCount times:

{1," & lWordCount & "}"

What this means in English is that the RegExp is looking for up to the first 5 'words' of the input string. When we execute the RegExp the match is returned as Item1 (index position 0) of a MatchCollection, so this is what we return:

.Execute(strSource)(0)

We Trim this because the regExp returns any trailing space




 
Very nice, strongm, much better than my solution. A couple of observations that might be helpful to some:

<(\S+(\s|$))

If you're in the habit of putting double spaces at the end of sentences (e. g. "I think. Therefore I am."), you'll need to add a plus (or star) after the lower case s to get words in all but the first sentence to match the pattern: [tt](\S+(\s+|$))[/tt]

<When we execute the RegExp the match is returned as Item1 (index position 0) of a MatchCollection..

The point is that since many regular expression patterns have multiple matches to the input string, there has to be a way to return multiple matches. The Execute method returns each match as a separate item in a MatchCollection object (see for more info). In this case, there is by definition only one match for this pattern (all you're doing is returning the first n words in a string, as a new string), so strongm is simply referencing the first item in the MatchCollection.

Bob
 
I've got a slightly more obscure solution, if we're prepared to consider using a RichTextBox ...
 
Well, that sounds like even more fun. Will you publish it?
 
Ok, along with the RichTextBox you'll need to add a reference to TOM (if not listed as an available reference browse to riched20.dll).

Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Private Const WM_USER = &H400&
Private Const EM_GETOLEINTERFACE = (WM_USER + 60)

Private Function GetFirstWords(rtbText As RichTextBox, lWordCount As Long) As String
Dim myIUnknown As IUnknown '
Dim TextRange As ITextRange
Dim tomDoc As ITextDocument


SendMessage rtbText.hwnd, EM_GETOLEINTERFACE, 0&, myIUnknown
Set tomDoc = myIUnknown
Set TextRange = tomDoc.Range(0, 1024) ' should be more than sufficient to pick up first 5 words
TextRange.End = 0
TextRange.MoveEnd tomWord, lWordCount

GetFirstWords = TextRange
End Function
 
Ok, I've done a little reading and I see what you're up to. Very interesting! I never knew that TOM had all that functionality.

I'm guessing that you started with the idea that the MoveEnd method recognized "word" as a unit, and everything proceeded from that.

A couple of questions, though. What's the point of the WM_USER? Why can't you just define EM_GETOLEINTERFACE as &h43C? Second, why do you think that VB didn't directly implement TOM as part of the RTB control's direct interface, instead requiring you to go through the SendMessage hoops to get the IRichEditOle pointer?

Bob
 
>I never knew that TOM had all that functionality.

I keep banging away about it in this forum ...

>What's the point of the WM_USER?

Just an old habit that I sometimes take up again ...

>why do you think that VB didn't directly implement TOM as part of the RTB

Legacy. The richText control that VB supports is version 1.0. It has NEVER been upgraded. However, the richtext control on the OS has been upgraded several times, and has to actually contain an emulation of version 1 so that VB's control continues to work.

What we're doing is simply cheating by going underneath the covers to get access to the later control and thus interfaces that VB doesn't even know about ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top