设为首页收藏本站Access中国

Office中国论坛/Access中国论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

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

[ADO/DAO] [讨论]用ADO如何建立临时查询

[复制链接]
跳转到指定楼层
1#
发表于 2004-11-9 02:58:00 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
我在DAO中可以用以下代码建立临时查询:

Dim qdf As DAO.QueryDef, strSQL As String

Set qdf = CurrentDb.CreateQueryDef("临时查询", strSQL)

DoCmd.OpenQuery qdf.name, , acReadOnly

请问用ADO如何实现?

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享分享 分享淘帖 订阅订阅
2#
发表于 2004-11-9 04:05:00 | 只看该作者
To 阿丰:'ADOX

Sub ADOCreateQuery()    Dim cat     As New ADOX.Catalog

    Dim cmd     As New ADODB.Command

   

    ' Open the catalog

    cat.ActiveConnection = "rovider=Microsoft.Jet.OLEDB.4.0;" & _

        "Data Source=C:\nwind.mdb;"    ' Create the query

    cmd.CommandText = "Select * FROM Categories"

    cat.Views.Append "AllCategories", cmd

   

    Set cat = Nothing

   

End Sub
3#
 楼主| 发表于 2004-11-9 22:17:00 | 只看该作者
问题已解决,谢谢楼上的兄弟。还有一个新问题,在DAO中可用下列代码删除临时查询文件:  CurrentDb.QueryDefs.Refresh

  For Each qdf In CurrentDb.QueryDefs

      If qdf.name = "临时查询" Then

         CurrentDb.QueryDefs.Delete qdf.name

         Exit For

      End If

  Next qdf在ADO中我用以下代码,老是报出错, Dim cat  As New ADOX.Catalog

Set cat.ActiveConnection = CurrentProject.Connection

cat.Views.Refresh

cat.Views.Delete "临时查询"

Set cat.ActiveConnection = Nothing

Set cat = Nothing

可有好办法?
4#
发表于 2004-11-10 01:10:00 | 只看该作者
Set qdf = CurrentDb.CreateQueryDef("", strSQL)还以为你是真正用的是临时查询呢,如果真正的临时查询是不用删除的了。看看下面的例子: Sub ClientServerX2() Dim dbsCurrent As Database

Dim qdfBestSellers As QueryDef

Dim qdfBonusEarners As QueryDef

Dim rstTopSeller As Recordset

Dim rstBonusRecipients As Recordset

Dim strAuthorList As String ' Open a database from which QueryDef objects can be

' created.

Set dbsCurrent = OpenDatabase("DB1.mdb") ' Create a temporary QueryDef object to retrieve

' data from a Microsoft SQL Server database.

Set qdfBestSellers = dbsCurrent.CreateQueryDef("")With qdfBestSellers

  .Connect = "ODBC;DATABASE=pubs;UID=saWD=;" & _

    "DSN=Publishers"

  .SQL = "SELECT title, title_id FROM titles " & _

   "ORDER BY ytd_sales DESC"

  Set rstTopSeller = .OpenRecordset()

  rstTopSeller.MoveFirst

End With ' Create a temporary QueryDef to retrieve data from

' a Microsoft SQL Server database based on the results from

' the first query.

Set qdfBonusEarners = dbsCurrent.CreateQueryDef("")With qdfBonusEarners

  .Connect = "ODBC;DATABASE=pubs;UID=saWD=;" & _

   "DSN=Publishers"

  .SQL = "SELECT * FROM titleauthor " & _

   "WHERE title_id = '" & _

   rstTopSeller!title_id & "'"

  Set rstBonusRecipients = .OpenRecordset()

End With ' Build the output string.

With rstBonusRecipients

  Do While Not .EOF

   strAuthorList = strAuthorList & "    " & _

    !au_id & ":  $" & (10 * !royaltyper) & vbCr

   .MoveNextLoop

End With ' Display results.

MsgBox "lease send a check to the following " & _

  "authors in the amounts shown:" & vbCr & _

  strAuthorList & "for outstanding sales of " & _

  rstTopSeller!Title & "." rstTopSeller.Close

dbsCurrent.CloseEnd Sub[em05]
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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

GMT+8, 2024-5-16 20:20 , Processed in 0.092374 second(s), 27 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

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