从访问VBA中的查询创建表

时间:2014-11-12 16:54:23

标签: mysql sql vba ms-access access-vba

我有这个在VBA中创建的查询,我希望将其放入VBA中的表中。无论如何我能做到吗?我查看了一个make table查询,但我不确定如何在VBA中创建一个。任何帮助将不胜感激!

Sub RevH()
Dim dte As String, clientQry As String, db As Database, clientQry1 As Variant


Set db = CurrentDb


dte = InputBox("What date was the Data Dump run?", "Please Input a date")


clientQry = "SELECT DISTINCT t.[CLIENT ID], t.[CLIENT NAME] " & _
            "FROM FN_DataDump_ALL_" & dte & " as t WHERE " & _
            " (((t.[CLIENT NAME]) Not Like  ""*Test*"" ));"

If CheckQuery("NewIDs") = "Yes" Then
DoCmd.DeleteObject acQuery, "NewIDs"
End If


clientQry1 = db.CreateQueryDef("NewIDs", clientQry)




End Sub

1 个答案:

答案 0 :(得分:3)

为什么要为此创建新查询?只需通过调用CurrentDb.Execute方法执行SQL。

第二:创建新表的查询的正确语法是:

SELECT YourFields INTO YourNewTable FROM YourOldTable

所以简单地做出类似的事情:

Dim clientQry as String

clientQry = "SELECT DISTINCT t.[CLIENT ID], t.[CLIENT NAME] " & _
            "INTO NewTableName " & _
            "FROM FN_DataDump_ALL_" & dte & " as t WHERE " & _
            " (((t.[CLIENT NAME]) Not Like  ""*Test*"" ));"

CurrentDb.Execute clientQry, dbFailOnError
顺便说一句:大多数时候制作表格的物理副本是个坏主意。通常,您将设置一个带有附加日期列的历史记录表,并将所有副本转储到该表中。在运行时期间,永远不应该要求添加新的表/列。其他任何东西都表明数据模型不好!