Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Generate duplicate records in Access 2000

Status
Not open for further replies.
Dec 20, 2001
4
GB
Can anyone please suggest code to generate a sequentially numbered range of records (the quantity of records will vary) counting from a selected integer field (ideally the Primary Key) in a Table. All other Fields in the record set would keep the same values. Thanks
 
Try writing the sqlstr you require, open that as a recordset, and then clone the recordset.

You were a bit vague with your question so that is the best I can do. Elaborate on what you are trying to do? Do you want to copy a section of a table to another table? Or manipulate a section of data from a table in code?
 
Sorry I was so vague! What I want to do is generate, say, 20 new records numbered 2346 to 2365 but keeping the exact same data from record 2345 shown in its Fields 1,2,3 etc. Does this make more sense?

Unique Field1 Field2 Field3
1234 Fred Jim 03/02/2003
2345 Sam Ken 01/02/1999
4567 Bob Dave 06/12/2002
 
Try this:
Function AddDupRecords(i as integer, j as integer)
Dim dbs as database
Dim rst as recordset
Dim x as integer
Dim rst1 as recordet, sqlstr as string

set dbs = currentdb
set rst = dbs.openrecordset("table1")
sqlstr = SELECT * FROM table1 WHERE Unique = " & i & ";"
set rst1 = dbs.openrecordset(sqlstr)
For x = i to i+j
with rst
.addnew
.fields("unique") = x
.fields("field1") = rst1.field1
'etc for all fields
.update
end with
x = x+1
Loop
rst1.close
rst.close
set dbs = nothing
End sub

From the debug window run ?AddDupRecords(<record to copy>, <number of copies>)
 
Very many thanks for your help.
Doubt I'd have ever got there!
Will give this a try.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top