473,386 Members | 1,883 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Count Words in a Field

2
Is it possible to use MS Access to count the number of words
in a data field?
Nov 1 '06 #1

✓ answered by pks00

What if u used the Split command?


Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim sWords() as String
  3.  
  4. sWords = Split(myfield," ")
  5.  
  6. msgbox "Number of words is " & ubound(sWords)
  7.  
  8.  

21 22082
NeoPa
32,556 Expert Mod 16PB
Is it possible to use MS Access to count the number of words
in a data field?
I don't know of a way short of writing a function to process through the data character by character.
Nov 1 '06 #2
southoz
24
Good ay ,
try using the len function to get the length of the string then use a repeating instr function to find all the spaces in the string, that would give u a proper word count

southoz
Nov 1 '06 #3
This function counts the number of words in text. Note it treats hyphenated words as two words. If you don't want this remove the hyphen case from the select function.

Expand|Select|Wrap|Line Numbers
  1. Function WordCount(ByRef Str As String) As Long
  2.  
  3.     Dim cnt As Long
  4.     Dim Words As Long
  5.  
  6.     Words = 0
  7.     WordCount = 0
  8.     If Len(Str) = 0 Then
  9.         Exit Function
  10.     End If
  11.     For cnt = 1 To Len(Str)
  12.         Select Case Mid(Str, cnt, 1)
  13.             Case " "
  14.                 Words = Words + 1
  15.             Case "."
  16.                 Words = Words + 1
  17.             Case ","
  18.                 Words = Words + 1
  19.             Case ";"
  20.                 Words = Words + 1
  21.             Case ":"
  22.                 Words = Words + 1
  23.             Case "-"
  24.                 Words = Words + 1
  25.         End Select
  26.     Next cnt
  27.     If Words = 0 Then Words = 1
  28.     WordCount = Words
  29. End Function
  30.  
Nov 2 '06 #4
Killer42
8,435 Expert 8TB
Just a point - the Instr method, might be slightly more complex to code (though not difficult), but would likely execute faster (except possibly when dealing with very short strings).

Performance would only be an issue, of course, if you are driving it very hard - that is, hitting it for millions of strings, or something.
Nov 2 '06 #5
The instr method would be quicker though more complex. however if the function has to deal with punctuation other than spaces betweeen words then it gets very much more complex and probably would not be much quicker.
Nov 2 '06 #6
NeoPa
32,556 Expert Mod 16PB
Andrew,

Try the Case statement this way for brevity.

-Adrian.
Expand|Select|Wrap|Line Numbers
  1.         Select Case Mid(Str, cnt, 1)
  2.             Case " ", ".", ",", ";", ":", "-"
  3.                 Words = Words + 1
  4.         End Select
Nov 2 '06 #7
Neat, I forgot I can use lists
Nov 2 '06 #8
Killer42
8,435 Expert 8TB
Andrew,
Try the Case statement this way for brevity.
-Adrian.
Expand|Select|Wrap|Line Numbers
  1.         Select Case Mid(Str, cnt, 1)
  2.             Case " ", ".", ",", ";", ":", "-"
  3.                 Words = Words + 1
  4.         End Select
Nice one. However, this will double count in many cases. A full stop, for example (a "period" to you Yanks) is usually going to be followed by at least one space. It might actually produce a more useful count if you don't look for all the punctuation, but just count gaps. For a really accurate count you might need a more complex routine.

The instr method would be quicker though more complex. however if the function has to deal with punctuation other than spaces betweeen words then it gets very much more complex and probably would not be much quicker.
Good point, I didn't really think about that. For just spaces, Instr is almost certainly, as discussed, a little more complex to code and a little faster to execute. For a more thorough search including commas, colons and so on, I would avoid the Instr method.
Nov 2 '06 #9
Damn, I didn't think of the puctuation - space case. I'll have to amend the routine to fix that.

It strikes me that this is a useful function to have in my standard library so its worth the effort to get it right.
Nov 2 '06 #10
NeoPa
32,556 Expert Mod 16PB
I wasn't going to mention it, but if it's an oft-used routine, you want to use a flag which remembers whether you're currently in word mode or between words mode. Hope that helps.
(It's good posting for you guys as you don't have to spell everything out in fine details :) )
Nov 2 '06 #11
pks00
280 Expert 100+
What if u used the Split command?


Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim sWords() as String
  3.  
  4. sWords = Split(myfield," ")
  5.  
  6. msgbox "Number of words is " & ubound(sWords)
  7.  
  8.  
