Results 1 to 3 of 3
  1. #1
    major6000 is offline Novice
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2012
    Posts
    1

    duplicate table records multiple times according to condition

    Hi All.

    I have one table like:

    ID Type No
    -----------------
    1 FL 2
    2 DL 3


    I need the following table to be created using first table , each record should be duplicated according to value in No field:

    ID Type No
    -----------------
    1 FL 2
    1 FL 2
    2 DL 3
    2 DL 3
    2 DL 3





    Any help will be appreciated

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows XP Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Run multiple INSERT sql actions. You can manually run an Access query or use VBA code and recordset to automate.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    recyan's Avatar
    recyan is offline Expert
    Windows 2K Access 2000
    Join Date
    Dec 2011
    Posts
    662
    Just check if below crude function gives some guidelines :

    myTable - the original table
    myTable_2 - the final table

    Code:
    Public Function CopyRecordsVariableTimes()
        Dim strsqlMakeTable
        Dim strsql
        Dim strsql_1
        Dim strsql_2
        Dim i
        Dim j
        Dim IDCount
        Dim db As DAO.Database, rst As DAO.Recordset, sSQL As String
        Dim rsta As DAO.Recordset
        Dim rstb As DAO.Recordset
        Dim TheNoOffa
    
        'Now the code sets the current database to the one we jsut worked on
        Set db = CurrentDb()
    
        ' Create the new table
        strsqlMakeTable = "SELECT myTable.ID, myTable.Type, myTable.NoOff INTO myTable_2 FROM myTable WHERE 1 = 2;"
        db.Execute strsqlMakeTable, dbFailOnError
    
        ' get the count of Records in the Main table
        strsql = "SELECT Count(myTable.ID) AS CountOfID FROM myTable;"
        'Debug.Print strsql
        
        Set rsta = db.OpenRecordset(strsql, dbOpenDynaset)
        
        IDCount = rsta![CountOfID]
    
        For i = 1 To IDCount
        
            strsql_1 = "SELECT TOP 1 a.ID, a.Type, a.NoOff AS TheNoOff FROM (SELECT TOP " & i & " [ID], [Type], NoOff FROM myTable ORDER BY [ID]) AS a ORDER BY [ID] DESC;"
            'Debug.Print strsql_1
            
            Set rstb = db.OpenRecordset(strsql_1, dbOpenDynaset)
            
            TheNoOffa = rstb![TheNoOff]
            'Debug.Print TheNoOffa
    
            For j = 1 To TheNoOffa
            
                strsql_2 = "INSERT INTO myTable_2 ( ID, Type, NoOff ) " & strsql_1
                db.Execute strsql_2, dbFailOnError
                'Debug.Print strsql_2
                
            Next j
            
        Next i
    
    MsgBox "Completed successfully."
    
    End Function
    Thanks

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 03-23-2012, 11:40 AM
  2. Start and Stop times where condition true
    By cheshire_smile in forum Queries
    Replies: 3
    Last Post: 07-05-2011, 09:59 PM
  3. show duplicate records when appending a table
    By bdaniel in forum Programming
    Replies: 1
    Last Post: 06-01-2011, 07:15 AM
  4. Replies: 10
    Last Post: 04-19-2011, 03:38 PM
  5. Replies: 5
    Last Post: 12-10-2009, 10:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums