在 vba 和 access 上使用记录集,如何复制最后一条记录并更改它的字段
working with recordset on vba and access , how can I copy the last record and change a field of it
我在 table 调用 "MyData" 中有我的源数据,我可以在其中查看我有多少产品以及它们是什么类型的产品。
如果产品是产品类型 A,我只创建一个补偿规则。但是,如果是B类产品,我需要创建两个补偿规则。
我或多或少使用了以下算法
rsrule=db.OpenRecordset("Rules", dbOpenTable)
Do While Not rsdd.EOF
rsrule.AddNew
Do stuff
Detect the type of the product
type="B"
flag= true
rsrule.Update
now I want to add the same rule I have just added and change only one field rsrule![compensation percentage]=0.25
rsdd.MoveNext
Loop
任何人都可以指出正确的方向来完成这个吗?
非常感谢
这看起来像是产品和补偿规则之间的一对多关系。每个产品可以有很多补偿规则。因此,相应地构建数据库:
产品 table 为库存中的每个产品分配 1 个 ProductID。添加了 ProductTypes 和 CompensationRuleTypes tables,以便数据库可以扩展以处理新产品和补偿规则。 MyDataTable 没有其他信息,因此如果最终用户需要执行诸如添加产品之类的操作,请将其替换为表单;如果最终用户只需要查看产品计数,则将其替换为报告。
接下来您似乎需要 vba 代码来填充所有产品的 ProductCompensationRules table。此代码还检查是否已添加适当的规则并在这种情况下跳过它们:
Private Sub FillCompensationRulesTableUsingVBA()
Dim rsProducts As dao.Recordset
Set rsProducts = CurrentDb.OpenRecordset("Products", dbOpenDynaset)
Dim rsProductsCompensationRules As dao.Recordset
Set rsProductsCompensationRules = CurrentDb.OpenRecordset("ProductCompensationRules", dbOpenDynaset)
rsProducts.MoveFirst
Do While Not rsProducts.EOF 'loop through products and add compensation rule 2 for all products and compensation rule 1 for producttype 2
If IsNull(DLookup("ProductID", "ProductCompensationRules", "ProductID = " & rsProducts!ProductID & " AND CompensationRuleID = 2")) Then
With rsProductsCompensationRules
.AddNew
!ProductID = rsProducts!ProductID
!CompensationRuleID = 2
.Update
End With
End If
If rsProducts!ProductTypeID = 2 Then
If IsNull(DLookup("ProductID", "ProductCompensationRules", "ProductID = " & rsProducts!ProductID & " AND CompensationRuleID = 1")) Then
With rsProductsCompensationRules
.AddNew
!ProductID = rsProducts!ProductID
!CompensationRuleID = 1
.Update
End With
End If
End If
rsProducts.MoveNext
Loop
'clean up
rsProducts.Close
rsProductsCompensationRules.Close
Set rsProducts = Nothing
Set rsProductsCompensationRules = Nothing
End Sub
我在 table 调用 "MyData" 中有我的源数据,我可以在其中查看我有多少产品以及它们是什么类型的产品。
如果产品是产品类型 A,我只创建一个补偿规则。但是,如果是B类产品,我需要创建两个补偿规则。
我或多或少使用了以下算法
rsrule=db.OpenRecordset("Rules", dbOpenTable)
Do While Not rsdd.EOF
rsrule.AddNew
Do stuff
Detect the type of the product
type="B"
flag= true
rsrule.Update
now I want to add the same rule I have just added and change only one field rsrule![compensation percentage]=0.25
rsdd.MoveNext
Loop
任何人都可以指出正确的方向来完成这个吗?
非常感谢
这看起来像是产品和补偿规则之间的一对多关系。每个产品可以有很多补偿规则。因此,相应地构建数据库:
产品 table 为库存中的每个产品分配 1 个 ProductID。添加了 ProductTypes 和 CompensationRuleTypes tables,以便数据库可以扩展以处理新产品和补偿规则。 MyDataTable 没有其他信息,因此如果最终用户需要执行诸如添加产品之类的操作,请将其替换为表单;如果最终用户只需要查看产品计数,则将其替换为报告。
接下来您似乎需要 vba 代码来填充所有产品的 ProductCompensationRules table。此代码还检查是否已添加适当的规则并在这种情况下跳过它们:
Private Sub FillCompensationRulesTableUsingVBA()
Dim rsProducts As dao.Recordset
Set rsProducts = CurrentDb.OpenRecordset("Products", dbOpenDynaset)
Dim rsProductsCompensationRules As dao.Recordset
Set rsProductsCompensationRules = CurrentDb.OpenRecordset("ProductCompensationRules", dbOpenDynaset)
rsProducts.MoveFirst
Do While Not rsProducts.EOF 'loop through products and add compensation rule 2 for all products and compensation rule 1 for producttype 2
If IsNull(DLookup("ProductID", "ProductCompensationRules", "ProductID = " & rsProducts!ProductID & " AND CompensationRuleID = 2")) Then
With rsProductsCompensationRules
.AddNew
!ProductID = rsProducts!ProductID
!CompensationRuleID = 2
.Update
End With
End If
If rsProducts!ProductTypeID = 2 Then
If IsNull(DLookup("ProductID", "ProductCompensationRules", "ProductID = " & rsProducts!ProductID & " AND CompensationRuleID = 1")) Then
With rsProductsCompensationRules
.AddNew
!ProductID = rsProducts!ProductID
!CompensationRuleID = 1
.Update
End With
End If
End If
rsProducts.MoveNext
Loop
'clean up
rsProducts.Close
rsProductsCompensationRules.Close
Set rsProducts = Nothing
Set rsProductsCompensationRules = Nothing
End Sub