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: - SELECT [TABLE1].ID, [TABLE1].[Business Date], [TABLE1].[ Code], [TABLE1].DOB, [TABLE1].[FA],
-
Len TABLE1].[FA]) AS LEN, [TABLE1].PD,
-
([TABLE1].PD/0.62,2) AS PD1, IIf([TABLE1].PD>=100,100,Round([TABLE1].PD/0.25,2)) AS PD2,
-
IIF(MID([TABLE1].[PDT],1,2)='HP','INDLE1',IIF(MID([TABLE1].[ PDT],1,8)='Staff','INDLE1','NIND')) AS CAT
-
FROM TABLE1;
2 3624
Hi
I would suggest something like this perhaps - Sub UseTableVariable()
-
Dim i As Integer
-
Dim TableName As String
-
Dim sql As String
-
-
For i = 1 To 10
-
-
Select Case i
-
Case Is = 1
-
TableName = "TABLE1"
-
Case Is = 2
-
TableName = "TABLE2"
-
Case Is = 3
-
TableName = "TABLE3"
-
Case Is = 4
-
TableName = "TABLE4"
-
Case Is = 5
-
TableName = "TABLE5"
-
Case Is = 6
-
TableName = "TABLE6"
-
Case Is = 7
-
TableName = "TABLE7"
-
Case Is = 8
-
TableName = "TABLE8"
-
Case Is = 9
-
TableName = "TABLE9"
-
Case Is = 10
-
TableName = "TABLE10"
-
End Select
-
-
sql = "SELECT [" & TableName & "].ID, " & _
-
"[" & TableName & "].[Business Date], " & _
-
"[" & TableName & "].[ Code], " & _
-
"[" & TableName & "].DOB, [" & TableName & "].[FA], " & _
-
"Len " & TableName & "].[FA]) AS LEN, " & _
-
"[" & TableName & "].PD, " & _
-
"([" & TableName & "].PD/0.62,2) AS PD1, " & _
-
"IIf([" & TableName & "].PD>=100,100,Round([" & TableName & "].PD/0.25,2)) AS PD2, " & _
-
"IIF(MID([" & TableName & "].[PDT],1,2)='HP','INDLE1',IIF(MID([" & TableName & "].[ PDT],1,8)='Staff','INDLE1','NIND')) AS CAT " & _
-
"FROM " & TableName & ";"
-
-
'######## USE THE SQL HERE
-
Next i
-
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
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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....
|
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...
|
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...
|
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. ...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
| |
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...
|
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,...
|
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...
|
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...
|
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,...
|
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...
| |
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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 ...
| |