我已经编写了代码和Access 2007,我想在运行它时显示答案,但它没有显示。我认为有一个我遗漏的代码,请帮助显示。
代码是:
Private Sub cmdCalculate_Click()
On Error GoTo ErrorHandling
Dim rsAsset As Recordset
Dim SQL As String
SQL = "SELECT * FROM Asset WHERE Asset.Asset_Condition ='Repairs';"
Set rsAsset = CurrentDb.OpenRecordset(SQL)
Dim Total As Integer
Total = 0
rsAsset.MoveFirst
While (Not rsAsset.EOF)
Total = Total + rsAsset!Asset_Condition
rsAsset.MoveNext
Wend
Dim Ave As Double
Ave = Total / rsAsset.RecordCount
Debug.Print "Total Repairs= " & Total
Exit Sub
ErrorHandling:
MsgBox Err.Description
rsAsset.Close
End Sub
我已经用数据填充了表格,但不知道如何编写将显示答案的代码。
答案 0 :(得分:0)
有一次你做WHERE Asset.Asset_Condition ='Repairs'
,而另一点你威胁" Asset_Condition"好像它是一个数值(Total = Total + rsAsset!Asset_Condition
)。
现在。是" Asset_Condition"文本或数字?
为什么不使用SQL来进行数学运算而不是从VBA中逐个记录1?在这里尝试这个人,假设" Asset_RepairCost"是数据表中现有的数字字段," Asset_Condition"是文字:
SELECT SUM(Asset_RepairCost) AS 'TotalRepair', AVG(Asset_RepairCost) AS 'AverageOfTotalRepair' FROM Asset WHERE Asset.Asset_Condition ='Repairs'