Solved

ACCESS VBA to merge  tables with additional field

Posted on 2014-10-20
6
601 Views
Last Modified: 2014-10-22
Hi ,

    I have 10 tables of the identical structure Tb1, Tb2 ..TB10
 
   The  TB_merge is a table to merge all the rows in the 10 tables . But  with an  additional column called  Origin .  This is to identify the origin of the row ( This  For each row  from Tb1,  the column  Origin  should be filled with Tb2 .. For each row from Tb2 the column should be filled with Tb2 )

 How can I do this in a VBA ( or any other means )

 Thanks
0
Comment
Question by:Sam OZ
6 Comments
 
LVL 17

Assisted Solution

by:Barry Cunney
Barry Cunney earned 150 total points
ID: 40391628
Hi Sam,
An INSERT INTO .... SELECT... UNION query may be a possible option:
The following is an example

INSERT TB_merge
SELECT 
*,
'TB_01' AS [Origin]
FROM
[Tb1]
UNION
SELECT 
*,
'TB_02' AS [Origin]
FROM
[Tb2]
UNION
SELECT 
*,
'TB_03' AS [Origin]
FROM
[Tb3]
.....
.....

Open in new window



So in MS Access you can create a new query and choose View | SQL to get to a free format SQL window and then you can create SQL similar to that above to SELECT all rows from the different source tables and INSERT INTO TB_merge
0
 
LVL 57

Assisted Solution

by:Jim Dettman (Microsoft MVP/ EE MVE)
Jim Dettman (Microsoft MVP/ EE MVE) earned 100 total points
ID: 40391678
I would not bother with the UNIONs; just do ten separate queries, one for each table that you want to append.

If this is a one time thing, you can execute them manually.

If via VBA, use CurrentDB().execute <SQL>  , dbFailOnError

to execute the SQL directly

or:

Dim qryDef as DAO.Querydef

set qrydef = CurrentDB().querydef("<name of query>")

qrydef.execute, dbFailOnError

Jim.
0
 

Author Comment

by:Sam OZ
ID: 40391690
Thanks for the comments .  I was looking for a way of automation than manual . Because the number of Tables can change later ( But I can assume it will be tables starting with TB_)

 Infact, My real need is to have  a column by name Origin  to identify from which Table  the row is originated from
0
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
ID: 40391733
Or another option, since you don't know how many tables you are going to have, write a VBA subroutine which loops through the tables (you could either pass the table names in as a parameter array or if they all have the same name with the exception of the value 01-10, then you could just build the table name at run time.

Adding the Origin is actually the easy part.
0
 
LVL 119

Accepted Solution

by:
Rey Obrero earned 250 total points
ID: 40391825
try these codes

Dim td As DAO.TableDef, db As DAO.Database
Set db = CurrentDb
For Each td In db.TableDefs
If td.Name <> "TB_Merge" Then
    If Left(td.Name, 3) = "TB_" Then
        CurrentDb.Execute "Insert into TB_merge select [" & td.Name & "].* From [" & td.Name & "]"
        CurrentDb.Execute "Update Tb_merge set origin='" & td.Name & "' where Origin is null"
    End If
End If
Next

Open in new window

0
 
LVL 30

Expert Comment

by:hnasr
ID: 40391927
Comments above are fine.
You mentioned: " I was looking for a way of automation than manual . "

Here is a comment to try.
Assume in a form 3 text boxes (prefix, noFrom, noTo) and a command button (processDo
Private Sub processDo_Click()
Dim prefix As String
    Dim sql As String
    Dim i As Integer
    'validate fields
    If Me.prefix <> "" Then
    Else
        Me.prefix = "TB_"
    End If
    If Me.NoTo < NoFrom Then
        Me.NoTo = Me.NoFrom
    End If
    'loop to process
    DoCmd.SetWarnings off
    For i = Me.NoFrom To Me.NoTo
        sql = "INSERT INTO " & Me.prefix & "merge" _
             & " SELECT * FROM " & Me.prefix & i
        DoCmd.RunSQL sql
    Next
    DoCmd.SetWarnings True
End Sub

Open in new window

0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I originally created this report in Crystal Reports 2008 where there is an option to underlay sections. I initially came across the problem in Access Reports where I was unable to run my border lines down through the entire page as I was using the P…
A theme is a collection of property settings that allow you to define the look of pages and controls, and then apply the look consistently across pages in an application. Themes can be made up of a set of elements: skins, style sheets, images, and o…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now