Link to home
Start Free TrialLog in
Avatar of ktjamms2
ktjamms2Flag for United States of America

asked on

Replace text in a string with a table field value

I’m using an Access database. One of the tables contains a field that is a string expression. The string expression contains a reference to a field in another MASTER table in the database.
The Master table is joined by ID# with the table with the string expression (INNER JOIN MasteTable ON TableName.IDNUM = MasterTable.IDNUM

Example expression:
@M.PCNT @M.DOLLAR 0 % M$  
(PCNT & DOLLAR are fields in a MASTER table)
I would like to replace the reference with the actual value in the field it is referencing. When there is only one reference, I have designed a query and used:
EXPRESSION2: Replace([TableName]![EXPRESSION],"@M.PCNT",[MasterTable]![PCNT])
But, when there is more than one reference in the expression string, I have not found a syntax that will replace more than one reference.
I was thinking that a function that would check the case and make the appropriate replacement for the entire table instead of making separate querries would be nice, but I’m not sure of the syntax.
Like what I had in mind:
Case
Instr([TableName]![EXPRESSION]) = "@M.PCNT"
strReplace = [MasterTable]![PCNT]
Case
Instr([TableName]![EXPRESSION]) = "@M.DOLLAR"
strReplace = [MasterTable]![DOLLAR]
Have a case for every reference in the expression field.
Example expression:
@M.PCNT @M.DOLLAR 0 % M$
The Result would be like this
.02 500 0 % M$
if (@M.PCNT field value = .02) and (@M.DOLLAR field value = 500)
But the tables would have to be joined by ID#

Any help would be greatly appreciated!
ASKER CERTIFIED SOLUTION
Avatar of tbsgadi
tbsgadi
Flag of Israel image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of als315
You can add small table  with two fields: source and replacement. In first record should be @M.DOLLAR and 500, in second @M.PCNT and .02
Then create an update query
Look at sample (tbl11- source, tbl2- replacement rules, qry1- update query)
DB1.accdb
Avatar of ktjamms2

ASKER

tbsgadi, I tried yours:
Try: Replace(Replace([TableName]![EXPRESSION],"@M.PCNT",[MasterTable]![PCNT]),[TableName]![EXPRESSION],"@M.DOLLAR",[MasterTable]![DOLLAR])

I don't know if I'm doing something wrong, but in the cases where the same reference is in the expression more than once, it returns #Error and for the rest, it returns only the REFERENCE (@M.DOLLAR) OR it returns #Func! and not the remainder of the string expression.
There are too many records to try to re-create two tables every time I need to complete this task.
You need only records with values to be replaced. Have you looked sample DB?
Each ID# can have 50 records with different references to the master table and I have about 23,000 ID#'s. Total of about 600,000 lines that need references replaced with an estimated 75 fields from the master table. Some strings reference more than one master table field.
The references don't have the same values for each ID#
This Works...had to remove the [TableName]![EXPRESSION],

Replace(Replace([TableName]![EXPRESSION],"@M.PCNT",[MasterTable]![PCNT]),"@M.DOLLAR",[MasterTable]![DOLLAR])
Great! Glad to help!