加入时更新 MS Access 语法错误

Posted

技术标签:

【中文标题】加入时更新 MS Access 语法错误【英文标题】:MS Access syntax error when updating on join 【发布时间】:2018-02-08 20:25:57 【问题描述】:

我收到了

运行时错误“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

【问题讨论】:

【参考方案1】:

在 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 和 SQL Server 的目的是让它们协同工作,但它们的少数语句有两种完全不同的语法?它们非常相似,以至于在互联网上搜索时很难记住和找到这样的异常。 没问题。这两个数据库都是使用不同技术的两种不同产品,它们共享同一个供应商。 SQL 有许多略有不同的方言。其实Access的UPDATE更接近mysql的!

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

MS Access SQL 语法错误

MS Access 中的 INSERT INTO 语法错误

MS Access Query 中的语法错误 [重复]

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

在大型 VBA/MS Access 项目中查找语法错误

在 Access 数据库中更新密码时 UPDATE 语句中的语法错误