Results 1 to 14 of 14
  1. #1
    Carissathorne is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    7

    Update one field based on change to another in same table

    Hi I'm a newbie at Access and I've come across a problem I can't easily Google the answer to.

    I have a table of client data. One field is the Spouse ID, which stores the record ID number of the client who is the spouse of said client. I have a form that I am using to edit the information, and when I update the Spouse ID in that form for one client I would like it to also update the Spouse ID of the client's spouse to the Record ID of the current client, so they always match up with each other. What's the easiest way to do this? VBA? Thanks for any help!

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,522
    an update query
    after the 'save' or afterupdate event

    docmd.openquery "quUpdSpouseID"

    the sql would use the current spouseID as the ClientID then update its spouseID from ClientID

  3. #3
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,891
    And if there is not already a client record for the spouse would need to instead open a form to enter new record. Then possibly also have to update records to remove spouse ID, assuming client can't have two spouses. I suppose it would be rare for your clients to switch spouses with each other, but not impossible.
    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.

  4. #4
    Carissathorne is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    7
    Thanks I'm not sure how to reference the field in the query...haven't done much other than basic lookup queries before. How do I tell it to update that field in another record if it matches a particular field in the current record? I'm missing how to do the syntax for referencing different records but same field.

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,891
    I would run this in VBA:

    CurrentDb.Execute "UPDATE Clients SET SpouseID = " & Me.ClientID & " WHERE ClientID = " & Me.SpouseID
    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.

  6. #6
    Carissathorne is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    7
    Yep works perfect... thanks! So Me.xxxx is referencing the current record is that right?

    Oh forgot to add that I don't think I'll need a separate form for entering a spouse that doesn't exist yet... its a drop down box where you can only choose someone who is already in the database. So if it's a new client the first one will go in with no spouse then the second one I will be able to choose the spouse that I already entered. Should work fine for my purposes.

    Another question....is there a way to have it open a specific folder in file explorer with a button click? We store correspondence etc. in a shared folder specific to each client. I would have to add a field with the folder path for each client which I would have to enter manually for each one...but it would be worth it in the end.

  7. #7
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,891
    Options:

    1. hyperlink type field, no VBA code needed

    2. text field stores path and code opens the folder, research FollowHyperlink method, review http://allenbrowne.com/func-GoHyperlink.html
    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.

  8. #8
    Carissathorne is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    7
    One issue... if I add a spouse and then delete the spouse, I'm getting an error message. Run-time error '3075': Syntax error (missing operator) in query expression 'ID ='

    here's the code I have

    CurrentDb.Execute "UPDATE DataClients SET SpouseID = " & Me.ID & " WHERE ID = " & Me.SpouseID

  9. #9
    Carissathorne is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    7
    Quote Originally Posted by June7 View Post
    Options:

    1. hyperlink type field, no VBA code needed

    2. text field stores path and code opens the folder, research FollowHyperlink method, review http://allenbrowne.com/func-GoHyperlink.html
    Looks like that will work great thanks for the info!

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,891
    You mean you are just deleting value from SpouseID field, not deleting record?

    If IsNull(Me.SpouseID) Then
    CurrentDb.Execute "UPDATE DataClients SET SpouseID = Null WHERE ID = " & Me.SpouseID.OldValue
    Else
    CurrentDb.Execute "UPDATE DataClients SET SpouseID = " & Me.ID & " WHERE ID = " & Me.SpouseID
    End If

    What event are you using?
    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.

  11. #11
    Carissathorne is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    7
    Quote Originally Posted by June7 View Post
    You mean you are just deleting value from SpouseID field, not deleting record?

    If IsNull(Me.SpouseID) Then
    CurrentDb.Execute "UPDATE DataClients SET SpouseID = Null WHERE ID = " & Me.SpouseID.OldValue
    Else
    CurrentDb.Execute "UPDATE DataClients SET SpouseID = " & Me.ID & " WHERE ID = " & Me.SpouseID
    End If

    What event are you using?
    I am using AfterUpdate event on the combobox for the data entry. When I add the code above I'm getting:

    Method or data member not found
    and it highlights .OldValue

    Here's the full code I have there now:

    Private Sub Combo138_AfterUpdate()




    CurrentDb.Execute "UPDATE DataClients SET SpouseID = " & Me.ID & " WHERE ID = " & Me.SpouseID


    If IsNull(Me.SpouseID) Then
    CurrentDb.Execute "UPDATE DataClients SET SpouseID = Null WHERE ID = " & Me.SpouseID.OldValue
    Else
    CurrentDb.Execute "UPDATE DataClients SET SpouseID = " & Me.ID & " WHERE ID = " & Me.SpouseID
    End If


    End Sub


    Without adding the code to set the record to null, it does actually work when I delete the value, if I save it, go to another record, then go back, it is deleted. It just gives me that syntax error box when I actually delete it.

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,891
    Remove the first Execute line that is not within the If Then structure.

    Well, the OldValue property works for me. The combobox is named SpouseID?
    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.

  13. #13
    Carissathorne is offline Novice
    Windows 10 Access 2016
    Join Date
    Nov 2017
    Posts
    7

    Thumbs up

    Quote Originally Posted by June7 View Post
    Remove the first Execute line that is not within the If Then structure.

    Well, the OldValue property works for me. The combobox is named SpouseID?
    Got it!

    Deleted the first line (duh should have noticed that)

    The combobox itself is named Combo138. I put Combo138 in place of SpouseID and it seems to be working now, no errors! Thanks so much for the help.
    Attached Thumbnails Attached Thumbnails Capture2.JPG   Capture.JPG  

  14. #14
    June7's Avatar
    June7 is online now VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,891
    Advise giving controls meaningful names. I always name different from field, such as: cbxSpouse.
    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.

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

Similar Threads

  1. Replies: 12
    Last Post: 06-28-2016, 11:22 PM
  2. Replies: 1
    Last Post: 06-18-2016, 10:51 AM
  3. Replies: 2
    Last Post: 10-27-2015, 09:12 AM
  4. Replies: 5
    Last Post: 11-01-2012, 09:26 AM
  5. Replies: 1
    Last Post: 08-31-2011, 04:03 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