Results 1 to 9 of 9
  1. #1
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    193

    Wink Another CurrentDB.Execute question. Error 3705

    Thank you for viewing my question.

    I have been looking through this forum and others for a solution, but haven't been able to find one that works in this way.

    I want to update xEmployees.SessionCount with the value 0, when a certain condition is met, but am having issues with the Where clause (I think).

    Basically SessionCount will be incremented by 1 until a threshold is reached, at which point I want to reset the value of SessionCount to 0.


    The code below throws up Error 3705.



    Code:
    CurrentDb.Execute "UPDATE xEmployees SET SessionCount = 0" & "WHERE xEmployees.WebUsername = " & LoggedUserName
    LoggedUserName is a public variable.

    I've successfully done similar things in the past, but always based the Where clause on values taken from a form, which seems to be what most examples I have found show.

    I'm pretty sure it's a syntax issue, but haven't been able to figure it out.

    Any assistance will be greatly appreciated.

  2. #2
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    And 3075 is?

    Looking at your code, you have no spaces between 0 and where?
    Do yourself a favour. Put the sql into a string variable and debug.print it until you get it correct. Then use that in the Execute command.

    Why are you even concatenating that when it could be a hardcoded string?
    Also if LoggedUserName is a string then you need to surround it with single quotes or triple double quotes if that would contain a single quote.
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

  3. #3
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    193
    Hi Welshgasman

    Thank you for your reply and suggestions. I'm running the code in the immediate window to test it out.

    I've changed the code to
    Code:
    CurrentDb.Execute "UPDATE xEmployees SET SessionCount = 0 " & "WHERE xEmployees.WebUsername = " & "LoggedUserName"
    But now get the error 3061. Too few paramaters. Expected 1


    I'm interested in your comment, but don't quite follow.

    Why are you even concatenating that when it could be a hardcoded string?

  4. #4
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    you may also try:
    Code:
    CurrentDb.Execute "UPDATE xEmployees SET SessionCount = 0 WHERE xEmployees.WebUsername = '" & Replace$(LoggedUserName,"'", "''") & "'"

  5. #5
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    193
    Hi JoJo

    Thank you for the reply. Your suggestion works, but I don'r understand why, and I'd like to.

    I'm good up to this point. Replace$(LoggedUserName,"'", "''") & "'"

    It's not something I've seen before. Could you explain a little please?

    Quote Originally Posted by jojowhite View Post
    you may also try:
    Code:
    CurrentDb.Execute "UPDATE xEmployees SET SessionCount = 0 WHERE xEmployees.WebUsername = '" & Replace$(LoggedUserName,"'", "''") & "'"

  6. #6
    jojowhite's Avatar
    jojowhite is offline Competent Performer
    Windows 11 Access 2021
    Join Date
    Jan 2025
    Posts
    434
    if your LoggedUserName variable has single quoute name like:
    Code:
    O'Donnel
    your query will not work and you will get error message.

    the Expression is simply means to replace all occurrence of single quote (')
    with double single quote (''). After replacement your query will work.

    don't worry if youre LoggedUserName don't have single quote, but
    who knows in the future.

  7. #7
    sheusz is offline Competent Performer
    Windows 10 Access 2016
    Join Date
    May 2015
    Posts
    193
    Thanks Jojo

    That's a good point. I never thought of that. Thank you.

  8. #8
    CJ_London is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,933
    LoggedUserName is a public variable.
    shouldn’t this be a tempvar to use in a query? Only way a public variable can be used is via a function. Always assumed that needed to be a function that returned the value but perhaps using the replace function is also a way

  9. #9
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,556
    Quote Originally Posted by sheusz View Post
    Hi Welshgasman

    Thank you for your reply and suggestions. I'm running the code in the immediate window to test it out.

    I've changed the code to
    Code:
    CurrentDb.Execute "UPDATE xEmployees SET SessionCount = 0 " & "WHERE xEmployees.WebUsername = " & "LoggedUserName"
    But now get the error 3061. Too few paramaters. Expected 1


    I'm interested in your comment, but don't quite follow.
    This
    Code:
    SET SessionCount = 0" & "WHERE

    when it can just be
    Code:
    SET SessionCount = 0 WHERE
    Please use # icon on toolbar when posting code snippets.
    Cross Posting: https://www.excelguru.ca/content.php?184
    Debugging Access: https://www.youtube.com/results?sear...bug+access+vba

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

Similar Threads

  1. Replies: 18
    Last Post: 10-17-2022, 06:13 AM
  2. CurrentDb.Execute Question
    By d9pierce1 in forum Programming
    Replies: 10
    Last Post: 09-02-2020, 10:53 AM
  3. RunSQL vs. QueryDef.Execute vs. CurrentDB.Execute
    By caubetiep1337 in forum Queries
    Replies: 8
    Last Post: 12-16-2015, 05:35 PM
  4. Error in CurrentDb.Execute
    By Stefan Moser in forum Access
    Replies: 5
    Last Post: 04-17-2015, 01:38 PM
  5. Replies: 12
    Last Post: 01-24-2014, 02:18 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