Insert date / time in Access database

I am using ASP.NET/VB and I am trying to insert the date and time into the Access Date / Time field, but I am getting an error (data type mismatch in the criteria expression). Here is a simplified version of my code:

Dim myDate As Date = Now() Dim myIns1 As New OleDbCommand("INSERT INTO tableCourse (date_submitted) VALUES (?)", myConn) myIns1.Parameters.AddWithValue("@myDate", myDate) myIns1.ExecuteNonQuery() 

Not sure why I get the error, and not quite sure if this is even the right way to get closer to pasting current date. From looking at other similar questions, there seem to be several different ways to do this, but my technical knowledge is limited, so it’s hard for me to understand this (in other words, decipher the answers that use technical terms that I know nothing about).

Thanks in advance!

+6
source share
2 answers

You can skip this parameter completely and simply write the NOW function directly to the request.

 Dim myIns1 As New OleDbCommand("INSERT INTO tableCourse (date_submitted) VALUES (NOW())", myConn) myIns1.ExecuteNonQuery() 

This answer (the one above) is probably the best solution, but the reason the source code did not work is because the parameter was not named. You can try the following:

 Dim myDate As Date = Date.Now Dim myIns1 As New OleDbCommand("INSERT INTO tableCourse (date_submitted) VALUES (@myDate)", myConn) myIns1.Parameters.AddWithValue("@myDate", myDate) myIns1.ExecuteNonQuery() 

Strike>

+2
source

Try to change

 Dim myDate As Date = Now() 

to

 Dim myDate As DateTime = Now() 

MS Access does not offer a DATE data type, only DATETIME.

-1
source

Source: https://habr.com/ru/post/913683/


All Articles