I need to write some SQL to be used with VBA where a query refers to itself.
Not very clear I know, so here is the finished result for Query21.
I am trying to get the value of CountTables.
This is the SQL for Query21 - SELECT TblPSysAllFields.TableName, TblPSysAllFields.FieldName,
-
TblPSysAllFields.InputTable, TblPSysAllFields.InputField,
-
DCount("TableName","Query21","TableName = '" & [TableName] & "'") AS CountTables
-
FROM TblPSysAllFields
-
WHERE (((TblPSysAllFields.FieldName) Is Not Null)
-
AND ((TblPSysAllFields.InputTable) Is Not Null) AND ((TblPSysAllFields.Level)=1)
-
AND ((TblPSysAllFields.IsPrimary) Not Like "*P*"))
-
ORDER BY TblPSysAllFields.FieldName, TblPSysAllFields.TableName;
So the problem is that the SQL refers to Query21, but I don't want Query 21 to be a saved Query because in the code, I will be changing the value of "Level"
Is there a way round this please?
Phil
Phil,
Try using a subquery. You would just have to make sure to change "Level" in each section. - SELECT TblPSysAllFields.TableName,
-
TblPSysAllFields.FieldName,
-
TblPSysAllFields.InputTable,
-
TblPSysAllFields.InputField,
-
tblSubTables.CountTables
-
FROM TblPSysAllFields
-
INNER JOIN (SELECT TblPSysAllFields.TableName,
-
Count(TblPSysAllFields.TableName) AS CountTables
-
FROM TblPSysAllFields
-
WHERE (((TblPSysAllFields.FieldName) Is Not Null)
-
AND ((TblPSysAllFields.InputTable) Is Not Null)
-
AND ((TblPSysAllFields.Level)=1)
-
AND ((TblPSysAllFields.IsPrimary) Not Like "*P*"))
-
GROUP BY TblPSysAllFields.TableName) AS tblSubTables
-
ON TblPSysAllFields.TableName = tblSubTables.TableName
-
WHERE (((TblPSysAllFields.FieldName) Is Not Null)
-
AND ((TblPSysAllFields.InputTable) Is Not Null)
-
AND ((TblPSysAllFields.Level)=1)
-
AND ((TblPSysAllFields.IsPrimary) Not Like "*P*"))
-
ORDER BY TblPSysAllFields.FieldName,
-
TblPSysAllFields.TableName;
Hope this hepps!
4 1179
Phil,
Try using a subquery. You would just have to make sure to change "Level" in each section. - SELECT TblPSysAllFields.TableName,
-
TblPSysAllFields.FieldName,
-
TblPSysAllFields.InputTable,
-
TblPSysAllFields.InputField,
-
tblSubTables.CountTables
-
FROM TblPSysAllFields
-
INNER JOIN (SELECT TblPSysAllFields.TableName,
-
Count(TblPSysAllFields.TableName) AS CountTables
-
FROM TblPSysAllFields
-
WHERE (((TblPSysAllFields.FieldName) Is Not Null)
-
AND ((TblPSysAllFields.InputTable) Is Not Null)
-
AND ((TblPSysAllFields.Level)=1)
-
AND ((TblPSysAllFields.IsPrimary) Not Like "*P*"))
-
GROUP BY TblPSysAllFields.TableName) AS tblSubTables
-
ON TblPSysAllFields.TableName = tblSubTables.TableName
-
WHERE (((TblPSysAllFields.FieldName) Is Not Null)
-
AND ((TblPSysAllFields.InputTable) Is Not Null)
-
AND ((TblPSysAllFields.Level)=1)
-
AND ((TblPSysAllFields.IsPrimary) Not Like "*P*"))
-
ORDER BY TblPSysAllFields.FieldName,
-
TblPSysAllFields.TableName;
Hope this hepps!
Thanks Twinnyfo
Other than a small typo error, it worked perfectly. I must admit that among my many Access weeknesses, SubQueries is high on the list.
Grateful thanks
Phil
I think I learned everything I know about Sub-Queries here on Bytes!
NeoPa 32,556
Expert Mod 16PB Sign in to post your reply or Sign up for a free account.
Similar topics
by: Minitman |
last post by:
I have an address database and I want to have the query prompt for me to
enter an expression that will bring up ALL names begining with same
first ltr for individual page on my report. I am...
|
by: Nicolae Fieraru |
last post by:
Hi All,
I have a query, Select Count(BoolField) from tblMyTable, Where BoolField =
true.
If I run the query by itself, it returns the number of true records
I want to use the result of that...
|
by: Nicolae Fieraru |
last post by:
Hi All,
I have a table, tblCustomers, with fields SalutationID and Firstname.
I made a query, qrySelect = "Select FirstName from tblCustomers Where
SalutationID = "
If I run this query by...
|
by: dio |
last post by:
Hi there,
I'm trying to create an OleDBDataAdapter with a command that will receive a
parameter to act as a filter on the query.
I'm using the Query Builder wizard and it works fine, unless I...
|
by: Dave |
last post by:
I have a page with a button that updates a table in the database. This should
affect the data displayed on the page.
I've used break points to ensure that the code calling the query to collect...
|
by: pmacdiddie |
last post by:
I have an append query that needs to run every time a line item is
added to a subform. The append writes to a table that is the source
for a pull down box. Problem is that it takes 5 seconds to...
|
by: Kevin M |
last post by:
I have one table and have created a form from that table. Also, I have
created a delete query. I want to but a button on the form which will delete
all records from the table; however, I cannot get...
|
by: Richard Hollenbeck |
last post by:
I am very sorry about the (almost) re-post, but you will see that my first
question wasn't very clear; I have another question I posted this morning
called, "in DAO: Run time error 3061 Too few...
|
by: Del |
last post by:
Hello and thanks for any and all assistance!
I have a database that is used by several users on several different
machines.
The backend database is housed on a file server.
Each user has a...
|
by: phill86 |
last post by:
Hi
I am re-posting this thread because it has become very confusing and I have got some way to solving the problem so it is a slightly different question from the initial thread.
here is the...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
| |