1

Trying to insert the current date into my Access database throws a syntax error for the INSERT INTO statement.

My database contains a field named Day which has the data type Date/Time.

My insert statement is:

Try
    dbConnection.Open()
    sqlString = "INSERT INTO table1 (Day) VALUES (@Day)"
    dbCommand.CommandText = sqlString
    dbCommand.Parameters.AddWithValue("@Day", FormatDateTime(Now, 2))
    dbCommand.ExecuteNonQuery()
Finally
    dbConnection.Close()
End Try

When I do Response.Write(FormatDateTime(Now, 2)) it displays 10/29/2013, which should be an acceptable record.

Doing these also throw a syntax error in the INSERT INTO statement:

Try
    dbConnection.Open()
    sqlString = "INSERT INTO table1 (Day) VALUES (Now())"
    dbCommand.CommandText = sqlString
    dbCommand.ExecuteNonQuery()
Finally
    dbConnection.Close()
End Try

Try
    dbConnection.Open()
    sqlString = "INSERT INTO table1 (Day) VALUES (#" & FormatDateTime(Now, 2) & "#)"
    dbCommand.CommandText = sqlString
    dbCommand.ExecuteNonQuery()
Finally
    dbConnection.Close()
End Try

Are the dates not in an acceptable format for the field?

1 Answer 1

1

Day is a reserved word. Enclose that name in square brackets to avoid confusing the db engine.

sqlString = "INSERT INTO table1 ([Day]) VALUES (Now())"
Sign up to request clarification or add additional context in comments.

3 Comments

I wasn't aware Day was a reserved keyword as well. Thank you Hans for providing the link and answering my question!
Both Date and Day are functions. I included a link to Allen Browne's page about reserved and other problem names. That page also include a link to a free utility you can download and use to examine your Access databases for problem names.
Hmmm. I was certain I was replying to a different comment. :-) Cheers.

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.