我被告知(并同意),最好是在现有querydef中替换SQL,而不是每次需要更改查询时都删除并重新定义querydef。但是我的代码似乎只能以第二种方式工作。这是我有工作的代码:
Dim db As Database
Set db = CurrentDb
Dim QD As QueryDef
Dim mySql As String
mySql = ""
mySql = "TRANSFORM COUNT(tblDocs.Document) AS CountOfDocument " & _
"SELECT tblDocs.[Contractor Dept], " & _
"COUNT(tblDocs.Document) AS [Total Of Document] " & _
"FROM tblDocs " & _
"GROUP BY tblDocs.[Contractor Dept] " & _
"PIVOT tblDocs.[Engineering Status Code]"
On Error Resume Next
db.QueryDefs.Delete "qryX" 'Remove temporary query if exists
Set QD = db.CreateQueryDef("qryX", mySql) 'create temporary query
DoCmd.RunSQL "SELECT * INTO tblDocsCrossTabX FROM qryX;"
这是我无法使用的代码
Dim db As Database
Set db = CurrentDb
Dim QD As QueryDef
Dim mySql As String
mySql = " "
Set QD = db.CreateQueryDef(("qryX"), mySql)
mySql = "TRANSFORM COUNT(tblDocs.Document) AS CountOfDocument " & _
"SELECT tblDocs.[Contractor Dept], " & _
"COUNT(tblDocs.Document) AS [Total Of Document] " & _
"FROM tblDocs " & _
"GROUP BY tblDocs.[Contractor Dept] " & _
"PIVOT tblDocs.[Engineering Status Code]"
QD.SQL = mySQL 'overwrite query SQL
DoCmd.RunSQL "SELECT * INTO tblDocsCrossTabX FROM qryX;"
奇怪的是,第二个版本并没有给我带来任何错误,但是它根本没有使交叉表成为可能。
编辑:也许我还不够清楚。问题是第二组代码可以。不。执行。的。 SQL。如果它执行了代码,我很乐意重写并反复使用相同的临时查询,但是确实如此。不。执行。的。 SQL ..
请回答如何使第二个块中的代码实际执行所指示的SQL语句并构建所需的表。
我知道如果删除查询,我必须重新进行查询。咄。我知道如果可以获取Set语句用所需的sql正确覆盖以前的sql,则“应该”能够重新使用相同的查询。我知道你们都想提供答案,但请回答我要提出的问题。
如果不删除它,您可以回收该对象:
' On Error Resume Next
Set QD = db.QueryDefs("qryX")
QD.SQL = mySQL
DoCmd.RunSQL "SELECT * INTO tblDocsCrossTabX FROM qryX;"
已解决。
在第一段代码中,当我删除并重新创建查询时,字符串mySql包含有效的SQL语句,因此,当我使用SET完全重新创建SQL时,Access可以将其分配给querydef。具体来说,
mySql = "Transform..."
来了之前
Set QD = db.CreateQueryDef("qryX", mySql)
在第二组代码中,当使用Set命令时,字符串mySql仅包含一个空格,因为“”不是有效的SQL语句,Set命令甚至都不会执行,而QueryDef QD甚至都不会得到已创建。
具体来说,第二代码中的错误发生是因为
mySql = " " 'NOT a valid SQL Statement
在尝试创建queryDef之前出现
Set QD = db.CreateQueryDef(("qryX"), mySql) 'QD never gets created because mySql is not valid SQL
防止以后再分配有效的SQL语句,因此不会创建任何表。
[如果可以使用子查询,则可以使用临时QueryDef来代替检查QueryDef
是否已经存在,只需将SELECT和INSERT-Query结合使用临时QueryDef
很遗憾,您不能在子查询中使用Transform
,因此此代码会导致Runtime-Error 3129 Invalid SQL statement
。
Sql = "SELECT * INTO tblDocsCrossTabX FROM (TRANSFORM COUNT(tblDocs.Document) AS CountOfDocument " & _
"SELECT tblDocs.[Contractor Dept], " & _
"COUNT(tblDocs.Document) AS [Total Of Document] " & _
"FROM tblDocs " & _
"GROUP BY tblDocs.[Contractor Dept] " & _
"PIVOT tblDocs.[Engineering Status Code])"
With CurrentDb.CreateQueryDef(vbNullString) 'or db.CreateQueryDef("") creates a not named and therefore temporary QueryDef
.SQL = Sql
.Execute dbFailOnError
End With
成功的代码示例。
Dim db As Database
Set db = CurrentDb
Dim QueryString As String
Dim QDDocsCross As QueryDef
Set QDDocsCross = db.CreateQueryDef("DocsCross")
QueryString = "TRANSFORM COUNT(Docs.Document) AS CountOfDocument " & _
"SELECT Docs.[Contractor Dept], " & _
"COUNT(Docs.Document) AS [Total Of Document] " & _
"FROM Docs " & _
"GROUP BY Docs.[Contractor Dept] " & _
"PIVOT Docs.[Engineering Status Code]"
QDDocsCross.SQL = QueryString