MS Access VBA子例程

时间:2016-07-21 15:38:10

标签: sql vba access-vba syntax-error

我正在使用Microsoft Access VBA中的子例程。我的程序的目的是创建一个名为newTableName的新表,然后对两个表tableName1tableName2执行连接。此联接的结果应存储在newTableName中。从我的在线研究中,我被认为无法创建一个空表来存储已连接的数据。因此,我尝试复制tablenName2并将其数据存储在newTableName中,直到连接为止进行。

但是,运行程序时出现以下错误:

  

运行时错误' -2147217900(80040e14)':

     

CREATE TABLE语句中的语法错误。

从这一行:

CurrentProject.Connection.Execute strNewTableQuery

使用用户输入测试了导致此查询的内容:

CREATE TABLE [UNIX_lob] AS SELECT * FROM UNIX

这是我的代码:

Option Compare Database

Public Sub JoinWithIIMMModule()

    Dim sqlNewTableQuery    As String
    Dim sqlJoinQuery        As String
    Dim tableName1          As String
    Dim tableName2          As String
    Dim newTableName        As String
    Dim commonField         As String

    ' Set the common field used to join the two tables '
    commonField = "[code]"

    ' Set the tableName1 to be used in the join. '
    tableName1 = "IIMM"

    ' Acquire name of table to be joined with table1 from the user'
    tableName2 = InputBox("Enter name of the table which you would like to join with " + tableName1 + ":", _
             "Table to split", "UNIX")

    ' Set the name of the new table where the joins will be stored.'
    newTableName = "[" + tableName2 + "_lob]"

    ' Create newTableName, which is a duplicate of the tableName2 table.'
    ' This is where the joined data will reside.'
    sqlNewTableQuery = "CREATE TABLE " + newTableName + " AS SELECT * FROM " + tableName2
    Debug.Print sqlNewTableQuery
    CurrentProject.Connection.Execute sqlNewTableQuery

    ' Join tableName1 and tableName2 on commonField, and store the joined data in newTableName'
    sqlJoinQuery = "SELECT " + tableName1 + ".*, " + tableName2 + ".*" & _
                 "INTO " + newTableName & _
                 "FROM " + tableName1 & _
                 "INNER JOIN " + tableName2 & _
                 "ON " + tableName1 + "." + commonField + " = " + tableName2 + "." + commonField + ";"
    Debug.Print sqlJoinQuery
    CurrentDb.Execute sqlJoinQuery

End Sub

有人知道可能导致此错误的原因吗?

0 个答案:

没有答案