title: How do I resolve this query?
body: <p>I receive runtime error 3122 :</p>
  
  <p>You tried to execute a query that does not include the specified expression <code>count(*)*t2.Daily_Charge_HKD</code> as part of the aggregate function</p>
  
  <p>What I want to do in the query:</p>
  
  <p>I want to group all the record in Opt_In_Customer_Record by event_plan_code, and have a total count for each of the code, then I reference the daily_charge from the daily_charge table by t1.event_plan_code = t2.event_plan_code, and multiply the daily_charge with the total count for each of the code</p>
  
  <p>Here is my code:</p>
  
  <pre><code>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 " &amp; _
                        "from Opt_In_Customer_Record t1 Inner Join Daily_Charge t2 " &amp; _
                        "On (t1.event_plan_code=t2.event_plan_code and t2.Home_BMO='" &amp; prefix &amp; "') " &amp; _
                        "group by t1.event_plan_code " &amp; _
                        "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
  </code></pre>