|
-
Nov 10th, 2025, 04:25 PM
#1
Thread Starter
Frenzied Member
[RESOLVED] Weird issue inserting date into Access table
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?
-
Nov 10th, 2025, 05:31 PM
#2
Re: Weird issue inserting date into Access table
AddWithValue implicitly sets the parameter type and it sounds like it is failing. Use the .Add("...", OleDbType.{type}).Value = "..." approach instead;
Code:
cmd.Parameters.Add("@EntryDate", OleDbType.Date).Value = myDate
-
Nov 11th, 2025, 08:02 AM
#3
Thread Starter
Frenzied Member
Re: Weird issue inserting date into Access table
dday ... thanks, I'll give that a try.
-
Nov 12th, 2025, 12:06 AM
#4
Re: Weird issue inserting date into Access table
That does seem very strange. You're providing a binary Date value either way, so it is bizarre that one would work while the other would not. What is the data type of the EntryDate column in the database?
-
Nov 12th, 2025, 01:37 AM
#5
Re: Weird issue inserting date into Access table
Irrespective of the weird issue: Why in blazes are you passing the Date as a Param in the first place?
Let Access do it.
Rule of thumb: If you want to insert an information, which the Database itself can create, then never ever pass it from outside
Aircode
Code:
cmd.CommandText = "insert into myTable (Type,Num,PartNum,Who,EntryDate) values (?,?,?,?,Date())"
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) 'Commented out
cmd.ExecuteNonQuery()
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
-
Nov 12th, 2025, 08:04 AM
#6
Thread Starter
Frenzied Member
Re: Weird issue inserting date into Access table
 Originally Posted by jmcilhinney
That does seem very strange. You're providing a binary Date value either way, so it is bizarre that one would work while the other would not. What is the data type of the EntryDate column in the database?
The column is a Date/Time data type. Ddays suggestion did work. I also found that converting the date to a string & back to a date works:
Code:
CDate(Date.Now.ToString)
-
Nov 12th, 2025, 08:06 AM
#7
Thread Starter
Frenzied Member
Re: Weird issue inserting date into Access table
 Originally Posted by Zvoni
Irrespective of the weird issue: Why in blazes are you passing the Date as a Param in the first place?
Let Access do it.
Rule of thumb: If you want to insert an information, which the Database itself can create, then never ever pass it from outside
Aircode
Code:
cmd.CommandText = "insert into myTable (Type,Num,PartNum,Who,EntryDate) values (?,?,?,?,Date())"
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) 'Commented out
cmd.ExecuteNonQuery()
Agreed, a better way to do it. I will change my code.
***Edit***
I ended up using Now() instead of Date() because I want the full timestamp:
Code:
insert into myTable (Type,Num,PartNum,Who,EntryDate) values (?,?,?,?,Now())
Last edited by nbrege; Nov 12th, 2025 at 08:40 AM.
-
Nov 12th, 2025, 11:25 AM
#8
Re: Weird issue inserting date into Access table
 Originally Posted by nbrege
Agreed, a better way to do it. I will change my code.
***Edit***
I ended up using Now() instead of Date() because I want the full timestamp:
Code:
insert into myTable (Type,Num,PartNum,Who,EntryDate) values (?,?,?,?,Now())
Well, there you go.
Glad i could help
Last edited by Zvoni; Tomorrow at 31:69 PM.
----------------------------------------------------------------------------------------
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------------------
People call me crazy because i'm jumping out of perfectly fine airplanes.
---------------------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|