3
votes

I am trying to use SQL in visual basic to insert Date and Time value into Access 2007 running in Access2002/3 mode. Using my code I have managed to insert text strings and numerical values into the table. However for the DateTime field get a syntax error. The field in Access is set as a Date/Time field type.

Below is my first function which generates the queries for the Database Accessing function:

Public Function NewUpload(ByVal UploadType As String) As Single

    Dim UploadNumber As Single
    Dim ColumnString As String
    Dim ValueString As String

    If DatabaseConnection("SELECT ID_UPL FROM tabUpload", "Read Recordset") = "Error" Then GoTo close
    Do Until rdrOLEDB.Read = False
        If Val(rdrOLEDB.Item(0).ToString()) > UploadNumber Then UploadNumber = Val(rdrOLEDB.Item(0).ToString())
    Loop
    rdrOLEDB.Close()
    cnnOLEDB.Close()

    UploadNumber = UploadNumber + 1

    'Update Uploads table:
    ColumnString = "ID_UPL,DateTime,IDUser,DataCalc"
    ValueString = Format(UploadNumber, "0000") & ",#" & Now.ToLongDateString & " " & Now.ToLongTimeString & "#,'" & My.User.Name & "','" & UploadType & "'"
    If DatabaseConnection("INSERT INTO tabUpload(" & ColumnString & ") VALUES(" & ValueString & ")", "Non-Query") = "Error" Then GoTo Close

    NewUpload = UploadNumber

Close: cnnOLEDB.Close()

End Function

Here is the second Function which connects to the Database

Public Function DatabaseConnection(ByVal Query As String, ByVal Task As String) As String
    'On Error GoTo Err

    cnnOLEDB.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & DataDirectoryName & DatabaseFileName
    cmdOLEDB.Connection = cnnOLEDB
    cmdOLEDB.CommandText = Query
    cnnOLEDB.Open()

    Select Case Task
        Case "Read Recordset"
            rdrOLEDB = cmdOLEDB.ExecuteReader()
            DatabaseConnection = "Read Recordset"
        Case "Read Scalar"
            DatabaseConnection = Str(cmdOLEDB.ExecuteScalar)
        Case "Non-Query"
            DatabaseConnection = Str(cmdOLEDB.ExecuteNonQuery())
            DatabaseConnection = "Non-Query"
    End Select

    Exit Function
Err:
    MsgBox("Database connection error.")
    DatabaseConnection = "Error"

End Function

When I run this code I get the Query: INSERT INTO tabUpload(IDUPL, DateTime, User, DataCalc) VALUES(0003, #17 August 2012 14:23:27#, 'UK\Pej', 'Calc')"

I have also tried several variations of DateTime using format and now(Year) giving yyyy-mm-dd etc.

Any help would be greatly appreciated.

3
Have you considered using parameters? - Fionnuala
Its worth repeating. Use parameters: see OLEDBCommand.Parameters - Sam Axe

3 Answers

5
votes

Both datetime and user are reserved words and must be enclosed in square brackets. Otherwise, it will run with that date.

 INSERT INTO tabUpload(IDUPL, [DateTime], [User], DataCalc) 
 VALUES(0003, #17 August 2012 14:23:27#, 'UK\Pej', 'Calc')"
2
votes

You're taking Vb.Net Now and making a literal value from it, then inserting that literal into your Date/Time field. However, the Access db engine provides its own Now() function, so you can use that function in your INSERT statement.

INSERT INTO tabUpload (IDUPL, [DateTime], [User], DataCalc)
VALUES(3, Now(), 'UK\Pej', 'Calc')
0
votes

Try to insert the date in the ODBC canonical format:

Format (Date, "yyyy-mm-ddThh:nn:ss")

Here's the documentation for the format function.