473,480 Members | 1,914 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

subquery syntax question - sub query after FROM?

32 New Member
Hi again,

I'm trying to create a calculated field in a query that uses a sub query in the expression.

This is what I have:

Expand|Select|Wrap|Line Numbers
  1. tmpProvinceAbbr: (SELECT ProvinceAbbr FROM (SELECT ProvinceOrStateTable FROM tblCountry WHERE CountryID = 1) WHERE ProvinceID = 8;)
ProvinceOrStateTable is actually a field in another table.

The sub query works on it's own and produces the value of the ProvinceOrStateTable as it should.

When I insert the sub query after FROM in the primary query, I get promted for input for the ProvinceAbbr field (I'm prompted just as any parameter query would promt for missing information).

This is not what I expected! This expression should be returning the value of ProvinceAbbr, not asking for it!

What am I doing wrong?

Thanks!
Oliver
Feb 5 '08 #1
4 2570
Lysander
344 Recognized Expert Contributor
Hi again,

I'm trying to create a calculated field in a query that uses a sub query in the expression.

This is what I have:

Expand|Select|Wrap|Line Numbers
  1. tmpProvinceAbbr: (SELECT ProvinceAbbr FROM (SELECT ProvinceOrStateTable FROM tblCountry WHERE CountryID = 1) WHERE ProvinceID = 8;)
ProvinceOrStateTable is actually a field in another table.

The sub query works on it's own and produces the value of the ProvinceOrStateTable as it should.

When I insert the sub query after FROM in the primary query, I get promted for input for the ProvinceAbbr field (I'm prompted just as any parameter query would promt for missing information).

This is not what I expected! This expression should be returning the value of ProvinceAbbr, not asking for it!

What am I doing wrong?

Thanks!
Oliver
Expand|Select|Wrap|Line Numbers
  1. tmpProvinceAbbr: (SELECT ProvinceAbbr FROM (SELECT ProvinceOrStateTable FROM tblCountry WHERE CountryID = 1) WHERE ProvinceID = 8;)
Where is ProvinceAbbr to be found, because you appear to be trying to select ProvinceAbbr FROM ProvinceOrStateTable, as that is all the sub query is returning? Can you give an example of how you could select ProvinceAbbr without a subquery, or can you list the fields in the two tables?
Feb 5 '08 #2
olivero
32 New Member
Expand|Select|Wrap|Line Numbers
  1. tmpProvinceAbbr: (SELECT ProvinceAbbr FROM (SELECT ProvinceOrStateTable FROM tblCountry WHERE CountryID = 1) WHERE ProvinceID = 8;)
Where is ProvinceAbbr to be found, because you appear to be trying to select ProvinceAbbr FROM ProvinceOrStateTable, as that is all the sub query is returning? Can you give an example of how you could select ProvinceAbbr without a subquery, or can you list the fields in the two tables?
Hi Lysander,

ProvinceAbbr is a field in a table called tblProvinces_CA. This table name is stored within a field called ProvinceOrStateTable that lives in a table called tblCountry.

I've hardcoded the WHERE statements with 1 and 8 to simplify locating the correct records during troubleshooting.

This is what it would look like without the sub query (this works as a query):

Expand|Select|Wrap|Line Numbers
  1. tmpProvinceAbbr: (SELECT ProvinceAbbr FROM tblProvinces_CA WHERE ProvinceID = 8;)
Thanks,
Oliver
Feb 5 '08 #3
Lysander
344 Recognized Expert Contributor
Hi Lysander,

ProvinceAbbr is a field in a table called tblProvinces_CA. This table name is stored within a field called ProvinceOrStateTable that lives in a table called tblCountry.

I've hardcoded the WHERE statements with 1 and 8 to simplify locating the correct records during troubleshooting.

This is what it would look like without the sub query (this works as a query):

Expand|Select|Wrap|Line Numbers
  1. tmpProvinceAbbr: (SELECT ProvinceAbbr FROM tblProvinces_CA WHERE ProvinceID = 8;)
Thanks,
Oliver
Ok, let me see if I understand this.

You want to select ProvinceAbbr from a table, but you don't know the table name until you get ProvinceOrStateTable from the country.

So what you need is (SELECT ProvinceAbbr FROM XXXXX WHERE ProvinceID = 8;)

and XXXX is (Select ProvinceOrStateTable from tblCountry where countrycode=1;)

I am still missing something. Is ProvinceOrStateTable only one value for each country?

I can't see how this can be done by one query alone, but I think it can be done by a query and a function in code to return the data you want.

What you need is a function to which you pass the ProvinceID and the CountryID, this function generates a SQL string to extract the ProvinceAbbr and returns it as a value to your query.

If what I think you are doing is correct, please confirm and I think I can come up with an answer. What you are trying to do is very similar to my old coding days of machine code, where you had 'Indirect Addressing'

You want to get info from ProvinceOrStateTable but ProvinceOrStateTable does not have the info, it has the name of the table that has the info.

Ok, I think this will do it. Bear in mind I am writing this in the forum, not testing it for real so I might not get it 100% correct.

Create a global module and create a public function as so.
Expand|Select|Wrap|Line Numbers
  1. public function getProvinceAbbr  (byval ProvinceID as integer, byval CountryID as integer) as integer
  2.  
  3. dim strTable as string
  4.  
  5. strTable=dlookup("ProvinceOrStateTable","tblCountry","CountryId=" & CountryID)
  6. 'now have the table name, test for null records maybe
  7.  getProvinceAbbr =dlookup("ProvinceAbbr",strTable,"ProvinceID=" & ProvinceID)
  8. 'now have the value you want
  9. exit function
  10.  
Then in you query have
Expand|Select|Wrap|Line Numbers
  1. select getProvinceAbbr(8,1) as tmpProvinceAbbr, SOMEOTHERDATA from maintable;
  2.  
Feb 5 '08 #4
olivero
32 New Member
Ok, let me see if I understand this.

You want to select ProvinceAbbr from a table, but you don't know the table name until you get ProvinceOrStateTable from the country.

So what you need is (SELECT ProvinceAbbr FROM XXXXX WHERE ProvinceID = 8;)

and XXXX is (Select ProvinceOrStateTable from tblCountry where countrycode=1;)

I am still missing something. Is ProvinceOrStateTable only one value for each country?

I can't see how this can be done by one query alone, but I think it can be done by a query and a function in code to return the data you want.

What you need is a function to which you pass the ProvinceID and the CountryID, this function generates a SQL string to extract the ProvinceAbbr and returns it as a value to your query.

If what I think you are doing is correct, please confirm and I think I can come up with an answer. What you are trying to do is very similar to my old coding days of machine code, where you had 'Indirect Addressing'

You want to get info from ProvinceOrStateTable but ProvinceOrStateTable does not have the info, it has the name of the table that has the info.

Ok, I think this will do it. Bear in mind I am writing this in the forum, not testing it for real so I might not get it 100% correct.

Create a global module and create a public function as so.
Expand|Select|Wrap|Line Numbers
  1. public function getProvinceAbbr  (byval ProvinceID as integer, byval CountryID as integer) as integer
  2.  
  3. dim strTable as string
  4.  
  5. strTable=dlookup("ProvinceOrStateTable","tblCountry","CountryId=" & CountryID)
  6. 'now have the table name, test for null records maybe
  7.  getProvinceAbbr =dlookup("ProvinceAbbr",strTable,"ProvinceID=" & ProvinceID)
  8. 'now have the value you want
  9. exit function
  10.  
Then in you query have
Expand|Select|Wrap|Line Numbers
  1. select getProvinceAbbr(8,1) as tmpProvinceAbbr, SOMEOTHERDATA from maintable;
  2.  
Yep, that's exactly what I'm trying to do! Indirect is right :)

Each Country only has one such table (a list of all the Provinces, States, or other for that country). The name of that table is stored in the ProvinceOrStateTable field in tblCountry.

A little complex, but I couldn't think of any other way to keep these things well organized. Each "province" table also stores tax information for that province. The tax information is almost always different from province or state to the other, and it's always different between countried. Sometimes rules need to be created for only on one country at a time.

From a long term point of view, it seems to me that it would be easier to manage a separate province table for each country rather than lumping all the provinces and states into one table, along with their respective (and very diverse) tax structures.

I'll give your code a shot. I kind of had a feeling that I wasn't going to be able to do this with just a straight query/subquery, but it never hurts to ask :)

