Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 342
  • Last Modified:

After_Update code; Once run deactivate

Is there a way that will deactivate code onthe Forms After_update code.  

I have a subform that contains 2 columns of Checkboxes - My current code automatically updates the checkboxes accordingly, however, once they have been updated I need to be able to manual change them.  what do I need to do to the following code that will allow the user to manual change the checkboxes' status.

Private Sub Form_AfterUpdate()
     With Me.RecordsetClone
        .MoveFirst
        .Edit
        .Fields("Primary").Value = True
        .Fields("Additional").Value = False
        .Update
        .MoveNext
        Do While Not .EOF
            .Edit
            .Fields("Primary").Value = False
            .Fields("Additional").Value = True
            .Update
            .MoveNext
        Loop
    End With

thanks,

Karen
0
Karen Schaefer
Asked:
Karen Schaefer
  • 7
  • 7
  • 6
  • +1
2 Solutions
 
omgangIT ManagerCommented:
Create a module level variable

Dim blUpdated As Boolean

Revise your current procedure

Private Sub Form_AfterUpdate()
     If blUpdated = False Then
          With Me.RecordsetClone
             .MoveFirst
             .Edit
             .Fields("Primary").Value = True
             .Fields("Additional").Value = False
             .Update
             .MoveNext
             Do While Not .EOF
                 .Edit
                 .Fields("Primary").Value = False
                 .Fields("Additional").Value = True
                 .Update
                 .MoveNext
             Loop
         End With
         blUpdated = True
     End If


OM Gang
0
 
jefftwilleyCommented:
Hi Karen,

>>Once run deactivate

This is normally done with a boolean flag of some kind. You need to define though what it means to run once. What if we move to a new record, or what if you move between records. Will the "Run Once" action be only the first time after the form is open?

Dim blAlreadyRan as boolean  Form Level Variable


yourcode()
blAlreadyRan = false  <form level variable
if blAlreadyRan = false then
do your stuff
end if
blAlreadyRan = true <--sets to true...now the code won't run again until form is closed and opened again.

end your code

0
 
Karen SchaeferBI ANALYSTAuthor Commented:
It still wants to run the update when I change the checkbox status.
where is the best place to put this part of the code - Keep in mind that this code is run on the subform - so should it go on the mainform?
Dim blAlreadyRan as boolean  Form Level Variable
blAlreadyRan = false  <form level variable
__________________________________________________________
Option Compare Database
Dim blAlreadyRan As Boolean
Private Sub Form_AfterUpdate()
blAlreadyRan = False
If blAlreadyRan = False Then
   With Me.RecordsetClone
        .MoveFirst
        .Edit
        .Fields("Primary").Value = True
        .Fields("Additional").Value = False
        .Update
        .MoveNext
        Do While Not .EOF
            .Edit
            .Fields("Primary").Value = False
            .Fields("Additional").Value = True
            .Update
            .MoveNext
        Loop
    End With
blAlreadyRan = True
End If
End Sub
0
Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

 
omgangIT ManagerCommented:
Well - you are setting the module level variable to False every time the AfterUpdate event fires,,,and then you check to see if it is False (which it always is) and change all your checkboxes back again.  Did you try my suggestion at all?
OM Gang
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
Not sure what you mean create a module level boolean  - do you mean create a function and if so how would it look - as a global variable or as actual function that does something?
k
0
 
jefftwilleyCommented:
Hi Karen,
Try moving the Dim statement for your boolean to a code module and declare it as global.

The code is good otherwise...it will determine the boolean value before it adds or updates anything.

Do you think you might want to enable this again for any particular reason? This code will fire once in the after upate event...then as long as the form is open, you won't be able to run it again.

You might also want to reset the boolean in your form close event...so the global will get reset.
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
Do you mean like this in my basGlobalDeclarations module:

Global blalreadyran As Boolean
Public Const blalreadyran = False
k
0
 
jefftwilleyCommented:
yep...I don't think you'll need the construct statement though.

