Results 1 to 3 of 3
  1. #1
    Artist.Anon is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    22

    Read and update a field in another table.


    A simple request and surely a simple answer. My code looks messy etc. it must be easier to do than what I am trying. I would appreciate if someone would take the time to help a new VBAer with this and let me know what the code would be to accomplish this:

    I want to read the value from a table 'systemDefaults', field 'sequenceNumber' in the first record (ID=1) and then update it by 1.

    Thank you.

  2. #2
    Evilferret is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Aug 2012
    Posts
    27
    Mostly the code is SQL not VBA, but here you go

    Code:
    DoCmd.RunSQL "SELECT sequenceNumber FROM systemDefaults WHERE ID=1"
    DoCmd.RunSQL "UPDATE systemDefaults SET sequenceNumber=sequenceNumber+1 WHERE ID=1"
    You said you want to read the value and then update it. The SELECT statement above will read the value, but it's probably not what you're looking for as you can't set a variable equal to a select statement (Since it returns a recordset, not a value)... You didn't really define why you wanted to read it though.

    If you're trying to set a variable in your program equal to the value prior to the update, this would be your code (Replace VariableName with the variable you want to store the value in):

    Code:
    VariableName = DLookup("sequenceNumber", "systemDefaults", "ID=1") 
    DoCmd.RunSQL "UPDATE systemDefaults SET sequenceNumber=sequenceNumber+1 WHERE ID=1"
    If you only want to do the update and don't care about reading and storing the value, you can just run this:

    Code:
    DoCmd.RunSQL "UPDATE systemDefaults SET sequenceNumber=sequenceNumber+1 WHERE ID=1"

  3. #3
    Artist.Anon is offline Novice
    Windows 7 64bit Access 2010 64bit
    Join Date
    Aug 2012
    Posts
    22
    Thank you. Just what I needed. I had found the DLookUp but couldn't find the DUpdate, hehe

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

Similar Threads

  1. Replies: 12
    Last Post: 03-17-2012, 04:46 AM
  2. Can Not Update. Object is Read Only
    By mrfixit1170 in forum Programming
    Replies: 3
    Last Post: 01-03-2012, 03:35 PM
  3. Replies: 1
    Last Post: 08-31-2011, 04:03 PM
  4. Allow Read or Update dep on record
    By AndreT in forum Forms
    Replies: 2
    Last Post: 07-31-2011, 09:46 PM
  5. Update field in table after update on a subform
    By jpkeller55 in forum Queries
    Replies: 3
    Last Post: 09-30-2010, 08:02 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