How many words?

jim04

New member
Local time
Yesterday, 16:02
Joined
Mar 10, 2008
Messages
7
Hello

I have a database with some texts.
i)How can i compute how many word i have in each text?
ii) How can i find the frequency of some specifically words?
 
Simple Software Solutions

In answer to your first question. Microsoft and other popular products use the analogy that the average number of characters in a word is 5. So if you divide the lenght of the string by 5 this will give aballpark figure, not an exact.

If you want to know the exact number you would have to loop throught the string for the occurances of a space

For x = 1 to Len(MyText)

If mid(mytext,x,1) = " " then

WordCound = WordCount + 1

Endif

Next


Bit crude but it will give you what you want. BTW what is the purpose of the exercise?

CodeMaster::cool:
 
DCrake where will i write this code?
 
For x = 1 to Len(MyText)

If mid(mytext,x,1) = " " then

WordCound = WordCount + 1

Endif

Next


Bit crude but it will give you what you want. BTW what is the purpose of the exercise?

CodeMaster::cool:

Where will i write this code?
 
If you want to know the exact number you would have to loop throught the string for the occurances of a space

CodeMaster::cool:
You don't have to loop through a string at all.

How about this?

Code:
Public Function CountWordsInString(ByVal strText As String) As Integer
    Dim astrWords() As String
    astrWords() = Split(strText, " ")
    CountWordsInString = (UBound(astrWords()) - LBound(astrWords())) + 1
End Function ' CountWordsInString
 
Last edited:
Simple Software Solutions

This depends on where you want to use it.

If you are simply using it to display on the form underneath a large textbox for example then you would use it on the say the got focus/lost focus of a textbox to pass the information to a label.

EG:

Me.LabelInfo = CountWords(Me.TextBox)

Where CountWords is a Function Created as follows:

Public Function CountWords(AnyString As String) As String

Dim WordCount As Integer

For x = 1 to Len(AnyString)

If Mid(AnyString,x,1) = " " Then

WordCound = WordCount + 1

Endif



Next

CountWords = CStr(WordCount) & " word(s)"

End Function

OR:

If you wanted the user to click on a button to find out the number of words in a textbox then on the OnClick of the button

MsgBox "There are " & Countwords(Me.TextBox) & " in this field."

OR:
If you wanted to use it a query the one of your columns would be

Words:Countwords([FieldName])

As long as you refer to the function correctly you should be able to use it anywhere.


CodeMaster::cool:
 
First of all thanks for helping me.
When i asked "where will i write this code?" i meant in a cell? in sql view? where?
 
A very neat clever solution Mile-O, but can I ask a question for my own education.
I thought that Lbound for the one dimensional array returned by the Split would always be 0 therefore
CountWordsInString = (UBound(astrWords())+ 1
would give the answer.
As I said I ask for educational not nit picking reasons as I wouldn't even have thought of that solution.

Brian
 
I thought that Lbound for the one dimensional array returned by the Split would always be 0 therefore
CountWordsInString = (UBound(astrWords())+ 1

Ha, you are right. I'm doing something in Excel just now based around start and end dates and that's the way my head's thinking is wired (end date-start date).

But, yes, UBound() + 1 should do the trick:

Amended:
Code:
Public Function CountWordsInString(ByVal strText As String) As Integer
    Dim astrWords() As String
    astrWords() = Split(strText, " ")
    CountWordsInString = UBound(astrWords()) + 1
End Function ' CountWordsInString


Jim,

You go to the modules tab, create a new module, and then copy and paste the function in to it. Then, to use it, you do a DCrake has mentioned in one of his posts above.
 
When you first open your db the first screen lists objects such as tables queries etc one of the etc is modules, click on it, then on the next screen click new and then paste the code into the left hand pane, it should all be clear by now, close this and you have a function which you can use in say a query by
countofwords: CountWordsInString(yourfieldname)
Don't name the module the same as the function, just leave it to default to module1 for now.

Brian
 
Jim,

This.....countofwords: CountWordsInString(yourfieldname)

is creating a new calculated filed in a query. When the query is opened or a form based on the query is opened then the created field countofwords will display the word count for each record based on the field that has the words you wish to count.

Whatever criteria you use won't matter as that will just determine which records the query displays.
 
Hope you are not going shooting tomorrow, or should that be today? :D

Brian
 

Users who are viewing this thread

Back
Top Bottom