Results 1 to 5 of 5
  1. #1
    P38's Avatar
    P38 is offline Novice
    Windows Vista Access 2007
    Join Date
    Jul 2012
    Posts
    4

    Looking for VBA code to duplicate records

    For one special report that must meet the specification of a third party we must create a database that duplicates each multiple item individually as shown below:

    Our records:
    Item Quantity
    Red widget 1
    Yellow Widget 5
    Blue Widget 1
    Green Widget 3

    How it is to be reported:


    Item Quantity
    Red widget 1
    Yellow Widget 1
    Yellow Widget 1
    Yellow Widget 1
    Yellow Widget 1
    Yellow Widget 1
    Blue Widget 1
    Green Widget 1
    Green Widget 1
    Green Widget 1

    We have accomplished the duplication in Excel. Here is the Excel code (Quantity in Column "F"):
    Code:
     
    Sub RepeatRows()
    Dim lRow As Long
    Dim RepeatFactor As Variant
     
        lRow = 1
        Do While (Cells(lRow, "A") <> "")
           
            RepeatFactor = Cells(lRow, "F")
            If ((RepeatFactor > 1) And IsNumeric(RepeatFactor)) Then
                   
               Range(Cells(lRow, "A"), Cells(lRow, "F")).Copy
               Range(Cells(lRow + 1, "A"), Cells(lRow + RepeatFactor - 1, "F")).Select
               Selection.Insert Shift:=xlDown
              
               lRow = lRow + RepeatFactor - 1
            End If
       
            lRow = lRow + 1
        Loop
    End Sub
    I am not up on VBA code for Access so I am looking for help on the code that will do the duplication within Access 2007.

  2. #2
    help_me_with_access is offline help_me_with_excel
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    262
    depends what the tables look like...

  3. #3
    P38's Avatar
    P38 is offline Novice
    Windows Vista Access 2007
    Join Date
    Jul 2012
    Posts
    4
    Here is some additional information about the original table and the repeated rows table. The original table has 18 fields and 902 records. The repeated rows table does not require all the fields from the original table. A new table would be created which has the necessary fields plus one calculated field. The repeated rows table should grow to 2,664 records with duplication.

    Repeated rows table
    Field1 - record number (auto-number in original table)
    Field2 - text (255 characters max)
    Field3 - number (this field contains the quantity amount)
    Field4 - calculated number field (currency)
    Field5 - date (short date)
    Field6 - number linked to lookup table
    Field7 - number linked to lookup table

    Fields 1 and 3 do not appear in the report.

  4. #4
    P38's Avatar
    P38 is offline Novice
    Windows Vista Access 2007
    Join Date
    Jul 2012
    Posts
    4
    Since no one here had any ideas I went looking elsewhere and found Microsoft code for repeating records in a report http://support.microsoft.com/kb/207664. Modifying that code a little gave me my results. Red code deleted and bold code added.

    Code:
    Option Explicit
        Dim intPrintCounter As Integer
        Dim intNumberRepeats As Integer
    
        Private Sub Report_Open(Cancel As Integer)
           intPrintCounter = 1
           intNumberRepeats = Forms!PrintForm!TimesToRepeatRecord
           intNumberRepeats = 0
        End Sub
                           
        Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
           ' Note: intNumberRepeats and intPrintCounter are initialized
           ' in the report's OnOpen event.
        On Error GoTo -1
        If IsNull(Field3) = "True" GoTo Out
        intNumberRepeats = Field3
           If intPrintCounter < intNumberRepeats Then
              intPrintCounter = intPrintCounter + 1
              ' Do not advance to the next record.
              Me.NextRecord = False
           Else
              ' Reset intPrintCounter and advance to next record.
              intPrintCounter = 1
              Me.NextRecord = True
           End If
        Out:
        End Sub

  5. #5
    P38's Avatar
    P38 is offline Novice
    Windows Vista Access 2007
    Join Date
    Jul 2012
    Posts
    4

    Thumbs up

    Since no one here had any ideas I went looking elsewhere and found Microsoft code for repeating records in a report http://support.microsoft.com/kb/207664. Modifying that code a little gave me my results. Red code deleted and bold code added.

    Code:
    Option Explicit
        Dim intPrintCounter As Integer
        Dim intNumberRepeats As Integer
    
        Private Sub Report_Open(Cancel As Integer)
           intPrintCounter = 1
           intNumberRepeats = Forms!PrintForm!TimesToRepeatRecord
           intNumberRepeats = 0
        End Sub
                           
        Private Sub Detail_Print(Cancel As Integer, PrintCount As Integer)
           ' Note: intNumberRepeats and intPrintCounter are initialized
           ' in the report's OnOpen event.
        On Error GoTo -1
        If IsNull(Field3) = "True" GoTo Out
        intNumberRepeats = Field3
           If intPrintCounter < intNumberRepeats Then
              intPrintCounter = intPrintCounter + 1
              ' Do not advance to the next record.
              Me.NextRecord = False
           Else
              ' Reset intPrintCounter and advance to next record.
              intPrintCounter = 1
              Me.NextRecord = True
           End If
        Out:
        End Sub

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

Similar Threads

  1. Replies: 9
    Last Post: 06-26-2012, 04:11 PM
  2. Problem with code to prevent duplicate entries
    By ResearchRN in forum Programming
    Replies: 5
    Last Post: 11-08-2011, 12:12 PM
  3. Duplicate Check code with Run-Time error '3079'
    By viper in forum Programming
    Replies: 5
    Last Post: 10-18-2010, 10:12 AM
  4. Duplicate Records
    By softspoken in forum Queries
    Replies: 3
    Last Post: 06-21-2010, 03:33 PM
  5. Delete duplicate records
    By Zukster in forum Queries
    Replies: 1
    Last Post: 08-26-2009, 03:14 AM

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