Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

After_Update code; Once run deactivate

Posted on 2007-03-28
23
Medium Priority
?
335 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 7
  • 6
  • +1
23 Comments
 
LVL 28

Accepted Solution

by:
omgang earned 1000 total points
ID: 18809631
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
ID: 18809664
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
ID: 18809820
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
10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

 
LVL 28

Expert Comment

by:omgang
ID: 18809859
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
ID: 18809886
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 1000 total points
ID: 18809903
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
ID: 18809938
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
ID: 18809995
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
ID: 18810021
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
ID: 18810084
you're right OmGang...I missed that. You have it right
0
 

Author Comment

by:Karen Schaefer
ID: 18810139
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
ID: 18810152
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
ID: 18811906
kfschaefer1, is every working fine EXCEPT when the user cuts & pastes records?
OM Gang
0
 

Author Comment

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

thanks,
K
0
 
LVL 34

Expert Comment

by:jefftwilley
ID: 18812066
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
ID: 18812070
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
ID: 18812092
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
ID: 18812193
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
ID: 18812287
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
ID: 18958551
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
ID: 18963089
Changed recommendation:

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

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
What’s inside an Access Desktop Database. Will look at the basic interface, Navigation Pane (Database Container), Tables, Queries, Forms, Report, Macro’s, and VBA code.
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

704 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