Results 1 to 5 of 5
  1. #1
    Niborgst is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    3

    Transfer singel and duplicates into an other table

    Hello all



    I have a table with unique records which I have to transfer into a second table. Now in the source table is an attribute "count". I will transfer each record into the result table, but I need each record the amount of the value from "count".

    for instance: record 1 has a count value: 1. so it is a single transfer. record 2 has the value 3, so I need record 2 times 3 into the result table.

    my vba script looks like this:

    Code:
    Dim row As Integer
    Dim rs As Recordset
    Dim i As Integer
    Dim Count As Integer
    Set rs = CurrentDb.OpenRecordset("my_source")
    duplicates= rs.Fields("Count").Value
    
    For row = 1 To rs.RecordCount
    
    For i = 1 To duplicates
    DoCmd.RunSQL ("INSERT INTO result_table SELECT .... FROM my_source "WHERE rss.RowNo =" & row)
    next
    
    Next

    unforthunately the second loop is not working since the value "duplicates" is always from the first row.

    Do you have any good advice for my small problem?

    Best regards Niborgst

  2. #2
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    rs.MoveFirst
    ...
    rs.MoveNext

  3. #3
    Niborgst is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    3
    Hello aytee111
    Thank you for you input. I am sorry, I'm not used to that function.
    how should I implement that?
    Code:
    ....
    rs.MoveFirst
    For row = 1 To rs.RecordCount
    
    For i = 1 To duplicates
    DoCmd.RunSQL ("INSERT INTO result_table SELECT .... FROM my_source "WHERE rss.RowNo =" & row)
    next
    
    rs.MoveNext
    Next
    ...
    or without a for loop?

    Best regards Niborgst

  4. #4
    aytee111 is offline Competent At Times
    Windows 10 Access 2013 64bit
    Join Date
    Nov 2011
    Location
    Nomad
    Posts
    3,936
    Code:
    Dim rs As Recordset, rsOut as Recordset
    Dim i As Integer
    
    Set rs = CurrentDb.OpenRecordset("my_source")
    Set rsOut = CurrentDb.OpenRecordset("result_table")
    
    rs.MoveFirst
    Do Until rs.EOF
     duplicates= rs!Count
     For i = 1 To duplicates
      rsOut.AddNew
       rsOut!field1=rs!field1
       rsOut!number=i
       ...
      rsOut.Update
     next
     rs.MoveNext
    Loop
    Set rs=Nothing
    Set rsOut=Nothing

  5. #5
    Niborgst is offline Novice
    Windows 7 64bit Access 2013 64bit
    Join Date
    Jun 2017
    Posts
    3
    This works. thank you very much

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

Similar Threads

  1. Vba code to transfer attachment to a table
    By ludovic_44 in forum Access
    Replies: 1
    Last Post: 01-10-2015, 02:01 PM
  2. Replies: 3
    Last Post: 04-28-2014, 07:22 AM
  3. Replies: 11
    Last Post: 01-15-2014, 02:02 PM
  4. Replies: 10
    Last Post: 11-19-2012, 11:05 AM
  5. Replies: 3
    Last Post: 04-23-2006, 09:09 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