Public DB As New ADODB.Connection
Public rs As New ADODB.Recordset
dim strsql as stringstrsql="declare @SQL varchar(8000) 
select @SQL='select 成本中心代码,成本中心名称' 
select @SQL=@sql+',sum(case 产品类别 when '''+[产品类别]+''' then 接收数量*单价 end) '''+[产品类别]+'''' 
from (select distinct 产品类别 from Porvsum) as a
select @SQL=@SQL+',sum(接收数量*单价) 合计,''2004-08-18'' 起始日期,''2004-08-27'' 结束日期 from Porvsum where  接收日期>=''2004-08-18'' and 接收日期<=''2004-08-27''
    group by 成本中心代码,成本中心名称
    order by  成本中心代码 '
exec (@sql)"rs.Open (strSql), DB, adOpenKeyset, adLockReadOnly
此种情况rs.recordcount的值为-1但是,如果用上述SQL语句动态生成的语句的话(就是下面这条):
strsql="select 成本中心代码,成本中心名称,sum(case 产品类别 when '弹簧、导柱、轴承类' then 接收数量*单价 end) '弹簧、导柱、轴承类',sum(case 产品类别 when '电器类' then 接收数量*单价 end) '电器类',sum(case 产品类别 when '钢材类' then 接收数量*单价 end) '钢材类',sum(case 产品类别 when '工具类' then 接收数量*单价 end) '工具类',sum(case 产品类别 when '管道类' then 接收数量*单价 end) '管道类',sum(case 产品类别 when '化工类' then 接收数量*单价 end) '化工类',sum(case 产品类别 when '劳保医药类' then 接收数量*单价 end) '劳保医药类',sum(case 产品类别 when '螺丝、冲针、发热圈类' then 接收数量*单价 end) '螺丝、冲针、发热圈类',sum(case 产品类别 when '皮带类:输送带等' then 接收数量*单价 end) '皮带类:输送带等',sum(case 产品类别 when '其它类' then 接收数量*单价 end) '其它类',sum(case 产品类别 when '切削、焊接类,铝棒、铜棒,铝线,焊丝' then 接收数量*单价 end) '切削、焊接类,铝棒、铜棒,铝线,焊丝',sum(case 产品类别 when '丝印器材类' then 接收数量*单价 end) '丝印器材类',sum(case 产品类别 when '文具类' then 接收数量*单价 end) '文具类',sum(接收数量*单价) 合计,'2004-08-18' 起始日期,'2004-08-27' 结束日期 from Porvsum where  接收日期>='2004-08-18' and 接收日期<='2004-08-27'
    group by 成本中心代码,成本中心名称
    order by  成本中心代码 
"
得出的 rs.RecordCount 的值就没问题,是不是用动态语句时
rs.Open (strSql), DB, adOpenKeyset, adLockReadOnly
这样写是不对的?应该怎么写呢? 

解决方案 »

  1.   

    大家可以参考我提这条语句的原因:
    http://community.csdn.net/Expert/topic/3312/3312248.xml?temp=.2461969
      

  2.   

    如果动态得不到,记录数。试一试,用SQL语句 
    myrst.open"SELECT mycount=count(field) from table"
    MyRecordCount=myrst("mycount")
      

  3.   

    我想表达的意思不是说我想得到记录数,
    而是:不同形式的SQL语句在SQL查询分析器得到的结果一样,但用VB中的Recordset得到的结果却不一样,如何让动态SQL语句的结果正确呢?
      

  4.   

    rs.Open (strSql), DB, adOpenDynamic, adLockReadOnly
      

  5.   

    To dingdong0080(小丁丁) :
    rs.Open (strSql), DB, adOpenDynamic, adLockReadOnly我是这样写的啊
      

  6.   

    rs.cursorlocation=aduseclient
    rs.open strsql,conn,1,1
      

  7.   

    To jone999(勋之梦) :
      
      好强啊!调试成功!
    请教这句话rs.cursorlocation=aduseclient是什么意思呢?