MS Access VBA:向 table 添加了列,但现在 SQL "INSERT INTO" 不再有效

MS Access VBA: Added columns to table but now SQL "INSERT INTO" won't work anymore

我在 table“MTOStudy”和“OtherDesc”中添加了两个新字段作为短文本数据类型。 table 中的其他数据类型是数字和 Yes/No 值。

但是,在 Access 中添加两列并更新 table 的相应表单后,我无法在单击按钮后更新 table。

我已经确认的事情:

  1. 按钮有效,使用消息框功能。
  2. 变量与 table 一致。
  3. 除了添加“MTOStudy”和“OtherDesc”变量外,代码与工作函数相同
  4. 我看到的错误代码是“错误编号:3078;MS Access 数据库引擎无法找到输入 table 或查询“128”。确保它存在并且其名称拼写正确正确。"

调试行:INSERT INTO tbl_MTO_vs_ETO([Order]、[Line]、[MTO]、[ETO]、[DUP]、[MTOStudy]、[OtherDesc])VALUES ( , , -1, 0, 0, "测试一个泵", "")

下面是函数:

Private Sub btn_save_Click()
'On Error GoTo Err_Execute

    If Me.Check_MTO = False And Me.Check_ETO = False And Me.Check_DUP = False Then
        MsgBox ("Please select one of the classification options."), vbCritical
    Else
    
    Dim Append_SQL As String, tbl_target As String, _
        target_fields As String, field_values As String, _
        errLoop As Error
    
    tbl_target = "tbl_MTO_vs_ETO"

    target_fields = "([Order]," _
    & " [Line]," _
    & " [MTO]," _
    & " [ETO]," _
    & " [DUP]," _
    & " [MTOStudy]," _
    & " [OtherDesc])"
            
    
    field_values = "(" _
    & " " & Me.order & "," _
    & " " & Me.line & "," _
    & " " & Me.Check_MTO.Value & "," _
    & " " & Me.Check_ETO.Value & "," _
    & " " & Me.Check_DUP.Value & "," _
    & " """ & Me.MTOStudy.Value & """," _
    & " """ & Me.OtherDesc.Value & """)"
    
    Call Check_MTO_Dropdown

    Append_SQL = "INSERT INTO " & tbl_target & " " & target_fields & " VALUES " & field_values
    
    CurrentDb.Execute Append_SQL
    
    Debug.Print Append_SQL

    On Error GoTo 0
    
Err_Execute:
    
    If DBEngine.Errors.count > 0 Then
    For Each errLoop In DBEngine.Errors
    MsgBox "Error number: " & errLoop.Number & vbCr & _
    errLoop.description
    Next errLoop
    End If

    'DoCmd.Close acForm, Me.name
    'MsgBox ("Test"), vbOKOnly
    
    End If
End Sub

如果 MTOStudy 或 OtherDesc 中有 quote/apostrophe 个字符(" 或 '),除非您从输入中过滤掉这些字符或编写更多代码来处理它们,否则这将崩溃。你好多了关闭使用记录集作为上面的 HackSlash 注释。

正如 HackSlash 提到的,我也更喜欢记录集。

如果你想走那条路,试试这个:

Private Sub btn_save_Click()
    
    'Create new record in tbl_MTO_vs_ETO
        Dim db As DAO.Database
        Dim rs As DAO.Recordset
    
        Set db = CurrentDb
        Set rs = db.OpenRecordset("tbl_MTO_vs_ETO")
        
            With rs
                .AddNew
                    ![Order] = Order
                    ![Line] = Line
                    ![MTO] = Check_MTO.Value
                    ![ETO] = Check_ETO.Value
                    ![DUP] = Check_DUP.Value
                    ![MTOStudy] = MTOStudy.Value
                    ![OtherDesc] = OtherDesc.Value
                .Update
            End With
    
        rs.Close
        db.Close
    
        Set rs = Nothing
        Set db = Nothing

End Sub

您的 INSERT 语句的 VALUES 子句将触发错误 3134:“INSERT INTO 语句中的语法错误” 因为每个语句之前没有包含任何值前两个逗号:

... VALUES ( , , -1, 0, 0, "TEST ONE PUMP", "")
            ^ ^

您需要为每个列出的字段提供东西。如果前两个字段允许 Null,您应该能够避免使用该错误:

... VALUES (Null, Null, -1, 0, 0, "TEST ONE PUMP", "")