I'm trying to insert some data into an Access database table that includes a date column. If I hardcode the date in a variable it works just fine. However if I use Date.Now it throws an error. See code below:
Code:Using con As OleDbConnection = New OleDbConnection(MDBConnString), cmd As New OleDbCommand("", con) con.Open() Try 'Dim myDate As Date = #11/10/2025 04:26:07 PM# 'this works Dim myDate As Date = Date.Now 'this fails 'insert the New number cmd.CommandText = "insert into myTable (Type,Num,PartNum,Who,EntryDate) values (?,?,?,?,?)" cmd.Parameters.Clear() cmd.Parameters.AddWithValue("Type", type) cmd.Parameters.AddWithValue("Num", newNum) cmd.Parameters.AddWithValue("PartNum", newPartNum) cmd.Parameters.AddWithValue("Who", Environment.UserName.ToLower) cmd.Parameters.AddWithValue("EntryDate", myDate) cmd.ExecuteNonQuery() Catch ex As Exception MessageBox.Show(ex.Message) End Try End Using
The error I'm getting is "Data type mismatch in criteria expression." My hard coded date is the same format as what Date.Now returns, so I can't understand why one works & the other doesn't. Can anyone see what I'm doing wrong?




Reply With Quote