Results 1 to 8 of 8

Thread: [RESOLVED] Weird issue inserting date into Access table

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Resolved [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?

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Posts
    12,398

    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
    "Code is like humor. When you have to explain it, it is bad." - Cory House
    VbLessons | HtmlLessons | CssLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Weird issue inserting date into Access table

    dday ... thanks, I'll give that a try.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,269

    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

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Weird issue inserting date into Access table

    Quote Originally Posted by jmcilhinney View Post
    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)

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jul 2006
    Location
    MI
    Posts
    2,012

    Re: Weird issue inserting date into Access table

    Quote Originally Posted by Zvoni View Post
    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.

  8. #8
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,269

    Re: Weird issue inserting date into Access table

    Quote Originally Posted by nbrege View Post
    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
  •  



Click Here to Expand Forum to Full Width