Nov 2 '06 #12
Killer42
8,435 Expert 8TB
What if u used the Split command?
Expand|Select|Wrap|Line Numbers
  1. Dim sWords() as String
  2. sWords = Split(myfield," ")
  3. msgbox "Number of words is " & ubound(sWords)
  4.  
Nice shortcut, that - I like it!
I think the basic question that this thread hinges on is whether you want to take notice of punctuation or not. My feeling would be that to generate a word count you have two basic options.
  • If you are only interested in gaps/spaces to delineate "words" (which would be my recommendation), Split is probably your best bet.
  • If you want to take all sorts of punctuation into account, you probably need to loop through character by character and code something like the Select Case.
Nov 2 '06 #13
NeoPa
32,556 Expert Mod 16PB
Nice one pks00, and/but Killer is spot on too.
Nov 2 '06 #14
Never knew there wuz a slpit function.

However nothing new under the sun. Looking up split on the net resulted in this set of handy routines, including wordcount.

http://j-walk.com/ss/excel/tips/tip93.htm
Nov 3 '06 #15
Killer42
8,435 Expert 8TB
Never knew there wuz a slpit function. However nothing new under the sun. Looking up split on the net resulted in this set of handy routines, including wordcount. http://j-walk.com/ss/excel/tips/tip93.htm
Definitely bookmarked - thanks.
Nov 3 '06 #16
PEB
1,418 Expert 1GB
Very good exemple pks00!

The function that i've use was:

Function word_count(my_sentence, sep) As Integer
Dim result As Integer
Dim my_str
result = 1
Do While InStr(1, my_str, sep) > 0
result = result + 1
my_str = Mid(my_str, InStr(1, my_str, sep) + 1, Len(my_str))
Loop
word_count = result
End Function

But yours it's great!

Best regards!


What if u used the Split command?


Expand|Select|Wrap|Line Numbers
  1.  
  2. Dim sWords() as String
  3.  
  4. sWords = Split(myfield," ")
  5.  
  6. msgbox "Number of words is " & ubound(sWords)
  7.  
  8.  
Nov 4 '06 #17
novel
2
Thank you all for your input! I appreciate the help. However - and I know this might be a stupid question - but how would I use this code in Access????
Nov 7 '06 #18
PEB
1,418 Expert 1GB
:)

Ok my friend, we've done a big discussion about your problem...

But you need to select a function it's better the suggestion of pks00 copy from thescripts and

in access go in modules, choose New module and Paste there the function

Save it...

To obtain the word count in a column in your query:

Create a query add your table or query on which should be based the new one.

In an empty column write the name of the choosen function and between the () instaed the name inside, put the name of the column that contain the text that you want to get the count
Nov 7 '06 #19
Here's a simple function
Mar 21 '17 #20
Expand|Select|Wrap|Line Numbers
  1. public function word_count(InWord as variant) as Integer
  2. dim words() as string
  3.  
  4. word_str=nz(word_str,"")   'deals with null values passed in
  5. word_str = replace(word_str,vbcr," ")  ' Whatever you want to also delineate a word can be placed here.
  6.  
  7. while instr(word_str,"  ") > 0 )  'Check to see if double spaces
  8.    word_str = replace(word_str,"  "," ") ' change double to single space
  9. wend
  10. words = split(word_str," ")
  11. word_count = ubound(words) + 1 
  12. end function
Mar 21 '17 #21
pks00 Good statement

Thank you all
Ahmed
Oct 3 '17 #22

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: dan | last post by:
this is a program to count average letters per word. i am able to count the total number of letters, but not words. How do you count the total number of words in a text file, so i am able to divide...
3
by: Chris L | last post by:
Greetings, I searched for this possibility without finding very much. It might be too simple ? In the simplest of terms, I need to place a button on a form, and have it advance the number in a...
11
by: Foodbank | last post by:
Hello, I'm trying to develop a program that will enable me to count the number of words in a text file. As a plus, I'd like to be able to count how many different words there are too. I have a...
10
by: Tom E. | last post by:
Hello, I would like to know what the most efficient way is to count words in a string and to determine what the average word length is. I know this can be done in a array, but is there an easier...
9
by: aaron | last post by:
I have a few documents in which I need to get a total word count. Can anyone help? I'd like to create this program so I can get the result after entering the filename, and have the option to...
1
by: MicMic | last post by:
I need to know how count a field that returns a zero or a negative?
0
by: akshar108 via DotNetMonster.com | last post by:
Hello friends, I want to count words of pdf files through .net programming. If there is any technique then please tell me -- Message posted via DotNetMonster.com...
4
by: jackloanshark | last post by:
Hello, I am a beginner of c. I wrote a program about counting words in a txt file.But actually there are some error in it and it will stop counting at the first paragraph and just return the number...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.