设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

返回列表 发新帖
查看: 1839|回复: 4
打印 上一主题 下一主题

[ADO/DAO] 求助:DAO查询代码转ADO

[复制链接]
跳转到指定楼层
1#
发表于 2012-9-5 14:07:26 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
已有DAO写好交叉表查询代码,为日后升级至服务器,拟用ADO改写连接方式。请求提示recordset和connection的正确表达。。。。。代码如下:

Private Sub cmdStatics_Click()

     Dim strWhere As String
     Dim strSql As String
     Dim qdf As DAO.QueryDef    
   
     strWhere = ""
   
    If Not IsNull(Me.txtInsYear) Then
        strWhere = strWhere & "(year([SupervisalPaperList.InspectionDate]) = '" & Me.txtInsYear & "') and "
    End If
   
    If Not IsNull(Me.cmbMonth) Then
        strWhere = strWhere & "(month(InspectionDate) = '" & Me.cmbMonth & "') and "
    End If
   
    If Not IsNull(Me.cmbSupervisor) Then
        strWhere = strWhere & "([Supervisor] = '" & Me.cmbSupervisor & "') and "
    End If
   
    If Len(strWhere) > 0 Then
        strWhere = Left(strWhere, Len(strWhere) - 5)
    End If
   
    If Len(strWhere) > 0 Then
        strSql = "TRANSFORM Count(SupervisalPaperList.ProjectName) AS ProjectName之计算 "
        strSql = strSql & "SELECT SupervisalPaperList.Supervisor, Count(SupervisalPaperList.ProjectName) AS 发整改总数 "
        strSql = strSql & "FROM SupervisalPaperList WHERE ((SupervisalPaperList.Reversion) = '是 ') and "
        strSql = strSql & "(" & strWhere & ")"
        strSql = strSql & " GROUP BY SupervisalPaperList.Supervisor "
        strSql = strSql & "PIVOT Format([InspectionDate],'yyyy - mm');"
        
    Else
        strSql = "TRANSFORM Count(SupervisalPaperList.ProjectName) AS ProjectName之计算 "
        strSql = strSql & "SELECT SupervisalPaperList.Supervisor, Count(SupervisalPaperList.ProjectName) AS 发整改总数 "
        strSql = strSql & "FROM SupervisalPaperList WHERE ((SupervisalPaperList.Reversion) = '是 ')"
        strSql = strSql & " GROUP BY SupervisalPaperList.Supervisor "
        strSql = strSql & " PIVOT Format([InspectionDate],'yyyy - mm');"
    End If

    Set qdf = CurrentDb.QueryDefs("CrossSupervisalPaperList")
    qdf.SQL = strSql
    qdf.Close
   
    Set qdf = Nothing

   
    '显示交叉表的内容
    Me.fmSubSupervisalPaperList.SourceObject = ""
    Me.fmSubSupervisalPaperList.SourceObject = "query.CrossSupervisalPaperList"
   
    '  清除Me.txtCount的controlSource
    Me.txtCount.ControlSource = ""
    Me.txtCount = DCount("*", "CrossSupervisalPaperList")


End Sub

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅

点击这里给我发消息

2#
发表于 2012-9-5 15:47:52 | 只看该作者
恐怕你的想法是好的,就这个样用ADO,只怕你最终还是要失望,因为,SQL Server 不支持交叉查询的语法,只能通过存储过程来实现交叉查询的结果。

你的红色部分,要用 ADOX 来替代, 只是写法有很大的差异。
3#
发表于 2012-9-5 18:26:18 | 只看该作者
本帖最后由 roych 于 2012-9-5 18:29 编辑
每个人都会经过这个阶段,见到DAO,就想知道ADO是什么。我很想告诉他,可能学过ADO,你会发现没什么特别。回望之下,可能会觉得DAO更好用。

——《东邪西毒》Access版
4#
 楼主| 发表于 2012-9-5 19:06:26 | 只看该作者
roych 发表于 2012-9-5 18:26
——《东邪西毒》Access版

不是说DAO不好,将来我可能要面对的是ADO.NET,这个DAO可不能在它里面运行吧。。。。
5#
发表于 2012-9-6 09:32:01 | 只看该作者
远虑亦忧,无虑亦忧,未雨绸缪,冬天只有三个月,可勤劳的小松鼠却每年都要埋3年都吃不完的松果。
等到DAO淘汰的时候再升级吧。
把XP做得太完美了,微软后悔了。

点评

不做的完美些怎能打败其他操作系统  发表于 2012-9-6 13:48
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|站长邮箱|小黑屋|手机版|Office中国/Access中国 ( 粤ICP备10043721号-1 )  

GMT+8, 2024-5-13 06:16 , Processed in 0.103303 second(s), 32 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表