473,513 Members | 2,492 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Repeat the same query for different tables

1 New Member
For example, I have 10 tables in my db and I have created a query based on table 2. Instead of copy and paste query for 10 times, is there anyway using macro or vba to replicate this for the remaining tables?

My query sql code, says is as below:
Expand|Select|Wrap|Line Numbers
  1. SELECT [TABLE1].ID, [TABLE1].[Business Date], [TABLE1].[ Code], [TABLE1].DOB, [TABLE1].[FA], 
  2. Len TABLE1].[FA]) AS LEN, [TABLE1].PD,
  3. ([TABLE1].PD/0.62,2) AS PD1, IIf([TABLE1].PD>=100,100,Round([TABLE1].PD/0.25,2)) AS PD2,
  4. IIF(MID([TABLE1].[PDT],1,2)='HP','INDLE1',IIF(MID([TABLE1].[ PDT],1,8)='Staff','INDLE1','NIND')) AS CAT
  5. FROM TABLE1;
Oct 25 '13 #1
2 3624
MikeTheBike
639 Recognized Expert Contributor
Hi

I would suggest something like this perhaps
Expand|Select|Wrap|Line Numbers
  1. Sub UseTableVariable()
  2.     Dim i As Integer
  3.     Dim TableName As String
  4.     Dim sql As String
  5.  
  6.     For i = 1 To 10
  7.  
  8.         Select Case i
  9.             Case Is = 1
  10.                 TableName = "TABLE1"
  11.             Case Is = 2
  12.                 TableName = "TABLE2"
  13.             Case Is = 3
  14.                 TableName = "TABLE3"
  15.             Case Is = 4
  16.                 TableName = "TABLE4"
  17.             Case Is = 5
  18.                 TableName = "TABLE5"
  19.             Case Is = 6
  20.                 TableName = "TABLE6"
  21.             Case Is = 7
  22.                 TableName = "TABLE7"
  23.             Case Is = 8
  24.                 TableName = "TABLE8"
  25.             Case Is = 9
  26.                 TableName = "TABLE9"
  27.             Case Is = 10
  28.                 TableName = "TABLE10"
  29.         End Select
  30.  
  31.         sql = "SELECT [" & TableName & "].ID, " & _
  32.               "[" & TableName & "].[Business Date], " & _
  33.               "[" & TableName & "].[ Code], " & _
  34.               "[" & TableName & "].DOB, [" & TableName & "].[FA], " & _
  35.               "Len " & TableName & "].[FA]) AS LEN, " & _
  36.               "[" & TableName & "].PD, " & _
  37.               "([" & TableName & "].PD/0.62,2) AS PD1, " & _
  38.               "IIf([" & TableName & "].PD>=100,100,Round([" & TableName & "].PD/0.25,2)) AS PD2, " & _
  39.               "IIF(MID([" & TableName & "].[PDT],1,2)='HP','INDLE1',IIF(MID([" & TableName & "].[ PDT],1,8)='Staff','INDLE1','NIND')) AS CAT " & _
  40.               "FROM " & TableName & ";"
  41.  
  42.         '######## USE THE SQL HERE
  43.     Next i
  44. End Sub
If the table names are truely sequetial with a common text as illustarted then the Select Case construct can be replaced with this

TableName = "TABLE" & i

but the Select Case you can specify any table name for each pass.

??

MTB
Oct 25 '13 #2
NeoPa
32,557 Recognized Expert Moderator MVP
If you have the same design duplicated in multiple tables then, almost certainly, you need to look at your design in light of the concepts of Database Normalisation and Table Structures.

The concept you're using (of splitting data between tables based on an attribute rather than the fundamental design) can, and will, cause you (or whoever comes after you to fix it) a great deal of grief.

MTB has answered your question, but I would strongly suggest that you take a step back and look at redesigning rather than follow an approach that will almost certainly lead you into many troubles.

Good luck.
Oct 25 '13 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
6306
by: sql-db2-dba | last post by:
We have DB2 UDB v8.1 fixpak3 on AIX 5. Production and Development configuarations (at least for DB2) are identical albeit production is a 2-way server while development has only one processor....
10
15342
by: Marizel | last post by:
I'm not sure there's an easy solution to this, but thought I'd ask. I often find myself with a query which I'd like to reuse, but with a different datasource. These datasources generally have...
4
2439
by: satish | last post by:
Values of two columns in two different tables--presentation using select Hi Everyone, i have two tables in the database . One is called address table and one is adressPhone Table. Below...
1
3391
by: fong.yang | last post by:
I've got a database with about 300,000 records. There are several different tables that are set up the same way with identical fields. I have the same queries individually set up for each table. ...
2
7302
by: sangita | last post by:
i don't know how to write module or query for the following problem in access i have two tables in one access database where id no &record no is same in two different tables, also fields a1, a2,...
5
10729
by: sqlgirl | last post by:
Hi, We are trying to solve a real puzzle. We have a stored procedure that exhibits *drastically* different execution times depending on how its executed. When run from QA, it can take as...
8
4329
by: phatz | last post by:
Hi, I have a schema with 10 tables. I want to run the same query on all 10 of these tables and output the results from the SQL query to a .txt file but am unsure how to do this. I know this will...
17
8374
reginaldmerritt
by: reginaldmerritt | last post by:
I have an OpenForm with a where statement which throws up an error because the form i'm trying to open uses a query which has two fields from different tables sharing the same name. Let me explain...
6
3191
HaLo2FrEeEk
by: HaLo2FrEeEk | last post by:
I have two different tables which havea different number of columns. One of the tables gets a new row every day and is populated with a st of 4 IDs from another table, along with today's date and an...
2
3619
by: asmodea | last post by:
I am trying to add a calculated field in my query to sum the payments up until the date of the entry. There are two tables involved. I have a table for the dates and amounts of wired payments...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7161
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
7384
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
7539
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...
1
7101
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5686
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
3234
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
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 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.