Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123

    Error in "Insert Into" Query

    Hi, I have the following SQL Statment in VBA that is coming up with the debugger (image below)

    Code:
    Dim varItm As Variant
    Set dbs = CurrentDb
    Dim sSQL As String
    sSQL = "INSERT INTO tblRosterRec ( Name, Date_, FctnID, EmpId ) " & _
    "SELECT qryAllocated.Name, tbl36hrWeekRosters.Date_, tblFunctions.ReliefCode, qryAllocated.EmpId " & _
    "FROM tbl36hrWeekRosters, qryAllocated INNER JOIN tblFunctions ON qryAllocated.FctnDesc = tblFunctions.FctnDesc " & _
    "WHERE tbl36hrWeekRosters.Date_ = " & Me.txtDate & "" & _
    "ORDER BY tblFunctions.ReliefCode;"
    dbs.Execute (sSQL)
    Click image for larger version. 

Name:	debug.png 
Views:	19 
Size:	21.5 KB 
ID:	14834

    Can someone please tell me where I'm going wrong.
    When I tried this query in the query builder it worked fine


    but where I have changed it for vba something has gone wrong.

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    Try changing this:
    "ORDER BY tblFunctions.ReliefCode;"
    ...to...
    " ORDER BY tblFunctions.ReliefCode;"
    Notice the leading " ".

  3. #3
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    I tried this and got the following error now.

    Click image for larger version. 

Name:	debug.png 
Views:	18 
Size:	20.1 KB 
ID:	14835

  4. #4
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    please note that "tbl36hrWeekRosters" only contains dates and is not joined to anything.
    the only reason this is used is so I could update the date column with the date contained in the form "txtdate" box

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    All we did is eliminate the 1st error the parser came to. I honestly do not know if you can contruct SQL that way. Try using the Query builder to construct this.

  6. #6
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Is Date_ field a date or text type? You show date criteria in dd/mm/yyyy structure. This will likely cause issues with date field type. Review: http://allenbrowne.com/ser-36.html

    When constructing SQL in VBA, criteria for a date field need # delimiters, for a text field need apostrophe.

    "WHERE tbl36hrWeekRosters.Date_ = #" & Me.txtDate & "#" & _

    However, date field and criteria values need to be in mm/dd/yyyy structure. If you have allowed input as dd/mm/yyyy, you will probably get inconsistent results. See the Allen Browne article.

    Table tbl36hrWeekRosters is not joined to other tables in the query. This will result in a Cartesian join of records. I am also not sure if the query will work with that arrangement.
    Last edited by June7; 12-22-2013 at 04:51 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  7. #7
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    Is there another way to insert into without using tbl36hrWeekRosters?
    All I need to add is the date that is from the listbox txtDate on the form

  8. #8
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    Maybe I am asking the wrong question...

    How can I "INSERT INTO" from Tables and forms at the same time?
    Code:
    "INSERT INTO tblRosterRec ( Name, Date_, FctnID, EmpId )
    Name, FctnID and EmpID come from tables and Date_ will come from the form.

  9. #9
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Concatenate variables. Reference to control on form is a variable.

    Maybe:

    sSQL = "INSERT INTO tblRosterRec ( Name, FctnID, EmpId, Date_ ) " & _
    "SELECT qryAllocated.Name, tblFunctions.ReliefCode, qryAllocated.EmpId, " & Me.textbox & " AS Date_ " & _
    "FROM qryAllocated INNER JOIN tblFunctions ON qryAllocated.FctnDesc = tblFunctions.FctnDesc ;"
    Last edited by June7; 12-22-2013 at 09:49 PM.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  10. #10
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    this is my query now, I am now getting a different error

    Code:
    sSQL = "INSERT INTO tblRosterRec ( Name, Date_, FctnID, EmpId ) " & _
    "SELECT qryAllocated.Name, " & Me.txtDate & " AS Date_&, Functions.ReliefCode, qryAllocated.EmpId" & _
    "FROM qryAllocated INNER JOIN tblFunctions ON qryAllocated.FctnDesc = tblFunctions.FctnDesc;"
    Click image for larger version. 

Name:	debug.png 
Views:	15 
Size:	20.4 KB 
ID:	14841

  11. #11
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Remove the &:

    AS Date_&,

    Need a space after EmpID:

    .EmpID "
    "FROM
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  12. #12
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    I am still getting an error "Too Few Parameters. Expected 4".

    this SQL below is straight out of a query in Access itself and is the original I used.
    Note that I have still included the tbl36hrWeekRosters table with no joins.
    when I run this query, it does exactly what it needs to do and adds the data correctly to the tblRosterRec table.
    Code:
    INSERT INTO tblRosterRec ( Date_, FctnID, EmpId, Name )
    SELECT tbl36hrWeekRosters.Date_, tblFunctions.ReliefCode, qryAllocated.EmpId, qryAllocated.Name
    FROM tbl36hrWeekRosters, qryAllocated INNER JOIN tblFunctions ON qryAllocated.FctnDesc = tblFunctions.FctnDesc
    WHERE tbl36hrWeekRosters.Date_=[Forms]![Form1]![txtDate];
    Maybe you might be able to translate it for use with vba better than I did.

  13. #13
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Assuming the code is behind Form1:

    strSQL = "INSERT INTO tblRosterRec ( Date_, FctnID, EmpId, Name ) " & _
    "SELECT tbl36hrWeekRosters.Date_, tblFunctions.ReliefCode, qryAllocated.EmpId, qryAllocated.Name " & _
    "FROM tbl36hrWeekRosters, qryAllocated INNER JOIN tblFunctions ON qryAllocated.FctnDesc = tblFunctions.FctnDesc " & _
    "WHERE tbl36hrWeekRosters.Date_=#" & Me.txtDate & "#";
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  14. #14
    WickidWe is offline Competent Performer
    Windows 7 64bit Access 2010 32bit
    Join Date
    Nov 2013
    Posts
    123
    I am still getting the error "too few parameters. Expected 3" with this.
    Is there a way to just command the access query to run if I save the query as qryRosterLog?

  15. #15
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,632
    Ooops! The semi-colon should be within quote marks or remove it because it isn't really needed with action sql.

    However, that still doesn't explain why the other sql fails. The debugger actually stops on the sql construct?

    Then check out DoCmd.OpenQuery
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 1
    Last Post: 03-14-2013, 12:39 PM
  2. Export "Query or Report" to a "Delimited Text File"
    By hawzmolly in forum Import/Export Data
    Replies: 3
    Last Post: 08-31-2012, 08:00 AM
  3. Replies: 4
    Last Post: 07-25-2012, 04:01 AM
  4. Replies: 2
    Last Post: 11-04-2011, 02:45 AM
  5. "Group By" causes "ODBC--Call Failed" error
    By kaledev in forum Queries
    Replies: 1
    Last Post: 03-09-2011, 02:43 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