2013-04-26 89 views
0

我已經編寫了代碼並在Access 2007上運行它,但我不希望顯示答案但它不顯示。我認爲有一個我遺漏的代碼,請幫助展示。在VBA中顯示結果的代碼

代碼是:

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

在某一時刻,您做了WHERE Asset.Asset_Condition ='Repairs',並且在另一點您將「Asset_Condition」威脅爲數字值(Total = Total + rsAsset!Asset_Condition)。
現在。是「Asset_Condition」文本還是數字?

而且,爲什麼不使用SQL來進行數學計算,而不是通過VBA中的一對一記錄?假設「Asset_RepairCost」是數據表中現有的數字字段,並且「Asset_Condition」是文本,請嘗試此人:

SELECT SUM(Asset_RepairCost) AS 'TotalRepair', AVG(Asset_RepairCost) AS 'AverageOfTotalRepair' FROM Asset WHERE Asset.Asset_Condition ='Repairs'