473,406 Members | 2,954 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,406 software developers and data experts.

String function (counting string within a string)

Does anyone know of a string function in Access that will allow me to
count the number of instances one string occurs within another? Or if
there is some sort of word count function? If there is, I'm not aware
of it. I've tried the following unsuccessfully ...

SELECT *, (Len([myfield])-Len(Replace([myfield],'
what_im_looking_to_count','')))/(Len(' what_im_looking_to_count')) AS
KeywordFoundCountFROM tblYourTable;

That'll find how many times the phrase what_im_looking_to_count occurs
within the field called 'myfield'

Thanks,

Ralph Noble
ra*********@hotmail.com
Nov 13 '05 #1
4 3471
What error do you get trying to use that query?

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

"Ralph Noble" <ra*********@hotmail.com> wrote in message
news:ed**************************@posting.google.c om...
Does anyone know of a string function in Access that will allow me to
count the number of instances one string occurs within another? Or if
there is some sort of word count function? If there is, I'm not aware
of it. I've tried the following unsuccessfully ...

SELECT *, (Len([myfield])-Len(Replace([myfield],'
what_im_looking_to_count','')))/(Len(' what_im_looking_to_count')) AS
KeywordFoundCountFROM tblYourTable;

That'll find how many times the phrase what_im_looking_to_count occurs
within the field called 'myfield'

Thanks,

Ralph Noble
ra*********@hotmail.com

Nov 13 '05 #2
"Ralph Noble" <ra*********@hotmail.com> wrote in message
news:ed**************************@posting.google.c om...
Does anyone know of a string function in Access that will allow me to
count the number of instances one string occurs within another? Or if
there is some sort of word count function? If there is, I'm not aware
of it. I've tried the following unsuccessfully ...

SELECT *, (Len([myfield])-Len(Replace([myfield],'
what_im_looking_to_count','')))/(Len(' what_im_looking_to_count')) AS
KeywordFoundCountFROM tblYourTable;

That'll find how many times the phrase what_im_looking_to_count occurs
within the field called 'myfield'

Thanks,

Ralph Noble
ra*********@hotmail.com


In what sense was your attempt to use this function unsuccesful? The
function really does count the occurences of one string within another. For
example, if you had tblMyTable with fields MyID and MyText and wanted to
count how many times the word 'pen' occurs in that field:

SELECT tblMyTable.MyID, tblMyTable.MyText,
(Len(MyText)-Len(Replace(MyText,'pen','')))/(Len('pen')) AS
KeywordFoundCount
FROM tblMyTable

I can see some problems you might encounter, such as when counting the
occurences of the word "pen" in the sentence "I spent 4 dollars on that
pen." The function returns 2 (not 1) because "pen" occurs within the word
"spent"

Nov 13 '05 #3
"Ralph Noble" <ra*********@hotmail.com> wrote in message
news:ed**************************@posting.google.c om...
Does anyone know of a string function in Access that will allow me to
count the number of instances one string occurs within another?


Try this:
----------------------------------------
Public Function AAA(StringToSearchIn As String, StringToSearchFor As String)
As Integer
Dim i As Integer, k As Integer, h As Integer
For i = 1 To Len(StringToSearchIn)
h = InStr(i, StringToSearchIn, StringToSearchFor)
If h Then
k = k + 1
h = h + 1
i = h
End If
Next
AAA = k
End Function
-----------------------------------------

Ciao
Bruno
Nov 13 '05 #4
"Ralph Noble" <ra*********@hotmail.com> wrote in message
news:ed**************************@posting.google.c om...
Does anyone know of a string function in Access that will allow me to
count the number of instances one string occurs within another? Or if
there is some sort of word count function? If there is, I'm not aware
of it. I've tried the following unsuccessfully ...

SELECT *, (Len([myfield])-Len(Replace([myfield],'
what_im_looking_to_count','')))/(Len(' what_im_looking_to_count')) AS
KeywordFoundCountFROM tblYourTable;

That'll find how many times the phrase what_im_looking_to_count occurs
within the field called 'myfield'

Thanks,

Ralph Noble
ra*********@hotmail.com


In what sense was your attempt to use this function unsuccesful? The
function really does count the occurences of one string within another. For
example, if you had tblMyTable with fields MyID and MyText and wanted to
count how many times the word 'pen' occurs in that field:

SELECT tblMyTable.MyID, tblMyTable.MyText,
(Len(MyText)-Len(Replace(MyText,'pen','')))/(Len('pen')) AS
KeywordFoundCount
FROM tblMyTable

I can see some problems you might encounter, such as when counting the
occurences of the word "pen" in the sentence "I spent 4 dollars on that
pen." The function returns 2 (not 1) because "pen" occurs within the word
"spent"


Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Igor2004 | last post by:
Ladies and Gentlemen, I would like to offer you the following string functions Transact-SQL GETWORDCOUNT() Counts the words in a string GETWORDNUM() Returns a specified word from a string...
0
by: Igor Nikiforov | last post by:
Ladies and Gentlemen, I would like to offer you the following string functions Transact-SQL GETWORDCOUNT() Counts the words in a string GETWORDNUM() Returns a specified word from a string...
14
by: Mike N. | last post by:
Hello: I have a form that contains a multiple-select field that has 12 options in it. I would like the user to be able to select UP TO FOUR of those options. If they select more than four, I...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
9
by: Divick | last post by:
Hi all, I have a problem related to std::string class. Is it ok to assign a global string variable to a local string object as shown below? I am trying to print the address of local string...
24
by: Derek Hart | last post by:
Is there an efficient line of code to count the number of instances of one string within another. If I have the sentence: "I want to go to the park, and then go home." It would give me a...
4
by: John | last post by:
1. Is there an easy function in VB to count for instance the number of A's in a string? 2. Is there an easy funtion in VB to pull all characters out of a string into an array? (I tried the split...
3
by: Jason Huang | last post by:
Hi, In C#, how do I retrieve a special sub string within a big string? e.g., the "...fdsajk...ABC123...fdfdskafdl... ..." string has 5000 characters, and I want to retrieve the string "123"...
4
by: sandvet03 | last post by:
I am trying to expand on a earlier program for counting subs and now i am trying to replace substrings within a given string. For example if the main string was "The cat in the hat" i am trying to...
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?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.