spriore
spriore

Reputation: 613

Microsoft Access VBA Insert Into Error

The Problem: I keep getting a syntax error for the following statement:

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

Now I have a feeling it is the Me.Controls function that keeps throwing this error but I can't get it to go away.

Context: This is for an update button, where it checks if there is a record, if the two spaces in the form are empty and if neither of these conditions are filled it will create a new record.

Now when this happens the data from textbox in# and out# are used to create the new record.

The Code:

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

Upvotes: 0

Views: 781

Answers (1)

Gustav
Gustav

Reputation: 55831

Try this:

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

Upvotes: 1

Related Questions