Now that variable is available from any form or module....so you don't have to worry whether you're on the main or subform.

You can set it from the code you already have...then reset it in your form's close event for the next time.
0
 
omgangIT ManagerCommented:
Jeff, either I'm missing something or your code sample (and the one she's using) always set's the variable to False immediately before testing the value of the same variable - it will always evaluate to False and the code inside the conditional will always fire
OM Gang

(Jeff's sample)
yourcode()
blAlreadyRan = false  <form level variable
if blAlreadyRan = false then
do your stuff
end if
blAlreadyRan = true <--sets to true...now the code won't run again until form is closed and opened again.

end your code


(Karen's sample)
Private Sub Form_AfterUpdate()
blAlreadyRan = False
If blAlreadyRan = False Then
   With Me.RecordsetClone
        .MoveFirst
        .Edit
        .Fields("Primary").Value = True
        .Fields("Additional").Value = False
        .Update
        .MoveNext
        Do While Not .EOF
            .Edit
            .Fields("Primary").Value = False
            .Fields("Additional").Value = True
            .Update
            .MoveNext
        Loop
    End With
blAlreadyRan = True
End If
End Sub
0
 
jefftwilleyCommented:
you're right OmGang...I missed that. You have it right
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
Now I am really confused - I set the global variable in my global variable module and set my code to not include the false statement - Correct?

K
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
Here is my latest version of code - Now it does not update the checkboxes if the user cuts and paste records - the checkboxes remain blank.
Option Compare Database

Global gAPFilePath As String
Global gAPNo As String
Global blalreadyran As Boolean

Public Const DEBUG_MODE = False

Option Compare Database

Private Sub Form_AfterUpdate()

If blalreadyran = False Then
   With Me.RecordsetClone
        .MoveFirst
        .Edit
        .Fields("Primary").Value = True
        .Fields("Additional").Value = False
        .Update
        .MoveNext
        Do While Not .EOF
            .Edit
            .Fields("Primary").Value = False
            .Fields("Additional").Value = True
            .Update
            .MoveNext
        Loop
    End With
blalreadyran = True
End If
End Sub
0
 
omgangIT ManagerCommented:
kfschaefer1, is every working fine EXCEPT when the user cuts & pastes records?
OM Gang
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
Yes, sorry to so long to respond - had computer issues.

thanks,
K
0
 
jefftwilleyCommented:
Hi Karen,
Now that we've created the boolean and the trigger to disable the code, do you want to make it act differently? You say that when the user cuts and pastes records...what do you expect in that scenario?
J
0
 
omgangIT ManagerCommented:
Just to clarify - the Sub Form_AfterUpdate event is on the subform or the main form?  If the user adds a record to the main form by cutting and pasting the checkboxes on the subform do not update?
OM Gang
0
 
Karen SchaeferBI ANALYSTAuthor Commented:
This code is only on the subform - the mainform data entry does not affect this bit of code.

It is on the afterupdate of the subform (Datasheet view).

K
0
 
omgangIT ManagerCommented:
Pasting a record into the subform's datasheet will still trigger the subform's AfterUpdate event.  As jefftwilley mentioned above you need to make sure you reset the global variable to False at some point - when the main form or subform closes, some other event, etc. - else it will remain set to True perpetually (untill the app closes) and prevent the code that follows the conditional from ever running again.
OM Gang
0
 
jefftwilleyCommented:
OM Gang is correct in his assessment. And I guess what I was trying to get you to tell us is how exactly you might want to control when you enable that code and when you don't want it enabled. A button or a separate checkbox on your main form could be used to set the boolean vs the code. you may consider this as the option also to reset your boolean in order to re-enable it as Om Gang suggested above.
J
0
 
omgangIT ManagerCommented:
Request to split points with jefftwilley as I believe he was involved at least as much as I was.
OM Gang
0
 
mbizupCommented:
Changed recommendation:

Split:  Accept: omgang {http:#18809631} ,  jefftwilley {http:#18809903}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 7
  • 7
  • 6
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now