Results 1 to 8 of 8
  1. #1
    sovereign is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    30

    Afterupdate form, Certain Fields

    Hi,

    I am looking to run a code if any one of a set of 10~15 Fields have been updated.

    Rather than putting the code into each individual field I was looking to do it on the whole form but then reduce the fields it checks.



    something like:

    sub Afterupdate

    if field1 or field 2 or field 3 ..... is updated then
    ....
    Else
    End If
    End Sub

  2. #2
    JoeM is offline VIP
    Windows XP Access 2007
    Join Date
    Jun 2012
    Posts
    3,904
    I think the issue is with checking the "AfterUpdate" event of the Form is going to be able to distinguish exactly which fields has been updated (I think it only knows that the record has been updated).

    What I would recommend doing is creating a Procedure that has the code that you want to run.
    Then in each of those 10-15 fields, simply have AfterUpdate code for those fields which calls that Procedure (so the code itself is not contained in each, only a call to the code).

  3. #3
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Just to be clear...do you want to run this code if any field is updated...or just if certain fields are updated?

    And is it one piece of code or multiple pieces of code, depending on which fields are updated?

    Linq ;0)>

  4. #4
    sovereign is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    30
    Joem- thats what i started to do but wasnt sure if there was a more efficient way.

    Linq - i want to run the same code if one of any one of the fields are changed

  5. #5
    sovereign is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    30
    Sorry pressed enter meant to clarifiy if any one of the certain fields are updated

  6. #6
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    Okay, you say 'updated,' which indicates you only want this on existing Records that are being edited, but not on New Records just being created...is that correct?

    If it is correct, the this will work:

    In Form Design View

    1. Press and Hold down <Shift>
    2. Right-Click, in turn, on each Control to be checked for change
    3. When done Click on Properties
    4. Click on the Other Tab
    5. In the Tag Property enter CC (just like that...no quotes)

    Now enter this code in the Form's code module:

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    
    Dim ctrl As Control
    
    Dim hits As Integer
    
    hits = 0
    
    If Not Me.NewRecord Then
    
     For Each ctrl In Me.Controls
        
      If TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox Then
                
       If ctrl.Tag = "CC" And Nz(ctrl.OldValue, "") <> Nz(ctrl.Value, "") Then
         hits = hits + 1
       End If
     
      End If
     
     Next ctrl
    
    If hits > 0 Then
     'place code to be run here
    End If
    
    End If
    
    End Sub

    In the code, replace the line

    'place code to be run here

    with the code to be run, or if code is in a Custom Function, the line to Call the CF.

    If you want the code to run in New and Existing Records, mark the Controls using the Tag Property, as above, then use this slightly modified code:

    Code:
    Private Sub Form_BeforeUpdate(Cancel As Integer)
    
    Dim ctrl As Control
    
    Dim hits As Integer
    
    hits = 0
    
     For Each ctrl In Me.Controls
        
      If TypeOf ctrl Is TextBox Or TypeOf ctrl Is ComboBox Then
                
       If ctrl.Tag = "CC" And Nz(ctrl.OldValue, "") <> Nz(ctrl.Value, "") Then
         hits = hits + 1
       End If
     
      End If
     
     Next ctrl
    
    If hits > 0 Then
     'place code to be run here
    End If
    
    End Sub

    Linq ;0)>

  7. #7
    sovereign is offline Advanced Beginner
    Windows 7 64bit Access 2013
    Join Date
    May 2015
    Posts
    30
    Linq.

    Using that code above will it not check all textbox or combo boxes for a change? Havent tested it yet as on way to work now.

  8. #8
    Missinglinq's Avatar
    Missinglinq is offline VIP
    Windows 7 64bit Access 2007
    Join Date
    May 2012
    Location
    Richmond (Virginia, not North Yorkshire!)
    Posts
    3,018
    The above codes will only check those Textboxes/Comboboxes whose Tag Properties are set to CC, as instructed. If they don't have CC in the Tag Property, they will not be checked.

    Linq ;0)>

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

Similar Threads

  1. Replies: 4
    Last Post: 10-29-2014, 03:49 PM
  2. Replies: 4
    Last Post: 02-27-2014, 03:39 PM
  3. Replies: 2
    Last Post: 06-09-2012, 01:56 AM
  4. Form AfterUpdate Event
    By RayMilhon in forum Forms
    Replies: 2
    Last Post: 09-09-2011, 09:20 AM
  5. Validation Rule Form AfterUpdate
    By GraemeG in forum Programming
    Replies: 6
    Last Post: 02-09-2011, 10:22 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