Thanks for the help!

Oliver
Feb 5 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
4560
by: lev | last post by:
CREATE TABLE . ( NULL , , (44) ) ID is non-unique. I want to select all IDs where the last entry for that ID is of type 11.
8
19565
by: Andrew McNab | last post by:
Hi folks, I have a problem with an MS Access SQL query which is being used in an Access Report, and am wondering if anyone can help. Basically, my query (shown below) gets some records from a...
7
2354
by: K. Crothers | last post by:
I administer a mechanical engineering database. I need to build a query which uses the results from a subquery as its input or criterion. I am attempting to find all of the component parts of...
3
22384
by: Dave | last post by:
I have a list of towns in tblTownData, and a list of people's names in tblNames. During a year, there are multiple events where more names are added in tblNames, for a particular town. ie:...
0
1093
by: KemperR | last post by:
Dear All, I have a very hard time with an ACCESS 2000 crosstab query. The basic query the crosstab is based on looks like this: SELECT DokumenteNachNummer.Dokumente_ID ,...
0
1262
by: lordofhyphens | last post by:
I'm trying to form a correlated subquery (right now, it's MS Access running to linked tables in MS SQL, although I have tried to make the same query against the MS SQL tables directly). The goal...
3
3504
by: laurentc via AccessMonster.com | last post by:
Hi. I have an issue with my Access project. I have rather big tables of data (about 11 000 rows). These tables are historical product quotations, so they are very simple : - MyDate...
4
6227
by: Diogenes | last post by:
Kind readers, I am currently developing an app with PHP 5 & MySql 5. The following select statement works as expected from the MySql command line but produces an error when run from PHP. ...
1
1484
by: Docster2005 | last post by:
Hi folks, A DTS package we have run for years now no longer works. The specific part that is not working is a subquery in the SOURCE object of a transformation. The source is based on a...
1
3552
bilibytes
by: bilibytes | last post by:
Hi everyone, I am trying to find out how to make a query within an array. i make a query where i get from a table named 'other_info', an array with all the user id's resulting from a subquery....
0
6911
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
7091
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
6966
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4787
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
4488
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...
0
2988
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1303
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
564
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
185
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.