Query Replace Function

bs0d

Well-known Member
Joined
Dec 29, 2006
Messages
622
I'm building a query in MS Access.

I have a time series of data (table 'A'), and comments with dates in a separate table (table 'B'). I have built a query to show data for an item, and the comments from Table B on the appropriate date where a matching item ID is found.

Is it possible for me to put in the max value of a given field from table 'A' at each point where a comment is currently shown from table B in the query?

I was thinking you could use replace(), but it's getting an error. I tried:

Code:
Expr1: Replace([table_b].[comment], "*", Max([table_a].[field]))
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Ok lets assume the example data:

Code:
Table A:
fields: ID, Date, Value

example records:
ID   Date          value
1    01/01         12
2    01/02         11
3    01/03         154
4    01/04         68
5    01/05         42

Table B:
fields: ID, Date, Comment

Example records:
ID    Date        Comment
1      01/02      the cheese is old and moldy
2      01/05      opening season is today

Using the example table data above, I'd like to create a query that generated this result:

Code:
ID   Date          value      Comment
1    01/01         12          
2    01/02         11          154
3    01/03         154
4    01/04         68
5    01/05         42          154

The query displays the max value from the "value" field in Table A at each instance of a comment in Table B (for the appropriate date(s)).
 
Upvote 0
DMAX with an IIF function should work. Give it a shot, and write back if you get stuck.
 
Upvote 0
From Post #3, why are comments "154" in the result. For one, there is no comment field with a value of 154. And why should dates 1/2 and 1/5 in relation to the comment table should give that result. Also, if there is only one comment on each day then why do we need max at all? This is unclear to me - the problem doesn't seem to be stated correctly. I don't think the sample data is illustrating what needs to happen.
 
Last edited:
Upvote 0
You're right, thanks Joe4.

In case anyone else finds the thread useful, here's basically what I used:

Code:
Expr1: IIF([table_a].[value] Is Not Null, DMax("value", "table_a", "ID=XXX"))


Thought I'd provide some background as to why I would want to do this.

I'm using an Excel chart to plot historical values. And I wanted to have a 2nd series that displayed a node / symbol where a comment existed. I wanted all of the comment nodes to appear in linear fashion across the top of the chart. To do this, I needed the max value for the item in the series to plot the data points. I could have done this in VBA, but though it would be much easier if I could craft the query to do it for me.

I apologize xenou if the question wasn't clear enough through my example(s). I may have answered your question with the background info above. The "154" appears instead of the comment if a comment existed for that date. The 154 is the max value from table a. I need/wanted the max value (instead of the comment) for charting purposes only.
 
Last edited:
Upvote 0
Good job! Glad you were able to figure it out. I thought you would be able to if I put you on the right path.
 
Upvote 0
There is a catch as I review the results...

It's providing what I need for each date that a comment exists in table_b, even if the ID's do not match. In other words, this works if theres only one entity that has comments. But since both tables house data for multiple entities, it's acting up a bit.
 
Upvote 0
Have you linked your two tables on the date field in your query?
 
Upvote 0

Forum statistics

Threads
1,223,911
Messages
6,175,324
Members
452,635
Latest member
laura12345

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top