Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Problem to insert data from DateTimePicker to Access database

Problem to insert data from DateTimePicker to Access database

Scheduled Pinned Locked Moved Visual Basic
helpdatabase
11 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    ivo75
    wrote on last edited by
    #1

    I want to insert data from DateTimePicker to Access database but have error "Data type mismatch in criteria expression" this is the code Dim aCon As OleDbConnection Dim aCmd As OleDbCommand Dim strCon As String strAcessFilePath = System.Windows.Forms.Application.StartupPath &; "\rabotnici.mdb" 'create connection with mdb file strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strAcessFilePath & ";User ID=Admin;Password=" aCon = New OleDbConnection(strCon) aCon.Open() strQuery = "insert into data_pr values(" + " '" + DateTimePicker1.Value + "'" + ")" aCmd = New OleDbCommand(strQuery, aCon) Dim da As New OleDbDataAdapter da.SelectCommand = aCmd aCmd.ExecuteNonQuery() aCon.Close() access database field format is Date/time Can You help me thanks

    W M I 3 Replies Last reply
    0
    • I ivo75

      I want to insert data from DateTimePicker to Access database but have error "Data type mismatch in criteria expression" this is the code Dim aCon As OleDbConnection Dim aCmd As OleDbCommand Dim strCon As String strAcessFilePath = System.Windows.Forms.Application.StartupPath &; "\rabotnici.mdb" 'create connection with mdb file strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strAcessFilePath & ";User ID=Admin;Password=" aCon = New OleDbConnection(strCon) aCon.Open() strQuery = "insert into data_pr values(" + " '" + DateTimePicker1.Value + "'" + ")" aCmd = New OleDbCommand(strQuery, aCon) Dim da As New OleDbDataAdapter da.SelectCommand = aCmd aCmd.ExecuteNonQuery() aCon.Close() access database field format is Date/time Can You help me thanks

      W Offline
      W Offline
      Wendelius
      wrote on last edited by
      #2

      Instead of concatenating values to sql strings, use parameters. That way you will avoid conversion problems (among many other things such as sql injections etc). Have a look at: OleDbParameter Class[^]

      The need to optimize rises from a bad design.My articles[^]

      I 1 Reply Last reply
      0
      • W Wendelius

        Instead of concatenating values to sql strings, use parameters. That way you will avoid conversion problems (among many other things such as sql injections etc). Have a look at: OleDbParameter Class[^]

        The need to optimize rises from a bad design.My articles[^]

        I Offline
        I Offline
        ivo75
        wrote on last edited by
        #3

        but this work when access field format is text

        W 1 Reply Last reply
        0
        • I ivo75

          but this work when access field format is text

          W Offline
          W Offline
          Wendelius
          wrote on last edited by
          #4

          Yes it does and also it can be done for dates and numbers. Access accepts dates in format #mm/dd/yyyy#. So for example today would be #01/25/2009# (without quotes). However, I hope that you don't take this shortcut since it's not the proper way to go. I already mentioned sql injections which is prevented by using parameters. To give a few more examples that would be problematic:

          • numbers, what if decimal separator is comma (as it is for example in my case). When updating a column you would have for example UPDATE ... SET ColumnName = 49,6 ... This would lead to an error
          • text, what if your text contains a quote, for example: UPDATE ... SET ColumnName = 'Guns 'n' Roses'. Error again
          • dates, input date is in different format. For example I use 25.1.2009 (Finnish format) etc.

          So what I'm trying to say is that it's few more lines of code, but the quality and robustness of your application will greatly improve when you use parameters.

          The need to optimize rises from a bad design.My articles[^]

          I 1 Reply Last reply
          0
          • W Wendelius

            Yes it does and also it can be done for dates and numbers. Access accepts dates in format #mm/dd/yyyy#. So for example today would be #01/25/2009# (without quotes). However, I hope that you don't take this shortcut since it's not the proper way to go. I already mentioned sql injections which is prevented by using parameters. To give a few more examples that would be problematic:

            • numbers, what if decimal separator is comma (as it is for example in my case). When updating a column you would have for example UPDATE ... SET ColumnName = 49,6 ... This would lead to an error
            • text, what if your text contains a quote, for example: UPDATE ... SET ColumnName = 'Guns 'n' Roses'. Error again
            • dates, input date is in different format. For example I use 25.1.2009 (Finnish format) etc.

            So what I'm trying to say is that it's few more lines of code, but the quality and robustness of your application will greatly improve when you use parameters.

            The need to optimize rises from a bad design.My articles[^]

            I Offline
            I Offline
            ivo75
            wrote on last edited by
            #5

            OK I try to use parameters thanks very much, but are you sure that work

            W 1 Reply Last reply
            0
            • I ivo75

              OK I try to use parameters thanks very much, but are you sure that work

              W Offline
              W Offline
              Wendelius
              wrote on last edited by
              #6

              ivo75 wrote:

              are you sure that work

              See for yourself, that's what they are for :) You could try something like this:

              ...
              strQuery = "insert into data_pr values(?)"
              aCmd = New OleDbCommand(strQuery, aCon)
              aCmd.Parameters.AddWithValue("ColumName", DateTimePicker1.Value)
              aCmd.ExecuteNonQuery()
              ...

              The need to optimize rises from a bad design.My articles[^]

              I 1 Reply Last reply
              0
              • W Wendelius

                ivo75 wrote:

                are you sure that work

                See for yourself, that's what they are for :) You could try something like this:

                ...
                strQuery = "insert into data_pr values(?)"
                aCmd = New OleDbCommand(strQuery, aCon)
                aCmd.Parameters.AddWithValue("ColumName", DateTimePicker1.Value)
                aCmd.ExecuteNonQuery()
                ...

                The need to optimize rises from a bad design.My articles[^]

                I Offline
                I Offline
                ivo75
                wrote on last edited by
                #7

                not work give me this error again "Data type mismatch in criteria expression"

                C W 2 Replies Last reply
                0
                • I ivo75

                  not work give me this error again "Data type mismatch in criteria expression"

                  C Offline
                  C Offline
                  ChandraRam
                  wrote on last edited by
                  #8

                  ivo75 wrote:

                  "Data type mismatch in criteria expression"

                  This just means that the data type of the field in your database is not the same as the value that you are trying to write into it. Did you try parametric method as suggested by Mika?

                  1 Reply Last reply
                  0
                  • I ivo75

                    I want to insert data from DateTimePicker to Access database but have error "Data type mismatch in criteria expression" this is the code Dim aCon As OleDbConnection Dim aCmd As OleDbCommand Dim strCon As String strAcessFilePath = System.Windows.Forms.Application.StartupPath &; "\rabotnici.mdb" 'create connection with mdb file strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strAcessFilePath & ";User ID=Admin;Password=" aCon = New OleDbConnection(strCon) aCon.Open() strQuery = "insert into data_pr values(" + " '" + DateTimePicker1.Value + "'" + ")" aCmd = New OleDbCommand(strQuery, aCon) Dim da As New OleDbDataAdapter da.SelectCommand = aCmd aCmd.ExecuteNonQuery() aCon.Close() access database field format is Date/time Can You help me thanks

                    M Offline
                    M Offline
                    Mr Oizo
                    wrote on last edited by
                    #9

                    Try this in your vb code:

                    CONVERT(DATETIME, '" & Format(datetimepicker1.value, "MM/dd/yyyy") & " 00:00:00', 102)

                    If this doesn't work change the code 102 to 101 or 103. It might be one of those codes that you need to use. should do the trick

                    1 Reply Last reply
                    0
                    • I ivo75

                      not work give me this error again "Data type mismatch in criteria expression"

                      W Offline
                      W Offline
                      Wendelius
                      wrote on last edited by
                      #10

                      Could you post the code you're currently having.

                      The need to optimize rises from a bad design.My articles[^]

                      1 Reply Last reply
                      0
                      • I ivo75

                        I want to insert data from DateTimePicker to Access database but have error "Data type mismatch in criteria expression" this is the code Dim aCon As OleDbConnection Dim aCmd As OleDbCommand Dim strCon As String strAcessFilePath = System.Windows.Forms.Application.StartupPath &; "\rabotnici.mdb" 'create connection with mdb file strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strAcessFilePath & ";User ID=Admin;Password=" aCon = New OleDbConnection(strCon) aCon.Open() strQuery = "insert into data_pr values(" + " '" + DateTimePicker1.Value + "'" + ")" aCmd = New OleDbCommand(strQuery, aCon) Dim da As New OleDbDataAdapter da.SelectCommand = aCmd aCmd.ExecuteNonQuery() aCon.Close() access database field format is Date/time Can You help me thanks

                        I Offline
                        I Offline
                        ivo75
                        wrote on last edited by
                        #11

                        Tahnk You very much everybody this is the code who work strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strAcessFilePath & ";User ID=Admin;Password=" aCon = New OleDbConnection(strCon) aCon.Open() strQuery = "insert into rabotnici values(?,?,?)" param1 = New OleDbParameter("@rabotnik_name", OleDbType.VarWChar, 100) param1.Value = ComboBox1.Text param2 = New OleDbParameter("@obekt_name", OleDbType.VarWChar, 100) param2.Value = ComboBox2.Text param3 = New OleDbParameter("@data_rabota", OleDbType.Date) param3.Value = DateTimePicker1.Value.ToShortDateString() aCmd = New OleDbCommand(strQuery, aCon) aCmd.Parameters.Add(param1) aCmd.Parameters.Add(param2) aCmd.Parameters.Add(param3) aCmd.ExecuteNonQuery() aCon.Close() this is work with parameters, again thanks

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups