我收到運行時錯誤3122:如何解決此查詢?
試圖執行一個查詢,不包括指定表達式count(*)*t2.Daily_Charge_HKD
作爲聚合函數的一部分
我要在查詢做什麼:
我想通過event_plan_code對Opt_In_Customer_Record中的所有記錄進行分組,併爲每個代碼計算總計數,然後通過t1.event_plan_code = t2.event_plan_code引用daily_charge表中的daily_charge,並將daily_charge與每個代碼的總數相乘的代碼
這裏是我的代碼:
Private Sub btnGenDaily_Click()
Dim filename As String
Dim prefix As String
Dim qryDef As DAO.QueryDef
Dim dbs As DAO.Database
Set dbs = OpenDatabase(CurrentDb.Name)
If IsNull(txtInputPath.value) Then
MsgBox "Please enter a valid input file location."
Else
If FileExists(txtInputPath.value) Then
If IsNull(txtOutputPath3.value) Then
MsgBox "Please enter a valid output file location."
Else
prefix = GetFileNamePrefix(txtInputPath.value)
sql = "select t1.event_plan_code, count(*), count(*)*t2.Daily_Charge_HKD " & _
"from Opt_In_Customer_Record t1 Inner Join Daily_Charge t2 " & _
"On (t1.event_plan_code=t2.event_plan_code and t2.Home_BMO='" & prefix & "') " & _
"group by t1.event_plan_code " & _
"order by t1.event_plan_code "
MsgBox sql
If ObjectExists("Query", "getDailyCharge") Then
DoCmd.DeleteObject acQuery, "getDailyCharge"
End If
With dbs
.QueryTimeout = 0
Set QueryDef = .CreateQueryDef("getDailyCharge", sql)
End With
strPathToSave = txtOutputPath3.value
DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, "getDailyCharge", strPathToSave, True
MsgBox "Daily charge report generated."
End If
Else
MsgBox "Input file does not exist. Please enter again."
End If
End If
End Sub
難道是因爲你不是由't2.Daily_Charge_HKD'分組嗎? – 2011-05-27 03:10:42
是的,但在這裏我不想由此分組。這使得一個不同的組。 – lamwaiman1988 2011-05-27 03:14:48
如果它不是分組字段,則不能直接在select中使用該字段;該字段只能用於sum,avg,min,max等彙總函數。 – 2011-05-27 03:17:47