简体   繁体   中英

Issue printing the results of a SELECT Query in VBA | MS Access

Essentially I would like to run a simple select query that gets the sum of a column of values and prints the results in the immediate window (debugger).

The following is an example of my query:

SELECT Sum([Quantity]*[Total]) As Total FROM MyTable

The following is my code so far:

Dim strSQL As String
strSQL = "SELECT Sum([Quantity]*[Total]) As Total FROM MyTable"
DoCmd.RunSQL strSQL
Debug.Print strSQL

How can I execute that query and grab the results of the SUM to Print?

SELECT queries are not 'run', they are opened. Run is for action SQL - DELETE, INSERT, UPDATE.

Options:

  1. Open recordset object and read value from calculated field.
Dim rs as DAO.Recordset
Set rs = CurrentDb.OpenRecordset("SELECT Sum([Quantity]*[Total]) As Tot FROM MyTable"
Debug.Print rs!Tot
  1. Domain aggregate function
    Debug.Print DSum("Quantity*Total", "MyTable")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM