Input the date of DateTimePicker in ms access using a query VB.NET

耗尽温柔 提交于 2019-12-25 04:45:28

问题


An unhandled exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll

Additional information: Syntax error (missing operator) in query expression '7/27/2016 6:20:48 PM'.

I got this message every time I click the save button on my program.

here's the code:

Public Sub savetoDB()
    Dim mydate As DateTime
    mydate = Me.dtpDateDel.Value

    con.Open()
    Dim sqlQry As String = "INSERT INTO [tbl_Monitoring] ([Truck Plate No], [Driver], [Helper], [Date of Delivery], [Product], [Payment], [Customer]) VALUES (@p1, @p2, @p3, " & mydate & ", @p5, @p6, @p7)"
    Using cmd As New OleDbCommand(sqlQry, con)
        cmd.Parameters.AddWithValue("@p1", cbxTruck.Text)
        cmd.Parameters.AddWithValue("@p2", cbxDriver.Text)
        cmd.Parameters.AddWithValue("@p3", cbxHelper.Text)
        cmd.Parameters.AddWithValue("@p5", cbxProduct.Text)
        cmd.Parameters.AddWithValue("@p6", txtPayment.Text)
        cmd.Parameters.AddWithValue("@p7", txtCustomer.Text)
        cmd.ExecuteNonQuery()
        con.Close()
        MsgBox("Save Successfully!")
    End Using

End Sub

the error was thrown to cmd.ExecuteNonQuery()


回答1:


So what happens when you do this?

Public Sub savetoDB()
Dim mydate As DateTime
    mydate = Me.dtpDateDel.Value

    con.Open()
    Dim sqlQry As String = "INSERT INTO [tbl_Monitoring] ([Truck Plate No], [Driver], [Helper], [Date of Delivery], [Product], [Payment], [Customer]) VALUES (@p1, @p2, @p3, @p4, @p5, @p6, @p7)"
    Using cmd As New OleDbCommand(sqlQry, con)
        cmd.Parameters.AddWithValue("@p1", cbxTruck.Text)
        cmd.Parameters.AddWithValue("@p2", cbxDriver.Text)
        cmd.Parameters.AddWithValue("@p3", cbxHelper.Text)
        cmd.Parameters.AddWithValue("@p4", mydate)
        // Alternatively you need to use something like this to format it correctly.
        //cmd.Parameters.AddWithValue("@p4", mydate.ToString("dd/mm/yyyy hh:mm"))
        cmd.Parameters.AddWithValue("@p5", cbxProduct.Text)
        cmd.Parameters.AddWithValue("@p6", txtPayment.Text)
        cmd.Parameters.AddWithValue("@p7", txtCustomer.Text)
        cmd.ExecuteNonQuery()
        con.Close()
        MsgBox("Save Successfully!")
    End Using
End Sub



回答2:


You can try this code :

Public Sub savetoDB()
Dim mydate As DateTime
mydate = Me.dtpDateDel.Value

con.Open()
Dim sqlQry As String = "INSERT INTO [tbl_Monitoring] ([Truck Plate No], [Driver], [Helper], [Date of Delivery], [Product], [Payment], [Customer]) VALUES (@p1, @p2, @p3, @p4, @p5, @p6, @p7)"
Using cmd As New OleDbCommand(sqlQry, con)
    cmd.Parameters.AddWithValue("@p1", cbxTruck.Text)
    cmd.Parameters.AddWithValue("@p2", cbxDriver.Text)
    cmd.Parameters.AddWithValue("@p3", cbxHelper.Text)
    cmd.Parameters.Add("@p4", SqlDbType.Date ,mydate) 
    cmd.Parameters.AddWithValue("@p5", cbxProduct.Text)
    cmd.Parameters.AddWithValue("@p6", txtPayment.Text)
    cmd.Parameters.AddWithValue("@p7", txtCustomer.Text)
    cmd.ExecuteNonQuery()
    con.Close()
    MsgBox("Save Successfully!")
End Using
End Sub


来源:https://stackoverflow.com/questions/38610476/input-the-date-of-datetimepicker-in-ms-access-using-a-query-vb-net

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!