创建许多记录,这些记录等于表字段的值

时间:2018-11-22 14:27:57

标签: access-vba

我有一个包含IDproduct numberproduct descriptionqty字段的表。

现在,我想创建一个新表,其中包含由父表的qty字段给出的大量记录。

例如,如果父表中有一个带有qty = 6的记录,那么它应该在子表中创建6记录,其序列号从1到6。

2 个答案:

答案 0 :(得分:1)

您可以使用一个简单的循环,如下所示:

Dim rs As DAO.Recordset
Dim Record As Integer
Dim Records As Integer

Set rs = CurrentDb.OpenRecordset("Select * From YourTable")
' Count of records - use value from your quantity field:
Records = 5 

For Record = 1 To Records
    rs.AddNew
        rs!SomeID.Value = Record
        rs!Field1.Value = SomeValue
        rs!Field2.Value = SomeOtherValue
        rs!Field3.Value = YetAnotherValue
   rs.Update
Next
rs.Close

Set rs = Nothing

答案 1 :(得分:1)

您可以使用下面的代码。

Dim rsTable1 as Recordset
Dim rsTable2 as Recordset
Dim Table1qty as integer
Dim i as integer

set rsTable1 = CurrentDb.openrecordset("SELECT qty FROM Table1")
set rsTable2 = CurrentDB.openRecordSet("SELECT * From Table2")

if rsTable1.RecordCount > 0 then
    while not rsTable1.EOF

        Table1qty = rsTable("qty")

        for i = 1 to Table1qty
           rsTable2.Addnew
           rsTable2!SerialNumber.Value = i
           rsTable2!SomeField.Value = SomeValue
           rsTable2.Update
         next 

     rsTable1.movenext
     wend
end