Results 1 to 4 of 4
  1. #1
    dada is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    40

    error on inserting value


    anyone can tell me what's wrong with this code? it keeps on giving me error "Number of query values and destination fields are not the same".. please help..

    btw, cTime and ccDate are Textbox

    Dim sql, in_sql, out_sql As String
    sql = "INSERT INTO Transaction(TimeIn , EntryDate) VALUES ('"
    sql = sql & cTime & "," & ccDate & "')"
    DoCmd.RunSQL (sql)

  2. #2
    pbaldy's Avatar
    pbaldy is offline Who is John Galt?
    Windows XP Access 2007
    Join Date
    Feb 2010
    Location
    Nevada, USA
    Posts
    22,521
    As answered elsewhere:

    Rather than surrounding each value with the single quote delimiter, you've got one before the first value and one after the last. Also the date/time delimiter is # rather than '.

    cross posters
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    maximus's Avatar
    maximus is offline Expert
    Windows 7 Access 2010 (version 14.0)
    Join Date
    Aug 2009
    Location
    India
    Posts
    931
    I have a table Table2 with a time field tm and a date Field dt. On my form there are two text boxes Text0 and Text2 from which I will populate tm and dt.

    Dim strSQL As String
    strSQL = "Insert into Table2(tm,dt)Values(#" & Me.Text0 & "#,#" & Me.Text2 & "#);"
    DoCmd.RunSQL strSQL


    this is how my strSQL will look like. Dates and Time needs to be enclosed with #

    Instead of using Docmd.RunSQL you can also use CurrentDb.Execute this is a better option.

    CurrentDb.Execute strSQL, dbFailOnError

    You can also use a RecordSet to insert data into the table like I have done here.

    Set rs = CurrentDb.OpenRecordset("table2")
    rs.AddNew
    rs!tm = Me.Text0
    rs!dt = Me.Text2
    rs.Update
    rs.Close
    Set rs = Nothing

  4. #4
    dada is offline Advanced Beginner
    Windows XP Access 2003
    Join Date
    Aug 2010
    Posts
    40
    awesomeness! thanks to the both of you.. works like charm!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Inserting into a database
    By Cined in forum Programming
    Replies: 1
    Last Post: 04-14-2010, 12:33 PM
  2. Inserting/Updating
    By detlion1643 in forum Access
    Replies: 1
    Last Post: 02-26-2010, 07:25 PM
  3. Inserting Comment on a cell??
    By yotapower10 in forum Access
    Replies: 6
    Last Post: 06-29-2009, 08:20 AM
  4. Replies: 0
    Last Post: 05-14-2009, 12:34 AM
  5. Inserting a picture
    By Carenas in forum Forms
    Replies: 0
    Last Post: 01-03-2009, 05:09 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums