Results 1 to 10 of 10
  1. #1
    gint32 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jan 2015
    Location
    Australia
    Posts
    116

    Angry SQL Insert error msg:" number of query values and destination fields not the same?"

    Can anyone tell me why the example1 on my forms after update event gives me the following error message "number of query values and destination fields not the same?"
    Yet the 2nd example works and injects without a hitch into the table "attend".


    All I have changed is to added another field to the list so as I can "catch/pickup/record" the logged on user that has updated/amended or appended a record via a form, does seem to be to difficult I would have thought!, but I am pulling my hair out on this one

    Example1...

    Code:
    RecDetect = DLookup("[scrStudent]", "attend", "[attstudent] = " & Me![scrStudent] & " AND [Attdate] = #" & Format(TDate, "mm/dd/yy") & "#")
    If IsNull(RecDetect) Then
        strSql = "INSERT INTO Attend ( AttStudent, AttDate, LogOnUser ) " _
        & "SELECT " & Me![scrStudent] & " AS F1, #" & Format(TDate, "mm/dd/yy") & "# AS F2, " & TypeAttend & " AS F3, #" & Me![txtUserId] & " AS F4;"
        DoCmd.RunSQL strSql
    else...do something else

    Example2...

    Code:
    RecDetect = DLookup("[scrStudent]", "attend", "[attstudent] = " & Me![scrStudent] & " AND [Attdate] = #" & Format(TDate, "mm/dd/yy") & "#")
    
    If IsNull(RecDetect) Then
        strSql = "INSERT INTO Attend ( AttStudent, AttDate, AttType ) " _
        & "SELECT " & Me![scrStudent] & " AS F1, #" & Format(TDate, "mm/dd/yy") & "# AS F2, " & TypeAttend & " AS F3;"
        DoCmd.RunSQL strSql
    else...do something else

    What I am I doing wrong, as I have no clue how remedy this one!
    and have been staring at this for hours now(I have attached the example)
    Many thanks in advance for any pointers towards a fix...
    Attached Files Attached Files

  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
    The message tells you the problem. You have 3 fields in the INSERT clause and 4 in the SELECT.
    Paul (wino moderator)
    MS Access MVP 2007-2019
    www.BaldyWeb.com

  3. #3
    gint32 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jan 2015
    Location
    Australia
    Posts
    116
    Thanks for the reply, Ok, I have now amended to have four of each and still gives the same message:
    I pasted the wrong code so here it is again - corrected: see attachment sample1
    So, if I use the code from Example1 is doesn't work, whereas Example 2 does, But of course I wish for Example1 to work so as I get that 4th field populated


    I am attempting to add an additional field to the sql statement, which would be the logged on user.


    'notworking below
    example 1
    Code:
    'If IsNull(RecDetect) Then
       ' strSql = "INSERT INTO Attend ( AttStudent, AttDate, AttType, LogOnUser ) " _
        '& "SELECT " & Me![scrStudent] & " AS F1, #" & Format(TDate, "mm/dd/yy") & "# AS F2, " & TypeAttend & " AS F3, #" & Me![txtUserId] & " AS F4;"
       ' DoCmd.RunSQL strSql

    'working
    example2
    Code:
      
        If IsNull(RecDetect) Then
        strSql = "INSERT INTO Attend ( AttStudent, AttDate, AttType ) " _
        & "SELECT " & Me![scrStudent] & " AS F1, #" & Format(TDate, "mm/dd/yy") & "# AS F2, " & TypeAttend & " AS F3;"
        DoCmd.RunSQL strSql
    Attached Files Attached Files
    Last edited by gint32; 02-01-2016 at 04:12 AM. Reason: file upload

  4. #4
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,420
    you seem to have a surplus # the first query- should be

    ....& "# AS F2, " & TypeAttend & " AS F3, " & Me![txtUserId] & " AS F4;"

  5. #5
    gint32 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jan 2015
    Location
    Australia
    Posts
    116
    thanks, but changed to suit as below, unfortunately I still get the same message
    Code:
    If IsNull(RecDetect) Then
       strSql = "INSERT INTO Attend ( AttStudent, AttDate, LogOnUser ) " _
        & "SELECT " & Me![scrStudent] & " AS F1, #" & Format(TDate, "mm/dd/yy") & "# AS F2, " & TypeAttend & " AS F3, " & Me![txtUserId] & " AS F4;"
        DoCmd.RunSQL strSql

  6. #6
    gint32 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jan 2015
    Location
    Australia
    Posts
    116
    Code:
       
      strSql = "INSERT INTO Attend ( AttStudent, AttDate, AttType, LogOnUser ) " & _
               "SELECT " & Me![scrStudent] & " AS F1, #" & Format(TDate, "mm/dd/yy") & "# AS F2, " & TypeAttend & " AS F3, " & Me![txtUserId] & " AS F4;"
      Debug.Print strSql
      DoCmd.RunSQL strSql

  7. #7
    gint32 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jan 2015
    Location
    Australia
    Posts
    116
    best to have a look at the attachment

  8. #8
    gint32 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jan 2015
    Location
    Australia
    Posts
    116
    Quote Originally Posted by Ajax View Post
    you seem to have a surplus # the first query- should be

    ....& "# AS F2, " & TypeAttend & " AS F3, " & Me![txtUserId] & " AS F4;"
    output of debug...INSERT INTO Attend ( AttStudent, AttDate, AttType, LogOnUser ) SELECT 219477 AS F1, #03/07/16# AS F2, 1 AS F3, gerrynew1 AS F4;

  9. #9
    CJ_London is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,420
    usually fields with an ID as part of the name are numeric, but I see from your debug output it is text so you need to use single quotes

    ...." AS F3, '" & Me![txtUserId] & "' AS F4;"

    what happens if you copy and paste the (revised) sql generated by the debug? Does it run?

    The reason I ask is normally you need a FROM and a table if using a SELECT, but you say your second query in your initial post works (which also doesn't have a FROM) so it can't be that.

    When updating tables with data not from another table I use the construct

    INSERT INTO Attend ( AttStudent, AttDate, AttType, LogOnUser ) VALUES(219477, #03/07/16# , 1, 'gerrynew1')

  10. #10
    gint32 is offline Competent Performer
    Windows 7 64bit Access 2013
    Join Date
    Jan 2015
    Location
    Australia
    Posts
    116

    Thumbs up

    many thanks for your efforts, yeh. ID doesn't in this instance refer to a numeric value...But good news is, I finally got it to inject using the following code
    ...I didn't know that datatypes were defined by using different quotes like ' or " or # I just assumed if it wouldn't go into the table it would throw a make table for entries that wouldn't insert...

    Code:
    ' ...
      strSql = "INSERT INTO Attend ( AttStudent, AttDate, AttType, LogOnUser ) VALUES (" & _
               Me![scrStudent] & ", #" & Format(TDate, "mm/dd/yy") & "#, " & TypeAttend & ", '" & Me![txtUserId] & "');"
      Debug.Print strSql
      DoCmd.RunSQL strSql
    ' ...

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

Similar Threads

  1. Replies: 1
    Last Post: 03-13-2015, 06:24 AM
  2. Replies: 1
    Last Post: 12-30-2013, 09:11 AM
  3. Error in "Insert Into" Query
    By WickidWe in forum Queries
    Replies: 16
    Last Post: 12-23-2013, 02:55 PM
  4. Replies: 5
    Last Post: 03-22-2013, 01:11 PM
  5. Replies: 10
    Last Post: 12-15-2010, 11:12 AM

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