473,414 Members | 1,989 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,414 software developers and data experts.

Douglas J. Steele's CountInstances() function...

MLH
Access 97 doesn't have Replace$ function -
at least I couldn't find it. Can someone tell me
what the command does so I can adapt Doug's
code for my own use in A97?

Private Function CountInstances( _
ByVal ToSearch As String, _
ByVal ToFind As String) As Long

CountInstances = (Len(ToSearch) - _
Len(Replace$(ToSearch, ToFind, vbNullString))) _
\ Len(ToFind)

End Function

Aug 9 '07 #1
1 1623
On Aug 9, 1:38 pm, MLH <C...@NorthState.netwrote:
Access 97 doesn't have Replace$ function -
at least I couldn't find it. Can someone tell me
what the command does so I can adapt Doug's
code for my own use in A97?

Private Function CountInstances( _
ByVal ToSearch As String, _
ByVal ToFind As String) As Long

CountInstances = (Len(ToSearch) - _
Len(Replace$(ToSearch, ToFind, vbNullString))) _
\ Len(ToFind)

End Function
The Replace function tries to replace inside a string the occurrences
of one substring with another. I think its speed can be increased
easily by utilizing the 'start' argument of the InStr function to
prevent searching the same portion of the string more than once.
Here's an example to show a potential pitfall:

s = "abracadabrabracadabra"
sThis = "abracadabra"
sWithThis = "barbara"

After the first replacement,

s = "barbarabracadabra"

If you don't watch out by picking up the search after the replacement,
the second replacement would result in s = "barbarbarbara" instead of
s = "barbarabracadabra"

So not only should the 'start' argument be used to prevent searching
the same portion of string more than once for efficiency, it should
also skip over the inserted string.

How about something like:

Function Replace(ByVal s As String, ByVal sThis As String, ByVal
sWithThis As String) As String
Dim intS As Integer
Dim strTemp As String

intS = 1
strTemp = s
Do While InStr(intS, strTemp, sThis) 0
intS = InStr(intS, strTemp, sThis)
If intS = 1 Then
strTemp = sWithThis & Right(strTemp, Len(strTemp) - Len(sThis))
intS = Len(sWithThis) + 1
Else
strTemp = Mid(strTemp, 1, intS - 1) & sWithThis & Right(strTemp,
Len(strTemp) - intS - Len(sThis) + 1)
intS = intS + Len(sWithThis)
End If
Loop
Replace = strTemp
End Function

Note that if 'start' is longer than 'string1,' such as when the string
to be replaced is at the end and intS becomes one larger than the
length of strTemp, the InStr function will return 0. The built-in
Replace function of later versions of Access results, as it should, in
"barbarabracadabra" for the given example. The replacement Replace
function shown here should allow you to use the elegant technique
Douglas posted within A97 for counting the number of occurrences of a
substring within a string. Be sure to test the Replace function
thoroughly before using it and please post back if you discover any
problems with it.

James A. Fortune
CD********@FortuneJames.com

Aug 10 '07 #2

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

Similar topics

0
by: Google Mike | last post by:
You readers in comp.lang.php may use PostgreSQL, so you might find this review useful... ISBN 0-7357-1257-3 "PostgreSQL", Copyright 2003 by Sams Publishing. Authors: Korry Douglas, Susan...
3
by: israel | last post by:
The current DDJ ( at least the latest that our antipodean newsstands have ) has an interesting interview with Guy Steele. He contributed to Scheme, Lisp , Java and did the first port of Tex.
3
by: Chris | last post by:
Hi, I have two, similar SQL Server 7.0 databases (in the same SQL Server Group) that I use as the backend for two Access 2000 front end applications - one is the live version, the other is the...
9
by: J Bunton | last post by:
My Access 97 applictions seem to have recently become very Cpu useage hungry. When opened they go to almost 100% useage on the peformance monitor. After several seconds this returns to near...
1
by: Bryan Olson | last post by:
Yesterday I embarrassed myself on sci.crypt with some incorrect C code and corresponding claims about the language. My source was Harbison and Steele (H&S), /C, A Reference Manual/ and I thought...
1
by: Peter Michaux | last post by:
Hi, I just finished watching two presentations (in three clips each) that Douglas Crockford gave to the Yahoo! folks. The first is called "Theory of the DOM" and the second "Advanced...
2
by: kj | last post by:
My favorite book on C, by far, is Harbison and Steele's "C: A Reference Manual." (In fact, it is my all-time favorite computer language reference book, irrespective of language.) Even though it...
1
by: Aron | last post by:
Hello the forum, I had a question about the function Douglas Crockford provided to remove the "new" operator from everyday coding syntax: function object (o) { function F() {} ...
2
by: Evertjan. | last post by:
"It turns out a browser war is a good thing," although most of the pain will be borne by Web developers and users, Crockford said. ...
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
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
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
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.