Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
| Search | Today's Posts | Mark Forums Read
Access Discussion of Microsoft Access database design and programming. See also the forums for Access ASP and Access VBA.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Access section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old January 21st, 2005, 12:43 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: South Bend, Indiana, USA.
Posts: 149
Thanks: 0
Thanked 0 Times in 0 Posts
Default Copy Header and Details to similar tables

I have a header table and several related detail tables.

I am trying to copy them "all" (one specific and complete record, that is) to almost identical header and detail tables. (The original tables are basically holding templet "prompt" data, that the "copy" tables will actually hold all the actual collected data in)

I'm not sure how to go about it, especially since the detial tables will ultimately need the key field from the header table so they can be linked up. Since I will repeatedly use the original tables (the copy tables will hold many duplicates of them) I know I can't just use their key field IDs.

Do I do this with queies or VBA or both or what?

I'm using Access 2000 MDB.
__________________
Mitch
  #2 (permalink)  
Old February 2nd, 2005, 07:32 PM
Authorized User
 
Join Date: Jun 2003
Location: Califon, NJ, USA.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You'll need to do this in steps, and for that, I think you'll need to use VB or VBA. Essentially, you want to read the first row from the header table and write it to the second header table, then immediately reread the second table to get the new record's primary key (assuming it's an AutoNumber field). Once you have the new primary key, you need to process all of the detail records for the original header record. For each one, change the foreign key field to the value of the new header record's primary key, then insert the detail record into the second detail table. Your code might look something like this pseudo code:

    Get all Header1 records
    Do While Not Header1.EOF
        Write the current record to the Header2 table
        Get the highest primary key on the Header2 table and save

        Get all Detail1a records for the current Header1 record
        Do While Not Detail1a.EOF
            Write record to Detail2a with new foreign (primary) key
        Loop

        Get all Detail1b records for the current Header1 record
        Do While Not Detail1b.EOF
            Write record to Detail1b with new foreign (primary) key
        Loop

        .
        etc.
        .
    Loop

Hope this helps.

Pete
  #3 (permalink)  
Old February 2nd, 2005, 07:34 PM
Authorized User
 
Join Date: Jun 2003
Location: Califon, NJ, USA.
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Sorry, there was a typo in the pseudo code. It should look like this:

    Get all Header1 records
    Do While Not Header1.EOF
        Write the current record to the Header2 table
        Get the highest primary key on the Header2 table and save

        Get all Detail1a records for the current Header1 record
        Do While Not Detail1a.EOF
            Write record to Detail2a with new foreign (primary) key
        Loop

        Get all Detail1b records for the current Header1 record
        Do While Not Detail1b.EOF
            Write record to Detail2b with new foreign (primary) key
        Loop

        .
        etc.
        .
    Loop

Pete


Similar Threads
Thread Thread Starter Forum Replies Last Post
Linking two similar pivot tables together exceldummy Excel VBA 1 May 2nd, 2007 10:09 AM
copy tables and stored procs between database lokey VB Databases Basics 1 March 5th, 2007 02:40 PM
Query database for List of tables & other details imsuneeta SQL Server 2000 2 January 18th, 2005 07:48 AM
Here is some similar problem amit_bhatia Beginning PHP 1 December 7th, 2004 11:30 AM
Copy tables from one Access DB to Another mckly VB Databases Basics 1 August 4th, 2004 09:52 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.