Solved

ACCESS VBA to merge  tables with additional field

Posted on 2014-10-20
6
573 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

 
LVL 47

Expert Comment

by:Dale Fye (Access MVP)
Comment Utility
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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
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…

743 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

8 Experts available now in Live!

Get 1:1 Help Now