How do I select N words from a string of words?

I think your standard module function will look something like:
Code:
Option Compare Database
Option Explicit
 
Function SelectWords(InString As String, InCount As Integer) As String
'-- Return a string with InCount words from the InString supplied separated by one space as a delimiter
   Dim MyArray() As String
   Dim x As Integer
 
   SelectWords = ""                       '-- Initialize the return value
   If Len(InString) > 0 Then
      MyArray = Split(InString, " ")
      If UBound(MyArray) > 0 Then      '-- We have some array elements
         For x = 0 To UBound(MyArray) - 1
            SelectWords = Trim(MyArray(x))
            '-- Add a trailing space as a delimiter
            SelectWords = SelectWords & " "
         Next
         '-- Strip off the last space
         SelectWords = left(SelectWords, Len(SelectWords) - 1)
      End If
   End If
 
End Function

Allan,
Yours is the first solution I have started to evaluate but I was immediately confused by the fact that the InCount parameter is not actually used in your code! Being unfamiliar with programming arrays, I have not yet worked out how to fix this....

David
 
Oops! :o I knew I was forgetting something. This is still AIR CODE
Code:
Option Compare Database
Option Explicit

Function SelectWords(InString As String, InCount As Integer) As String
'-- Return a string with InCount words from the InString supplied separated by one space as a delimiter
   Dim MyArray() As String
   Dim x As Integer

   SelectWords = ""                       '-- Initialize the return value
   If Len(InString) > 0 Then
      MyArray = Split(InString, " ")
      If InCount > UBound(MyArray) Then
         '-- Limit the InCount to Max UBound of the Array
         InCount = UBound(MyArray)
      End If
      If (UBound(MyArray) > 0) And (InCount > 0) Then      '-- We have some array elements
         For x = 0 To InCount - 1
            SelectWords = Trim(MyArray(x)) & " "
         Next
         '-- Strip off the last space
         SelectWords = left(SelectWords, Len(SelectWords) - 1)
      End If
   End If

End Function
 
I have now tried all three solutions from RuralGuy, vbaInet and Brianwarnock.

To make RuralGuy's solution select more than one word I had to change the line SelectWords = Trim(MyArray(x)) & " " to the following:
SelectWords = SelectWords & Trim(MyArray(x)) & " "

I also had to add a missing definition for MyArray to Brianwarnock's code.

After these minor corrections, all three solutions worked. At first glance, it would appear that only Brian included code to cope with multiple blank spaces between words. However, I will probably end up using a mixture of the best bits of all three solutions.

Thanks to all of you for your kind assistance.

David
 
Glad you could mix ideas to come to a final solution.

The one thing you could extract from mine is that it handles if the requested number of words is greater than the number of words present. That's in the 3rd IF statement.

Good luck!
 
David, good troubleshooting. You are absolutely correct on my code. It does however deal with multiple spaces by using the Trim() function when getting the word back out of the array.
 
It does however deal with multiple spaces by using the Trim() function when getting the word back out of the array.

Based on my VB.NET experience, I think that having multiple blank spaces in the original string will result in the Split function creating separate 'words' or array members that are simply blank spaces. This would then mess up the count.

BTW, what is the simplest way to test code fragments in Access 2003? I would be checking my assertions before posting here if I didn't find it such a hassle (probably due to gaps in my Access skills).

David
 
Last edited:
That function could have been tested from the immediate window if I was not so lazy. :o If the code is in the class module of a form then you would need to set a breakpoint and run the form.
 
Last edited:
My solution forgot the final Ltrim to remove the leading " " I've noe edited it in. I don't know what happened to the declaration of myarray it was in my original, must have deleted it during tidying up the copy. :o

It was tested for and handled multiple spaces and requesting more words than in the original.

Brian
 

Users who are viewing this thread

Back
Top Bottom