在连接

时间:2018-02-08 20:25:57

标签: sql ms-access access-vba

我正在接收

  

运行时错误'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 个答案:

答案 0 :(得分:1)

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

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