Microsoft Access VBA填充总记录百分比的字段

时间:2017-05-22 20:27:34

标签: sql vba ms-access

当我点击按钮command9时,我想使用count函数计算匹配combo7Insp_Cat = 1的记录数,然后计算总记录的20%。然后使用Update语句将字段Insp_Type更新为“C”,将记录数限制为先前计算的总记录的20%。

这是我迄今为止的代码,但在计数行上出现语法错误。

Private Sub Command9_Click()
Dim strSql As String
Dim Rec_Qty As Integer
Dim Rec_Perc As Integer
'Return record count for all records in Tbl_Inspections matching WO_ID in Combo7 and Insp_Cat =1

Rec_Qty = Count (WO_ID & Insp_Cat) Where [WO_ID]= Me.[Combo7]& [Insp_Cat]=1 From Tbl_Inspections

Rec_Per = Rec_Qty * 0.2

'Update records for "C" 20% records using Rec_Per value in limit function of Update command
strSql = "Update Tbl_Inspections"
strSql = strSql & "Set Insp_Type = 'C' WHERE WO_ID = Me.Combo7 & Insp_Cat = 1 & Limit = Rec_Per"
CurrentDb.Execute strSql
End Sub

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

不能在VBA中使用Count()函数,只能在查询或表单或报表的文本框中使用。

DCA()和VBA中使用的其他域聚合函数。

连接文字文本和变量。

Rec_Qty = DCount("*", "Tbl_Inspections", "[WO_ID]= " & Me.[Combo7] & " AND [Insp_Cat]=1")

CurrentDb.Execute "Update Tbl_Inspections Set Insp_Type = 'C' WHERE WO_ID = " & Me.Combo7 & " AND Insp_Cat = 1 AND Limit = " & Rec_Per