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: - 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
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: - 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
- 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?
- 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): - tmpProvinceAbbr: (SELECT ProvinceAbbr FROM tblProvinces_CA WHERE ProvinceID = 8;)
Thanks,
Oliver
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): - 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. -
public function getProvinceAbbr (byval ProvinceID as integer, byval CountryID as integer) as integer
-
-
dim strTable as string
-
-
strTable=dlookup("ProvinceOrStateTable","tblCountry","CountryId=" & CountryID)
-
'now have the table name, test for null records maybe
-
getProvinceAbbr =dlookup("ProvinceAbbr",strTable,"ProvinceID=" & ProvinceID)
-
'now have the value you want
-
exit function
-
Then in you query have -
select getProvinceAbbr(8,1) as tmpProvinceAbbr, SOMEOTHERDATA from maintable;
-
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. -
public function getProvinceAbbr (byval ProvinceID as integer, byval CountryID as integer) as integer
-
-
dim strTable as string
-
-
strTable=dlookup("ProvinceOrStateTable","tblCountry","CountryId=" & CountryID)
-
'now have the table name, test for null records maybe
-
getProvinceAbbr =dlookup("ProvinceAbbr",strTable,"ProvinceID=" & ProvinceID)
-
'now have the value you want
-
exit function
-
Then in you query have -
select getProvinceAbbr(8,1) as tmpProvinceAbbr, SOMEOTHERDATA from maintable;
-
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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.
|
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...
|
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...
|
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:...
|
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 ,...
| |
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...
|
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...
|
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.
...
|
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...
|
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....
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
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...
| |