标题: 怎么把 SELECT count的值赋给一个变量? [打印本页] 作者: fatmingli 时间: 2016-8-3 09:55 标题: 怎么把 SELECT count的值赋给一个变量? 请问怎么把SQL查询的语句的值赋给变量AA
查询的语句是,也不知写得对不对,请问代码如何写下去
SELECT COUNT(DISTINCT 代码) as AA FROM 交易记录 WHERE (((交易记录.日期) >= [Forms]![交易记录]![Text4])) GROUP BY 交易记录.代码 HAVING (((交易记录.代码)<>'其他' And (交易记录.代码)<>'1810'));
作者: tmtony 时间: 2016-8-3 10:09
SQL语句的值 不能直接赋给变量的
要 用ADO 或DAO
如 dim rs as dao.recordset
set rs=currentdb.openrecordset("select count(*) as aa from 表名 where 条件")
dim aa as long
aa=rs("aa")作者: fatmingli 时间: 2016-8-3 10:15 本帖最后由 fatmingli 于 2016-8-3 16:12 编辑
谢谢,试一试试了,下面语句正常
Set rs = CurrentDb.OpenRecordset("SELECT count(*) as AA FROM 明威广州证券交易记录 where 证券代码<>'其他' and 证券代码<>'131810'")
但如果加上distinct就不正常了,
Set rs = CurrentDb.OpenRecordset("SELECT count(distinct 证券代码) as AA FROM 交易记录 where 证券代码<>'其他' and 证券代码<>'131810'")
我的目的是在计算表“交易记录”中的字段“证券代码”有多少条不同证券代码。请问代码怎么改,谢谢
作者: fatmingli 时间: 2016-8-4 11:45
找到了一个方法,好像这样可以
Set rs = CurrentDb.OpenRecordset("select count(*) as AA from (select distinct 证券代码 from 交易记录 where 交割日期>='" & Text4 & "' and 证券代码<>'其他' and 证券代码<>'131810')") 作者: tmtony 时间: 2016-8-14 09:08
然后再 读 rs("aa")的值 就可以了