Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
|
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old August 2nd, 2009, 08:19 PM
Authorized User
 
Join Date: May 2009
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to use a field in an non-related table in a query

In MS Access I need to use a field in a "constants" table in a query with several tables to which the constants table cannot be related directly.

In simplified form, I take a sum1 field from table 1 and a sum2 field from table 2. These two tables are related by a CustID field. I want to compare those two sums and produce a report if the sums are different by a set, but varying percentage.

I want to have a constants table in which one field is SumDiff. (There are other "constants" in it that I would use in other ways) That is, if we want to see all records where Sum1 in greater than sum2 by the amount specified in the constants table, I would have a query with an expression such as:

if(([table1].[sum1]-[table2].[sum2])/[table2].[sum2]=>[constants].[SumDiff],"yes","no")

When I try to construct this query, it is fine when I bring in table1 and table2 and relate them, but as soon as I bring in the Constants table and do not relate it, the query returns an error message.

Certainly I could "hard code" in the percentage, but it then makes it difficult for the users to easily change that percentage difference in the two sum fields. And we need to somewhat regularly change that number in SumDiff. I would also like to avoid making the user interact with the query by typing in the percentage every time both for ease of use and possible typing errors, but also because the person running the query may not know the number to enter.

How can I draw this SumDiff field into the query from a table that has no other common fields with table1 and table2?
 
Old August 3rd, 2009, 01:56 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

You could do this with an outer join.

I wish you had shown your main query. As it is, I'll have to just use a sample:
Code:
SELECT T1.custID, T1.sum1, T2.sum2, 
       (T1.sum1-T2.sum2)/T2.sum2 AS pct.
       IIF( C.SumDiff IS NULL, 'No', 'Yes' ) AS isPercentageBigEnough
FROM ( table1 AS T1 INNER JOIN table2 AS T2 ON T1.custid = T2.custid )
LEFT JOIN Constants AS C ON ( (T1.sum1-T2.sum2)/T2.sum2 ) >= C.SumDiff
This assumes that there is only one row in the Constants table. And you put each contant into its own field.

If, instead, the Constants table has only two fields (ConstantName, ConstantValue say) then we'd need a variation on that. Incidentally, I really think that such a table with only the two fields is a superior design. It allows you to add new constants at any time without needing to restructure the DB. The only thing I might do is have 3 fields: ConstantName, ConstantNumber, ConstantString. Where each ConstantName has a value in *EITHER* (but not both) ConstantNumber and ConstantString. And use DOUBLE for the ConstantNumber. It can always be compared to any other numeric type without need for conversion.

Oh, what the heck. Here's what the query would look like if you used my concept of a 3-column Constants table:
Code:
SELECT T1.custID, T1.sum1, T2.sum2, 
       (T1.sum1-T2.sum2)/T2.sum2 AS pct.
       IIF( C.SumDiff IS NULL, 'No', 'Yes' ) AS isPercentageBigEnough
FROM ( table1 AS T1 INNER JOIN table2 AS T2 ON T1.custid = T2.custid )
LEFT JOIN Constants AS C 
ON ( C.ConstantName = 'SumDiff' AND ( (T1.sum1-T2.sum2)/T2.sum2 ) >= C.ConstantNumber )
The extra parens in the ON clause for the LEFT JOIN *are* necessary, but if you store this as a query in Access and then retrieve it, Access will have lost the parens. It's a bug I reported in Access98, Access2000, and Access2003. Dunno if it got fixed in more recent versions.
 
Old August 13th, 2009, 08:15 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,151
Thanks: 2
Thanked 14 Times in 14 Posts
Send a message via ICQ to SerranoG Send a message via AIM to SerranoG
Default

Without knowing table structure and other details, my first guess would be

iif([table2].[sum2] = 0, "N/A", iif(([table1].[sum1] - [table2].[sum2]) / [table2].[sum2] => [constants].[SumDiff], "yes", "no"))
__________________
Greg Serrano
Michigan Dept. of Environmental Quality
Air Quality Division





Similar Threads
Thread Thread Starter Forum Replies Last Post
database related query dkvaseeta Pro VB Databases 2 February 22nd, 2007 10:59 AM
To show selected related field of DDL1 in DDL 2 skirpane .NET Framework 1.x 1 June 1st, 2006 03:04 PM
query related to function nimeshkumargupta SQL Server 2000 0 January 18th, 2005 04:05 AM
Related Field Does Not Display in Query twsinc Access VBA 1 November 18th, 2004 10:39 AM
problems with related query apek PHP How-To 5 February 4th, 2004 02:06 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.