Results 1 to 8 of 8
  1. #1
    easyrider is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64

    How to correctly reference the primary key in a WHERE clause

    What is the best way to reference the primary key in a WHERE clause? I want to update a specific field in a Record that is currently shown on the Form.

  2. #2
    orange's Avatar
    orange is offline Moderator
    Windows 10 Office 365
    Join Date
    Sep 2009
    Location
    Ottawa, Ontario, Canada; West Palm Beach FL
    Posts
    16,870
    If the form is bound, you could update the control on the form.
    Perhaps there's more to your post.

  3. #3
    easyrider is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    I've seen countless examples that shows the WHERE clause having a literal value in it such as WHERE SomeField = 'x'. What I'm wondering is how you reference the ID field of the record so that the SQL UPDATE statement only changes that record.

    For example, if I have a table of employee salaries and a form to display this information, I would like to put a button on the form that (1) unlocks the Salary text box which gives the user the option to change the data. Then when they leave that control (On Exit) the following command runs:

    DoCmd.RunSQL UPDATE Salary_T SET Salary = NewSalary WHERE ........................

    How do I write the code to have the WHERE clause change only the salary in the current record?

  4. #4
    moke123's Avatar
    moke123 is offline Me.Dirty=True
    Windows 7 32bit Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Ma.
    Posts
    1,879
    If you have a field on your form named EmployeeID it would be something like
    Code:
    ... Where EmployeeID = " & me.EmployeeID
    You can use the Me keyword when in a forms module rather than Forms("YourFormName").EmployeeID. Me refers the the Form.

    If referencing the new salay textbox it may be like below.

    Code:
    dim strSql as string
    
    strSql = "UPDATE Salary_T SET Salary = " & Me.NewSalary  & " Where EmployeeID = " & me.EmployeeID
    
    Debug.Print strSql
    
    Currentdb.Execute strSql,dbFailOnError
    you may have to add delimiters depending on datatype
    If this helped, please click the star * at the bottom left and add to my reputation- Thanks

  5. #5
    easyrider is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    Thanks, that worked!

  6. #6
    ssanfu is offline Master of Nothing
    Windows 10 Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Quote Originally Posted by easyrider View Post
    I've seen countless examples that shows the WHERE clause having a literal value in it such as WHERE SomeField = 'x'. What I'm wondering is how you reference the ID field of the record so that the SQL UPDATE statement only changes that record.

    For example, if I have a table of employee salaries and a form to display this information, I would like to put a button on the form that (1) unlocks the Salary text box which gives the user the option to change the data. Then when they leave that control (On Exit) the following command runs:

    DoCmd.RunSQL UPDATE Salary_T SET Salary = NewSalary WHERE ........................

    How do I write the code to have the WHERE clause change only the salary in the current record?
    Why do you not just edit the Salary text box? Why use the code?


    The button would unlock the Salary text box, the user enters the NewSalary amount, the control exit event locks the Salary text box again.

    Just curious.......

  7. #7
    easyrider is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Dec 2021
    Location
    Lancaster County, PA
    Posts
    64
    Quote Originally Posted by ssanfu View Post
    Why do you not just edit the Salary text box? Why use the code?


    The button would unlock the Salary text box, the user enters the NewSalary amount, the control exit event locks the Salary text box again.

    Just curious.......
    Steve, thanks for the suggestion however my question was a simplified version of a more complex solution that I was looking for in regards to the WHERE clause syntax. I try to keep things short & sweet when posting questions as opposed to going into long, detailed explanations and pasting long lines of code.

    I was amazed at how many examples there are out there that only show the WHERE clause with a literal value.

  8. #8
    Join Date
    Jan 2017
    Location
    Swansea,South Wales,UK
    Posts
    6,555
    Quote Originally Posted by easyrider View Post
    I was amazed at how many examples there are out there that only show the WHERE clause with a literal value.
    Well it is only one step further to use a variable?
    Of course then you need to allow for dates or strings, or strings with ' in them.
    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. Need help with a WHERE clause
    By DB88 in forum Access
    Replies: 10
    Last Post: 06-11-2014, 01:40 PM
  2. Replies: 15
    Last Post: 12-10-2012, 06:37 PM
  3. WHERE clause
    By fabiobarreto10 in forum Forms
    Replies: 5
    Last Post: 04-12-2012, 02:42 PM
  4. Replies: 4
    Last Post: 03-13-2012, 12:50 PM
  5. Replies: 2
    Last Post: 07-28-2011, 09:20 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