Results 1 to 13 of 13
  1. #1
    JayRab is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Oct 2013
    Posts
    78

    CurrentDB.Execute Syntax error.... or a better way to insert

    Ok trying to update specific values into a query. here is the code I have now



    Code:
    Dim strtime As Date
    Dim streqid As Integer
    Dim strstatus As String
    streqid = Me.ID
    strtime = Me.Time_Delivered
    strstatus = "Closed"
    
    strSQL = "UPDATE HotCallsQ SET HotCallsQ.Status = '" & strstatus & "', HotCallsQ.[Time Job Ended] = #" & strtime & "# WHERE (((HotCallsQ.[Equipment DownTime ID])=" & streqid & "))"
    Debug.Print (strSQL)
    CurrentDb.Execute (strSQL)
    strtime is a date
    streqid is autonumber

    the print results give the following:


    UPDATE HotCallsQ SET HotCallsQ.Status = 'Closed', HotCallsQ.[Time Job Ended] = #1/24/2014 1:50:00 AM# WHERE (((HotCallsQ.[Equipment DownTime ID])=313))

    I get the too few parameters error on the CurrentDb.Execute line.

    Any help would be appreciated.

    Or is there is a better way to do this besides SQL string?

    Thanks

  2. #2
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    What happens if you paste the SQL from the immediate window into a new query object's SQL view? Are the three field names in table HotCallsQ spelled correctly?

  3. #3
    JayRab is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Oct 2013
    Posts
    78
    It works correct if I copy and paste in SQL window.

    I built the SQL string from getting it to work in SQL query window and just adding in the concatenated variables

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Another possible difference between the original SQL and what the Immediate Window provided could be the literal values provided by the variables. Running an UPDATE query using the literal values might recreate the exception. Maybe revert the record to a previous state and test the SQL.

    It is just that I do not see where it is going wrong. I usually write my Execute lines like
    CurrentDb.Execute strSQL
    without the parenthesis

    But I doubt that is the issue. So, I am still stuck on recreating the error in a query object using the SQL provided by the Immediate Window.

  5. #5
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Just for kicks, what happens if you try running it with this command?

    Code:
    DoCmd.RunSQL strSQL

  6. #6
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    I am thinking that is a good idea

  7. #7
    JayRab is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Oct 2013
    Posts
    78
    Works now with the do command... wonder why that was???

    Also how to I keep them from having to click the YES to you are about to update rows?

  8. #8
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Works now with the do command... wonder why that was???

    Also how to I keep them from having to click the YES to you are about to update rows?
    Not sure, but I have always had better luck doing it that way.

    To suppress the messages, use this:
    Code:
    DoCmd.SetWarnings False
    DoCmd.RunSQL strSQL
    DoCmd.SetWarnings True

  9. #9
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    There is a reason and I can't remember why. Thanks Joe

  10. #10
    JayRab is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Oct 2013
    Posts
    78
    Awesome Guys. I appreciate all the help. Wish there was a way to REP people who give great help.

  11. #11
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    Awesome Guys. I appreciate all the help. Wish there was a way to REP people who give great help.
    Your welcome. There is. See: https://www.accessforums.net/forum-s...ped-30072.html

    There is a reason and I can't remember why. Thanks Joe
    If you remember or figure out why, let me know. I would love to know too!
    There are some interesting discussions out there on the web on the two methods, like:
    http://www.utteraccess.com/wiki/inde...SQL_vs_Execute
    and
    http://www.eileenslounge.com/viewtopic.php?f=29&t=9414

    I am sure if I had the time to sift through them all, eventually I could probably figure out why.

  12. #12
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2013
    Posts
    7,862
    Paul explained it to me once. I will shoot you a PM if my rusty cage rattles the memory loose.

  13. #13
    JayRab is offline Advanced Beginner
    Windows 7 32bit Access 2007
    Join Date
    Oct 2013
    Posts
    78
    Rep all who helped me thanks for that. I will read up on them. Fairly new to a lot of this. Had a Visual Basic course back in college but havent used that in 8 years. Thankfully spent a lot of time with Matlab and learned a lot of the coding and loping in that which helps me.

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

Similar Threads

  1. Problems with CurrentDb.Execute "UPDATE
    By GraeagleBill in forum Programming
    Replies: 4
    Last Post: 04-06-2013, 03:21 PM
  2. Replies: 3
    Last Post: 03-05-2013, 11:17 AM
  3. Syntax Error: Insert Into statement in VBA
    By Kimbertha in forum Programming
    Replies: 1
    Last Post: 07-24-2012, 05:02 PM
  4. Update sql using currentdb.execute
    By laavista in forum Access
    Replies: 13
    Last Post: 08-15-2011, 03:51 PM
  5. INSERT INTO Syntax Error
    By eww in forum Programming
    Replies: 1
    Last Post: 04-06-2011, 10:28 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