Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Microsoft Office > Access and Access VBA > Access
Password Reminder
Register
| FAQ | Members List | Calendar | 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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #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
Reply With Quote
  #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
Reply With Quote
  #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
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 06:41 AM.


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