在连接上更新时出现MS Access语法错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了在连接上更新时出现MS Access语法错误相关的知识,希望对你有一定的参考价值。

我收到了

运行时错误'3075' - “查询表达式中的语法错误(缺少运算符)'ct.ContainerID FROM tblCrossarm AS mt INNER JOIN tblContainerTemp AS ct ON mt.SerialNumber = ct.SerialNumber

在失败时,strSQL变量看起来像:

UPDATE mt SET mt.ContainerID = ct.ContainerID FROM tblCrossarm AS mt INNER JOIN tblContainerTemp AS ct ON mt.SerialNumber = ct.SerialNumber

Private Sub btnSave_Click()
    Dim dbs As DAO.Database: Set dbs = CurrentDb
    Dim i As Integer
    Dim strSQL As String
    Dim arrMaterialType() As String
    Dim rstContainerTemp As DAO.Recordset
    Dim rstMaterialType As DAO.Recordset
    Dim strContainerID As Variant: strContainerID = Null

    If IsNull(cboContainerID.Column(0)) Or cboContainerID.Column(0) = "" Then
        If MsgBox("You have not selected a Container ID." & vbCr & vbLf & "Press OK to Delete the Container IDs of all materials in the Container Table or press cancel to abort.", vbOKCancel, "Delete Container IDs?") = vbCancel Then
            Exit Sub
        End If
    Else
        strContainerID = Chr$(39) & cboContainerID.Column(0) & Chr$(39)
    End If


    Set rstMaterialType = dbs.OpenRecordset("qryDistinctMaterialType")
    Set rstContainerTemp = dbs.OpenRecordset("tblContainerTemp")

    If Not (rstContainerTemp.EOF And rstContainerTemp.BOF) And Not (rstMaterialType.EOF And rstMaterialType.EOF) Then

        rstContainerTemp.MoveFirst
        rstMaterialType.MoveFirst

        Do Until rstMaterialType.EOF = True
            Do Until rstContainerTemp.EOF = True

                If rstMaterialType.Fields(0) = rstContainerTemp!MaterialType Then
'                    rstContainerTemp.Edit
'                    rstContainerTemp!ContainerID = strContainerID
'                    rstContainerTemp.Update
                    strSQL = "UPDATE tblContainerTemp AS ct SET ct.ContainerID = " & strContainerID & " WHERE ct.SerialNumber = " & Chr$(39) & rstContainerTemp!SerialNumber & Chr$(39) & ";"
                    dbs.Execute strSQL, dbFailOnError

                End If

                rstContainerTemp.MoveNext

            Loop

            rstMaterialType.MoveNext
        Loop

    End If


    strSQL = "UPDATE mt SET mt.ContainerID = ct.ContainerID FROM " & cboMaterialType.Column(0) & " AS mt INNER JOIN tblContainerTemp AS ct ON mt.SerialNumber = ct.SerialNumber;"
    dbs.Execute strSQL, dbFailOnError

    dbs.Close
    Set dbs = Nothing

End Sub
答案

在MS Access中,UPDATE语句不使用FROM子句。只需将查询调整为:

UPDATE tblCrossarm AS mt 
INNER JOIN tblContainerTemp AS ct ON mt.SerialNumber = ct.SerialNumber
SET mt.ContainerID = ct.ContainerID

以上是关于在连接上更新时出现MS Access语法错误的主要内容,如果未能解决你的问题,请参考以下文章

运行查询时出现运行时错误 3075 MS Access VBA

插入查询中发生语法错误(MS Access)

使用 oledbcommand.executeNonQuery() 更新 MS Access 记录时出现问题,结果未更新

在 MS Access 中导入电子表格数据时出现错误消息

连接到 ODBC 时出现 MS Access 错误

使用 Workbench 将 MS Access 迁移到 MySQL 时出现日期时间错误