Results 1 to 5 of 5

Thread: [RESOLVED] Data Type Mismatch

  1. #1

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Resolved [RESOLVED] Data Type Mismatch

    I am hacking through another tutorial this morning on how to use VB 2010 Express to add a new record to an existing MS Access database. The database has several fields in it including one which is a date field.

    I found the following code at some website and it looked simple enough for my needs, that is to just learn how to add a new record to Access.

    Code:
    Imports System.Data.OleDb
    
    Public Class Form1
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim myConnection As OleDbConnection
            Dim myCommand As OleDbCommand
    
            Dim mySQLString As String
            myConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyFiles\db1.mdb;")
            myConnection.Open()
    
            mySQLString = "INSERT INTO Table1 VALUES('','06/26/2012','','','FLG','33221','HATCH APD 38 x 42','Alum','Q1234','','Approved')"
            myCommand = New OleDbCommand(mySQLString, myConnection)
            myCommand.ExecuteNonQuery()
    
        End Sub
    End Class
    When I run this I get an error message which says "Data type mismatch in criteria expression." I assume it has something to do with the date field...but I'm still so new to this stuff.

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Data Type Mismatch

    MSAccess uses # around date values

    #06/26/2012# rather than '06/26/2012'

  3. #3

    Thread Starter
    Hyperactive Member Vladamir's Avatar
    Join Date
    Feb 2012
    Location
    Miami, FL
    Posts
    486

    Re: Data Type Mismatch

    Thanks...one of our DBA's came over and offered some assistance with this. We had to search google and even then it took us a couple of practice SQL commands inside of Access itself to figure out the syntax.

    Code:
    mySQLString = "INSERT INTO Table1 ( [Date] ) Values('06/21/2012');"
    This is kind of abbreviated version of the previous statement as at this point it's all testing anyway. But it worked. I will try the #'s next to insure we have this right.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,044

    Re: Data Type Mismatch

    You also have single quotes around 33221. If the field is a string, then that would be correct. If the field is numeric, then you should remove the single quotes.
    My usual boring signature: Nothing

  5. #5
    Fanatic Member
    Join Date
    Aug 2006
    Location
    In my head
    Posts
    913

    Re: Data Type Mismatch

    You should actually use parameters in all of your queries.

    Code:
    MyCommand.Parameters.Add("@ParameterName", OLEDBTYpe).Value = MyValue
    This stops you from having to remember all the syntax and will also allow you more flexibility in your application.

    HTH!!!

    D
    Platforms of choice: Visual Studio 2005/2008 Professional : Visual Studio 2010 Enterprise : PHP - Notepad++/WAMP

    Please Rate If I helped you.
    Please remember to mark threads as closed if your issue has been resolved.

    Reserved Words in Access | Connection Strings

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