Microsoft Access VBA插入错误

时间:2015-06-22 17:08:28

标签: vba ms-access access-vba ms-access-2010

问题: 我一直收到以下语句的语法错误:

CurrentDb.Execute "INSERT INTO Inventory ([Part #], [Year], [Week], [In], [Out]) " _ 
                & "VALUES ('" & Me.txtFindPart & "'," & Me.txtYear & "," & i & "," _ 
                & Me.Controls("in" & i) & "," & Me.Controls("out" & i) & ");"

现在我感觉这是Me.Controls函数不断抛出这个错误,但我无法让它消失。

上下文: 这是一个更新按钮,用于检查是否有记录,表单中的两个空格是否为空如果这些条件都没有填满,它将创建一个新记录。

现在发生这种情况时,#和out#中的文本框中的数据将用于创建新记录。

代码:

Dim i As Integer
i = 1

Do Until i = 53

    If DCount("[Part #]", "[Inventory]", "([Inventory].[Part #] = '" & txtFindPart & "' AND [Inventory].[Year] = " & txtYear & " AND [Inventory].[Week] = " & i & ")") > 0 Then
        CurrentDb.Execute "UPDATE Inventory SET [In] = '" & Me.Controls("in" & i) & "' WHERE [Part #] = '" & txtFindPart & "' AND [Year] = " & txtYear & " AND [Week] = " & i & ";"
        CurrentDb.Execute "UPDATE Inventory SET [Out] = '" & Me.Controls("out" & i) & "' WHERE [Part #] = '" & txtFindPart & "' AND [Year] = " & txtYear & " AND [Week] = " & i & ";"
        i = i + 1
    ElseIf Me.Controls("in" & i) = Null And Me.Controls("out" & i) = Null Then
        i = i + 1
    Else
        CurrentDb.Execute "INSERT INTO Inventory ([Part #], [Year], [Week], [In], [Out]) " _
                        & "VALUES ('" & Me.txtFindPart & "'," & Me.txtYear & "," & i & "," _
                        & Me.Controls("in" & i) & "," & Me.Controls("out" & i) & ");"
        i = i + 1
    End If
Loop
Me.Requery

1 个答案:

答案 0 :(得分:1)

试试这个:

Dim i As Integer
i = 1

Do Until i = 53
    If DCount("[Part #]", "[Inventory]", "[Inventory].[Part #] = '" & txtFindPart & "' AND [Inventory].[Year] = " & txtYear & " AND [Inventory].[Week] = " & i & "") > 0 Then
        CurrentDb.Execute "UPDATE Inventory SET [In] = '" & Me("in" & i).Value & "', [Out] = '" & Me("out" & i).Value & "' WHERE [Part #] = '" & txtFindPart & "' AND [Year] = " & txtYear & " AND [Week] = " & i & ";"
    ElseIf Not IsNull(Me("in" & i).Value) And IsNull(Me("out" & i).Value) Then
        CurrentDb.Execute "INSERT INTO Inventory ([Part #], [Year], [Week], [In], [Out]) " _
                        & "VALUES ('" & Me.txtFindPart & "'," & Me.txtYear & "," & i & ",'" _
                        & Me("in" & i).Value & "','" & Me("out" & i).Value & "');"
    End If
    i = i + 1   
Loop
Me.Requery