473,404 Members | 2,178 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,404 software developers and data experts.

Table name as parameter in SQL query, with VBA...

Dear all,

I have several tables based on exactly the same fields
(Key/Date/Price1/Price2).
I do some statistics on the prices.

However, as I have many different tables (the tables are different
because I directly import the data from a .csv file), I do not want to
create dozens of queries to be able to get the results of the different
tables.
Therefore, I would like to create a query in Access that will ask me
the table name from which to get the data each time I need.
Unfortunately, I find I cannot pass a table name as a Parameter to a
query.

I have to have to program a VBA code to construct a SQL string.
However, I cannot manage to build that code, as I am a beginner in VBA.

I would appreciate any help, hints or suggestions.

Thanks,

Laurent

Sep 11 '06 #1
5 14743
Just a quick note or hint, you want to make sure your field names do not
match key names. Such as Date is a key name which can conflict within a
query unless you rename your field to Contract_Date or enclose the Date
field within [], [Date].
"laurentc" <la*************@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
Dear all,

I have several tables based on exactly the same fields
(Key/Date/Price1/Price2).
I do some statistics on the prices.

However, as I have many different tables (the tables are different
because I directly import the data from a .csv file), I do not want to
create dozens of queries to be able to get the results of the different
tables.
Therefore, I would like to create a query in Access that will ask me
the table name from which to get the data each time I need.
Unfortunately, I find I cannot pass a table name as a Parameter to a
query.

I have to have to program a VBA code to construct a SQL string.
However, I cannot manage to build that code, as I am a beginner in VBA.

I would appreciate any help, hints or suggestions.

Thanks,

Laurent

Sep 11 '06 #2
Thanks for your hint Michael.

I do not have an issue with the field "date", as it is written as
"MyDate".

Anyway, anyone who is able to help me beginning the VBA code that
"should" be rather simple would help me a lot...

Regards,

Laurent


Michael Kintner wrote:
Just a quick note or hint, you want to make sure your field names do not
match key names. Such as Date is a key name which can conflict within a
query unless you rename your field to Contract_Date or enclose the Date
field within [], [Date].
"laurentc" <la*************@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
Dear all,

I have several tables based on exactly the same fields
(Key/Date/Price1/Price2).
I do some statistics on the prices.

However, as I have many different tables (the tables are different
because I directly import the data from a .csv file), I do not want to
create dozens of queries to be able to get the results of the different
tables.
Therefore, I would like to create a query in Access that will ask me
the table name from which to get the data each time I need.
Unfortunately, I find I cannot pass a table name as a Parameter to a
query.

I have to have to program a VBA code to construct a SQL string.
However, I cannot manage to build that code, as I am a beginner in VBA.

I would appreciate any help, hints or suggestions.

Thanks,

Laurent
Sep 11 '06 #3

laurentc wrote:
Dear all,

I have several tables based on exactly the same fields
(Key/Date/Price1/Price2).
I do some statistics on the prices.
Classic denormalized design. What happens if you need to add another
price for a Key/Date pair?
Hwever, as I have many different tables (the tables are different
because I directly import the data from a .csv file), I do not want to
create dozens of queries to be able to get the results of the different
tables.
another non-optimal design. Create ONE table containing all the data
and a flag field that tells you where the data came from (what import
file or whatever). I'm being picky, right? Well, consider that you
can create an unbound form that allows you to filter the records any
way you like and then you can base your reports etc on that.
>

Therefore, I would like to create a query in Access that will ask me
the table name from which to get the data each time I need.
you can use RunSQL to create the tables, but why? Seems like a lot of
effort for no reason. What do you do with the data once it has been
processed?

Sep 11 '06 #4
I agree with those who say you should combine the tables with an ID of their
origin to keep them identifiable. That said you could use code similar to
that below to pick the table to query.

__________________________________________________ ___
Sub setquery()
Dim Db As Database
Dim RS As Recordset
Dim qry As QueryDef
Dim strQryFirstpart As String
Dim strQryWhole As String
Dim strTable As String
Dim strQuote As String
Dim strTableName As String
Set Db = CurrentDb
strQryFirstpart = "Select * from "
strTableName = WhichTable()
strQryWhole = strQryFirstpart & strTableName
Set qry = Db.CreateQueryDef("", strQryWhole)
Set RS = qry.OpenRecordset()
End Sub

Function WhichTable()
Dim strhandback As String
strhandback = InputBox("Which table do you want?", "Table to Query?",
"")
WhichTable = strhandback
End Function
__________________________________________________ ____________
"laurentc" <la*************@gmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
Dear all,

I have several tables based on exactly the same fields
(Key/Date/Price1/Price2).
I do some statistics on the prices.

Sep 12 '06 #5
laurentc wrote:
Dear all,

I have several tables based on exactly the same fields
(Key/Date/Price1/Price2).
I do some statistics on the prices.

However, as I have many different tables (the tables are different
because I directly import the data from a .csv file), I do not want to
create dozens of queries to be able to get the results of the different
tables.
Therefore, I would like to create a query in Access that will ask me
the table name from which to get the data each time I need.
Unfortunately, I find I cannot pass a table name as a Parameter to a
query.

I have to have to program a VBA code to construct a SQL string.
However, I cannot manage to build that code, as I am a beginner in VBA.

I would appreciate any help, hints or suggestions.

Thanks,

Laurent
Whether you choose to put all these separate data drops into one large
table or keep them separate my suggestion is to keep a table with one
column that enumerates the tables to be processed. You could use this
meta-table as it were as the record source for a combo box to streamline
your analytics, or macro/VBA code that handles appending all the
sub-tables into the master table. In either case you could look to the
meta-table for table names and build specific SQL statements (in code)
from that.

HTH

--
Smartin
Sep 12 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Greg | last post by:
How can I use a variable as the table name?
2
by: James | last post by:
Can anyone please shed some light on the following... I have a framework that uses dynamically created tables, named using an incremental "attribute set ID", as follows: attrdata_1 attrdata_2...
2
by: Betrock | last post by:
This is probably very simple, but I just can't see my way thru it..... Short version: keyed values(numeric)in a lookup table are stored in a main table. They are displayed as text values - the...
1
by: Cory | last post by:
When I run the subroutine testEmailContacts the msgbox says that there is only 1 record. The sql for qyEmailContactsQyCard is below. There is over 3000 records in the table "tbl:Contact". What am i...
4
by: Sean Berry | last post by:
I have four tables that all have the same column names (50 in each.) I have created an admin program to edit, delete and add records to the tables and would like to use the table name as a...
3
by: ryanmhuc | last post by:
Is it possible to have a dynamic table name within a query or a table name that is a variable? This does not work but gives an example: SELECT * FROM concat('table', 'name') - OR - SET @table...
2
by: farhaaad | last post by:
Hi all, It would be kind if you if you tell me how to make a parameter query based on two fields, for example, I have two fields: PROVINCE and CITY. I say when I enter the Province, then it...
3
by: Andrea Raimondi | last post by:
Hello peers! I'm working on this application and I'm in need for some thoughtful advice :-p I have an SQLDataSource with params, select, etc. One of my params is the table name, which can be...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.