Solved

After_Update code; Once run deactivate

Posted on 2007-03-28
23
283 Views
Last Modified: 2013-11-28
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
Comment
Question by:Karen Schaefer
  • 7
  • 7
  • 6
  • +1
23 Comments
 
LVL 28

Accepted Solution

by:
omgang earned 250 total points
Comment Utility
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
 
LVL 34

Expert Comment

by:jefftwilley
Comment Utility
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
 

Author Comment

by:Karen Schaefer
Comment Utility
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
 
LVL 28

Expert Comment

by:omgang
Comment Utility
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
 

Author Comment

by:Karen Schaefer
Comment Utility
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
 
LVL 34

Assisted Solution

by:jefftwilley
jefftwilley earned 250 total points
Comment Utility
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
 

Author Comment

by:Karen Schaefer
Comment Utility
Do you mean like this in my basGlobalDeclarations module:

Global blalreadyran As Boolean
Public Const blalreadyran = False
k
0
 
LVL 34

Expert Comment

by:jefftwilley
Comment Utility
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
 
LVL 28

Expert Comment

by:omgang
Comment Utility
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
 
LVL 34

Expert Comment

by:jefftwilley
Comment Utility
you're right OmGang...I missed that. You have it right
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:Karen Schaefer
Comment Utility
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
 

Author Comment

by:Karen Schaefer
Comment Utility
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
 
LVL 28

Expert Comment

by:omgang
Comment Utility
kfschaefer1, is every working fine EXCEPT when the user cuts & pastes records?
OM Gang
0
 

Author Comment

by:Karen Schaefer
Comment Utility
Yes, sorry to so long to respond - had computer issues.

thanks,
K
0
 
LVL 34

Expert Comment

by:jefftwilley
Comment Utility
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
 
LVL 28

Expert Comment

by:omgang
Comment Utility
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
 

Author Comment

by:Karen Schaefer
Comment Utility
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
 
LVL 28

Expert Comment

by:omgang
Comment Utility
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
 
LVL 34

Expert Comment

by:jefftwilley
Comment Utility
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
 
LVL 28

Expert Comment

by:omgang
Comment Utility
Request to split points with jefftwilley as I believe he was involved at least as much as I was.
OM Gang
0
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
Changed recommendation:

Split:  Accept: omgang {http:#18809631} ,  jefftwilley {http:#18809903}
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Overview: This article:       (a) explains one principle method to cross-reference invoice items in Quickbooks®       (b) explores the reasons one might need to cross-reference invoice items       (c) provides a sample process for creating a M…
I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now