0

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
4
  • Do you actually have textboxs called: in1, in2, in3, in4... and out1, out2, out3, out4... on your form? Also do you get a specific Syntax error? Commented Jun 22, 2015 at 17:30
  • Run-Time Error '3134' Syntax Error in INSERT INTO statement. Commented Jun 22, 2015 at 17:38
  • 1
    @user2517352: Replace CurrentDb.Execute by Debug.Print. This will output all your actual SQL statements into the Immediate Window. Then, edit your question and paste one or two of the SQL statements so we can see the actual SQL that is executed. Commented Jun 22, 2015 at 20:52
  • When I replaced the code and clicked on my button it did nothing. Nothing was executed, nothing was updated or added, and there were no error messages or pop up windows. Commented Jun 23, 2015 at 11:31

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

6 Comments

Run Time Error 3075 Syntax Error (Missing operator) in query expression "100, [Out] = '50' WHERE [Part #] = '1' AND [Year] = 2015 AND [Week] = 1;'.
Well, with that little information you leave us guessing.
@Gustav: That error comes from the UPDATE query in the code from your answer, because you are missing a ' after SET [In] = '" & Me("in" & i).Value & "
Thanks Cristian. Edit made; it is present now.
I figured it out. I wasn't handling null values where I needed too.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.