Parsing text string function (1 Viewer)

deejay_totoro

Registered User.
Local time
Today, 17:18
Joined
May 29, 2003
Messages
169
Hello,

It would be great if someone could help me with writing a function.

The function will parse a string which can have any number of words, ie: "This is a string with spaces between words".

If a certain rule is met, ie: "wor" or "str" or "ith" then the function will subsitiute those letters with something else. For example:

"wor" becomes "WoR"
"str" becomes "S'TR"
"ith" becomes "ItH"


"This is a string with spaces between words" would become "This is a S'TRing wItH spaces between WoRds".
It would be great if these changes could be held in a lookup table so it would be easy to add new words or letters to search for.

Cheers!
 

DCrake

Remembered
Local time
Today, 17:18
Joined
Jun 8, 2005
Messages
8,632
Simple Software Solutions

You will need to employ the Replace function

Sample

Replace("Your string with spaces","g w","g-w")

Returns

Your string-with spaces

also

Replace("Find a bag of sand","a","?")

Returns

Find ? b?g of s?nd


David
 

deejay_totoro

Registered User.
Local time
Today, 17:18
Joined
May 29, 2003
Messages
169
Replace in function

hello,

thanks for the reply.

I have tried using Replace in a function that I call from a query. Here is the code:

Public Function fnReplace(MyField As Field, MyFindText As String, MyReplaceText As String)

MyFindText = "OB"
MyReplaceText = "O'B"

MyField = fnReplace([MyField], MyFindText, MyReplaceText)

End Function



Then in the query I have :Expr1: fnReplace([Some Field])

Problem is i cant get this to work. The error when I run the query is:

Wrong number of arguments used with function in query expression fnReplace([Some Field]).

Any ideas what I am doing wrong here?

Thanks again.
 

Rabbie

Super Moderator
Local time
Today, 17:18
Joined
Jul 10, 2007
Messages
5,906
hello,

thanks for the reply.

I have tried using Replace in a function that I call from a query. Here is the code:

Public Function fnReplace(MyField As Field, MyFindText As String, MyReplaceText As String)

MyFindText = "OB"
MyReplaceText = "O'B"

MyField = fnReplace([MyField], MyFindText, MyReplaceText)

End Function


Then in the query I have :Expr1: fnReplace([Some Field])

Problem is i cant get this to work. The error when I run the query is:

Wrong number of arguments used with function in query expression fnReplace([Some Field]).

Any ideas what I am doing wrong here?

Thanks again.
Your function definition of fnReplace has 3 parameters

in Expr1: you are only passing 1 parameter.
 

Call_Me_Sam

Chief Imperial Navigator
Local time
Today, 17:18
Joined
Feb 26, 2008
Messages
244
hello,

thanks for the reply.

I have tried using Replace in a function that I call from a query. Here is the code:

Public Function fnReplace(MyField As Field, MyFindText As String, MyReplaceText As String)

MyFindText = "OB"
MyReplaceText = "O'B"

MyField = fnReplace([MyField], MyFindText, MyReplaceText)

End Function


Then in the query I have :Expr1: fnReplace([Some Field])

Problem is i cant get this to work. The error when I run the query is:

Wrong number of arguments used with function in query expression fnReplace([Some Field]).

Any ideas what I am doing wrong here?

Thanks again.

Deejay,

i have a sneaky suspicion it's to do with the fact you're using the function within function and not actually returning the value to the passing expression..

Code:
[FONT=Courier New][COLOR=blue]{MyFindText = "OB"[/COLOR][/FONT]
[FONT=Courier New][COLOR=blue]MyReplaceText = "O'B"} located in the calling query i guess?[/COLOR][/FONT]
 
[FONT=Courier New]Public Function fnReplace(MyField As Field, MyFindText As String, MyReplaceText As String)[/FONT]
 
[FONT=Courier New][COLOR=red]fnReplace = Replace[/COLOR]([MyField], MyFindText, MyReplaceText)[/FONT]
 
[FONT=Courier New]End Function[/FONT]
 

deejay_totoro

Registered User.
Local time
Today, 17:18
Joined
May 29, 2003
Messages
169
thanks everyone.

I think im maybe going about this the wrong way.

im thinking I could just send the field from the query to the function and it would come back with a result?

I think the other point is I would like t use multiple parameters. ie. if the string contains MC do Mc or if it contains OB then use O'B and so on.

chers!
 

Call_Me_Sam

Chief Imperial Navigator
Local time
Today, 17:18
Joined
Feb 26, 2008
Messages
244
thanks everyone.

I think im maybe going about this the wrong way.

im thinking I could just send the field from the query to the function and it would come back with a result?

I think the other point is I would like t use multiple parameters. ie. if the string contains MC do Mc or if it contains OB then use O'B and so on.

chers!

deejay,

to use a function in a query, as rabbi said, you need to pass the correct number of explicit parameters (you can have optional parameters which you don't have to passthrough).

Have you not tried using the Replace function actually in a query instead of calling it from a user defined function?
 

LPurvis

AWF VIP
Local time
Today, 17:18
Joined
Jun 16, 2008
Messages
1,269
Are you using Access 97 or teh raw release of Access 2000?
I'm just wondering why you're trying to use a wrapping function of Replace at all.
(Not that it isn't an option - just wondering).

And if you do go down the optional parameter route - bear in mind that function parameters in expressions (which includes query calls to them) allow parameters to be optional only if there are no more parameters passed.
e.g. for a function which accepts three optional parameters
FunctionCall([Field1])
is fine. But
FunctionCall([Field1], , [FieldX])
wouldn't execute.
 

Brianwarnock

Retired
Local time
Today, 17:18
Joined
Jun 2, 2003
Messages
12,701
I can'tthink of anyway to do this except using a Function with a series of Replace statements and hard coding the values, I suppose one could create a table and lookup the values as one looped but I suspect the performance would be poor.

anyway as a stopgap

Public Function fnReplace(MyField ) As String

pass1=Replace(Myfield,"wow","WoW")
pass2=Replace(pass1,"ith","ItH")
etc until
fnReplace=passn
End Function

Notice I chickened out of the OB to O'B as I wasn't sure if the apostrophe would cause problems :D, you will have to try and see, if so and you cannot solve it come back.

query has parsedfield: fnReplace(Myfield)

Brian
 

Call_Me_Sam

Chief Imperial Navigator
Local time
Today, 17:18
Joined
Feb 26, 2008
Messages
244
Is replace case sensitive? I'm thinking that if you have a number of replace statements then is there a possibility of replacing something that has already been replaced in a previous line, and would this be an issue?
 

Users who are viewing this thread

Top Bottom