That could get decidedly tricky. I suppose if you're sure that the query
includes only one table you could search the SQL for the text ' FROM ' and
then assume that everything between that text and the next space is the name
of the table - unless you have tables with spaces in their names, when
you'll have to search for the "[" and "]" characters following the ' FROM '
text.
Keep in mind that there's nothing preventing anyone from having a field in a
table with the same name as the table itself, or from including the name as
part of a field name, so even after you've found the name of the table, you
can't go blindly replacing it anywhere it happens to appear in the SQL.
This is a decidedly non-trivial parsing operation you've taken on here. I
can't help wondering why it would ever be necessary - if you don't know what
the name of the table is, how do you know it needs to be changed, let alone
to what it should be changed?
--
Brendan Reynolds (MVP)
tjh said:
Thank You,
This looks like what I need.
How can I determine the name of the first table "tblTest1" --the name of
the
table currently used in the query.
This will be the only table in the query.
Do you know of the code to determine the table/tables currently used in
the
Query. I think that once this is determined I can declare it as "tblTest1"
and the query will change as you show below.
Thank You,
:
CurrentDb.QueryDefs("qryTest").SQL=
Replace(CurrentDb.QueryDefs("qryTest").SQL,"tblTest1","tblTest2")
--
Brendan Reynolds (MVP)
Can you refer me to somewhere that I can look into changing the table
name
in an SQL query with VBA code. I am familiar (to some extent, about 4
yr
experience) with using VBA code. I have just never changed SQL code
with
variables. I think setting up or declaring the variables to change the
table
and field names is giving me the most trouble. Setting up to change the
table
name is my biggest problem, I think. I believe I can work around the
field
names.
Thank You
:
I think your best bet is 3 separate queries / reports if you are
unfamilliar
with code and the fields are different.
Thank You for your response.
Could you describe how I can modify the SQL to specify which table
to
use
when I click the "Launch" command. I am not sure how to go about
doing
this.
If I need to use VBA code, could you please give an example of the
code.
Also, the amount field title is different in each table one for
Sales,
one
for COS and one for Income.
Thank You,
:
You could, but if the field names are the same, why not just base
the
single
report on a single query and modify the SQL when you click the
"Launch
Report" button
Hello,
Is it possible to use a form to select which table a Query will
utilize.?
I have three different tables: 1)Sales 2)COS 3)Income.
Each table has 3 columns/fields 1)contract 2)amount 3)Date.
I need to run similar reports from each. I would like to run a
report
from
any of the three reports and then select the date range to run
from
the
report - using one form. The only way I know how to do this is to
create 3
different queries, and then have the user select on the Form
which
query
to
run. Is it possible to have the user select on the form which
table
to
use
in
one query?
Thank You