Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsFormsForms ProgrammingQueriesModules / DAO / VBAReports / PrintingMacrosDatabase DesignSecurityConversionImporting / LinkingSQL Server / ADPMultiuser / NetworkingReplicationSetup / ConfigurationDeveloper ToolkitsActiveX ControlsNew UsersGeneral 1General 2
Access DirectoryToolsTutorialsUser Groups
Related Topics
SQL ServerOther DB ProductsMS OfficeMore Topics ...

MS Access Forum / General 1 / September 2006

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
laurentc - 11 Sep 2006 17:04 GMT
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
Michael Kintner - 11 Sep 2006 17:58 GMT
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].

> Dear all,
>
[quoted text clipped - 21 lines]
>
> Laurent
laurentc - 11 Sep 2006 18:04 GMT
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

> 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
[quoted text clipped - 26 lines]
> >
> > Laurent
pietlinden@hotmail.com - 11 Sep 2006 19:59 GMT
> 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?
Kc-Mass - 12 Sep 2006 15:46 GMT
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
______________________________________________________________

> Dear all,
>
> I have several tables based on exactly the same fields
> (Key/Date/Price1/Price2).
> I do some statistics on the prices.
Smartin - 12 Sep 2006 18:05 GMT
> Dear all,
>
[quoted text clipped - 21 lines]
>
> 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

Signature

Smartin

laurentc - 30 Sep 2006 15:33 GMT
Thanks for your advice.
Please excuse-me not to thank you before.
I eventually keep the numerous tables with their names thanks to a form,
numerous txtbox, and some VBA code...

Thanks for your help, guys !

L.

>> Dear all,
>>
[quoted text clipped - 12 lines]
>
>HTH
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.