Database.CreateQueryDef 方法 (DAO)
适用于:Access 2013、Office 2013
创建一个新的 QueryDef 对象。
语法
表达式 。CreateQueryDef (Name、 SQLText)
表达式 一个表示 Database 对象的变量。
参数
名称 |
必需/可选 |
数据类型 |
说明 |
---|---|---|---|
Name |
可选 |
Variant |
一个变量(String 子类型),它唯一命名新的 QueryDef。 |
SQLText |
可选 |
Variant |
一个变量(String 子类型),它是定义 QueryDef 的 SQL 语句。 如果省略此参数,则可以通过在将 QueryDef 追加到集合之前或之后设置它的 SQL 属性对其进行定义。 |
返回值
QueryDef
说明
在 Microsoft Access 工作区中,如果在创建 QueryDef 时为名称提供了除零长度字符串之外的信息,产生的 QueryDef 对象将自动追加到 QueryDefs 集合。
如果由名称指定的对象已经是 QueryDefs 集合的成员,则会发生运行时错误。 可以创建临时 QueryDef,方法是在执行 CreateQueryDef 方法时,将零长度字符串用于名称参数。 还可以通过将新建的 QueryDef 的 Name 属性设置为零长度字符串 ("") 来完成此操作。
如果您希望反复使用动态 SQL 语句,且不必在 QueryDefs 集合中创建任何新的永久对象,则使用临时的 QueryDef 对象十分有帮助。 不能将临时 QueryDef 追加到任何集合,因为零长度字符串不是永久 QueryDef 对象的有效名称。 始终可以设置新建 QueryDef 的 Name 和 SQL 属性,然后将 QueryDef 追加到 QueryDefs 集合。
若要在 QueryDef 对象中运行 SQL 语句,请使用 Execute 或 OpenRecordset 方法。
使用 QueryDef 对象是对 ODBC 数据库执行 SQL 传递查询的首选方法。
若要从 Microsoft Access 数据库引擎数据库中的 QueryDefs 集合中删除一个 QueryDef 对象,请对该集合使用 Delete 方法。
示例
以下示例使用 CreateQueryDef 方法创建并执行临时和永久的 QueryDef。 若要使该过程运行,需要使用 GetrstTemp 函数。
Sub CreateQueryDefX()
Dim dbsNorthwind As Database
Dim qdfTemp As QueryDef
Dim qdfNew As QueryDef
Set dbsNorthwind = OpenDatabase("Northwind.mdb")
With dbsNorthwind
' Create temporary QueryDef.
Set qdfTemp = .CreateQueryDef("", _
"SELECT * FROM Employees")
' Open Recordset and print report.
GetrstTemp qdfTemp
' Create permanent QueryDef.
Set qdfNew = .CreateQueryDef("NewQueryDef", _
"SELECT * FROM Categories")
' Open Recordset and print report.
GetrstTemp qdfNew
' Delete new QueryDef because this is a demonstration.
.QueryDefs.Delete qdfNew.Name
.Close
End With
End Sub
Function GetrstTemp(qdfTemp As QueryDef)
Dim rstTemp As Recordset
With qdfTemp
Debug.Print .Name
Debug.Print " " & .SQL
' Open Recordset from QueryDef.
Set rstTemp = .OpenRecordset(dbOpenSnapshot)
With rstTemp
' Populate Recordset and print number of records.
.MoveLast
Debug.Print " Number of records = " & _
.RecordCount
Debug.Print
.Close
End With
End With
End Function
以下示例使用 CreateQueryDef 和 OpenRecordset 方法以及 SQL 属性,查询 Microsoft SQL Server 示例数据库 Pubs 中的书名表,并返回最畅销书籍的书名和书名标识符。 然后查询作者表,并指示用户根据每个作者的版税份额向其发送红利支票(总红利为 ¥1,000,每个作者应收到该金额的一定份额)。
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
' Note: The DSN referenced below must be configured to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the Microsoft SQL Server.
.Connect = "ODBC;DATABASE=pubs;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
' Note: The DSN referenced below must be configured to
' use Microsoft Windows NT Authentication Mode to
' authorize user access to the Microsoft SQL Server.
.Connect = "ODBC;DATABASE=pubs;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
.MoveNext
Loop
End With
' Display results.
MsgBox "Please send a check to the following " & _
"authors in the amounts shown:" & vbCr & _
strAuthorList & "for outstanding sales of " & _
rstTopSeller!Title & "."
rstTopSeller.Close
dbsCurrent.Close
End Sub
以下示例演示如何创建参数查询。 名为 myQuery 的查询通过两个参数(命名为 Param1 和 Param2)创建。 要执行此操作,查询的 SQL 属性设置为定义参数的结构化查询语言 (SQL) 语句。
示例代码提供方:Microsoft Access 2010 程序员参考。
Sub CreateQueryWithParameters()
Dim dbs As DAO.Database
Dim qdf As DAO.QueryDef
Dim strSQL As String
Set dbs = CurrentDb
Set qdf = dbs.CreateQueryDef("myQuery")
Application.RefreshDatabaseWindow
strSQL = "PARAMETERS Param1 TEXT, Param2 INT; "
strSQL = strSQL & "SELECT * FROM [Table1] "
strSQL = strSQL & "WHERE [Field1] = [Param1] AND [Field2] = [Param2];"
qdf.SQL = strSQL
qdf.Close
Set qdf = Nothing
Set dbs = Nothing
End Sub