select语句包括misspeleld或missing的保留字或参数名

时间:2013-07-12 17:57:01

标签: sql vba ms-access

我道歉,如果这样的事情已经发布或只是一个简单的修复,但我一直在四处寻找,似乎无法找到解决这个问题的方法。我正在使用vba:ms访问,我正在尝试通过运行查询来在不同的数据库中同步两个相同的表。我一直在尝试制作脚本,以便它可以在只需要用户输入的不同数据库上运行。但是,通过这个过程,我似乎不断收到一个错误:

  

select语句包含保留字或参数名   错误拼写或遗失。

LinkUserTable函数基本上根据用户输入从另一个DB创建链接表。以下是子程序:

Sub LinkTables()
        Dim sTable1 As String
        Dim sTable2 As String
    sTable1 = LinkUserTable
    sTable2 = LinkUserTable

    vars_sql = "SELECT " & sTable1 & ".[f_ptid]," & sTable1 & ".[f_flag]," & sTable1 & ".[f_user]," & sTable1 & ".[f_brief]," & sTable1 & ".[f_gid]," & sTable1 & ".[f_vco]," & sTable1 & ".[f_dtype]," & sTable1 & ".[f_precs]," & sTable1 & ".[f_addr]," & sTable1 & ".[f_ndim]," & sTable1 & ".[f_sys]," & sTable1 & ".[f_unit]," & sTable1 & ".[f_value]," & sTable1 & ".[f_pred]," & sTable1 & ".[f_bflag]," & sTable1 & ".[f_dim1]," & sTable1 & ".[f_dim2]," & sTable1 & ".[f_dim3]," & sTable1 & ".[f_bound]," & sTable1 & ".[f_size]," & sTable1 & ".[f_updat]," & sTable1 & ".[f_time]," & sTable1 & ".[f_ldes]," & sTable1 & ".[f_sflag]," & sTable1 & ".[f_tflag]," & sTable1 & ".[rid]" + _
                    "FROM" + sTable1 + " AS LEFT JOIN " + sTable2 + " AS ON sTable1.[f_ptid] = sTable2.[f_ptid]" + _
                    "WHERE (((" & sTable2 & ".[f_ptid]) Is Null))" 


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

Set qdf = CurrentDb.CreateQueryDef("new_old", vars_sql)

End Sub

我只是想知道可能出现这种错误的问题是什么?所有字段都在两个数据库中,都具有相同的属性(只是不同的数据)。如果缺少我可以提供的更多信息,我会道歉。

2 个答案:

答案 0 :(得分:0)

正确的语法是LEFT JOIN,而不是AS LEFT JOIN

例外情况是,如果您想为表格提供别名,在这种情况下,您需要AS Alias LEFT JOIN ...但是,您想要在这种情况下执行此操作,因为您使用[table].[columnname]语法)。

答案 1 :(得分:0)

vars_sql赋值的第二行在字符串中有一些表变量:

" AS ON sTable1.[f_ptid] = sTable2.[f_ptid]"

上面应该是这样的:

" AS ON " + sTable1 + ".[f_ptid] = " + sTable2 + ".[f_ptid]"