Consulting

Results 1 to 7 of 7

Thread: Solved: Insert data from one table into another

  1. #1
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    204
    Location

    Solved: Insert data from one table into another

    I have two tables, TableA and TableB with same structure. Each may/may not contain the same data. I want to add (append) to TableA Those records in TableB whose data in a chosen column is not equal to those in the same column in TableA.

    It would seem that a "simple" SQL should do it..but not for me!

  2. #2
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    Do both these tables have primary keys?

  3. #3
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    204
    Location
    To XCav8r: Neither table has a primary key. ???

  4. #4
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    There are a few ways you could go about this. You might try the following steps:
    1. Use an append query to add the data from TableB to TableA. (Or create a new table with a make table query using TableA, then use TableB in the append query to add its data.)
    2. Use a SELECT DISTINCT query to get unique values.
    3. Use the SELECT DISTINCT query to make the desired table.
    Another method:
    1. Create a SELECT query for each table.
    2. Use a UNION query to join the unique values.
    3. Use the UNION query to make the desired table.
    SELECT * FROM TableA
    UNION
    SELECT * FROM TableB

  5. #5
    VBAX Tutor
    Joined
    Mar 2005
    Posts
    204
    Location
    Looks like I marked "Solved" too soon, but hetre is something that works.

    And it is MUCH more complicated than that suggested by Cav8r:
    First, append one table to the other, then


    [VBA] Public Sub Sweep1(ByVal Table As String)
    Dim db As DAO.Database
    Dim rst As DAO.Recordset
    Dim Dup1 As String
    Dim Dup2 As String
    Dim Dup As String
    Dim index1 As Integer
    Dim index2 As Integer
    Dim TableSize As Integer
    Set db = CurrentDb()
    Set rst = db.OpenRecordset(Table)
    TableSize = rst.RecordCount
    With rst
    For index2 = 1 To TableSize
    index1 = 1
    .MoveFirst
    Do Until .EOF
    Dup = ![Key]
    If (index1 = index2) Then
    Dup1 = Dup
    Else
    Dup2 = Dup
    End If
    If ((Dup1 = Dup2) And (index1 > index2)) Then
    .Delete
    End If
    .MoveNext
    index1 = index1 + 1
    Loop
    Next index2
    .Close
    End With
    End Sub
    [/VBA]
    Last edited by xCav8r; 08-06-2005 at 08:47 PM. Reason: Corrected VBA Tags

  6. #6
    VBAX Master Norie's Avatar
    Joined
    Jan 2005
    Location
    Stirling, Scotland
    Posts
    1,831
    Location
    mud

    Do you really need code for this?

    From the code it certainly looks like there are some linked fields between the tables.

    Perhaps you can give more information on the table structure, also what/where should records be appended and according to what criteria.
    Those fields might not have a defined relationship, but you could create one in a query.

  7. #7
    VBAX Expert xCav8r's Avatar
    Joined
    May 2005
    Location
    Minneapolis, MN, USA
    Posts
    912
    Location
    Yeah, it's rather tough to help without the specifics.

    PS. Mud, I think the VBA tag has to be enclosed by square brackets, ie., [VBA] and [/VBA]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •