Cancel save after BeforeUpdate code runs

OK - thinking through this.

I suppose I could go to each control's On Dirty event and set the value of variable there - or more correctly - reset it from whatever I initially set it when I declare it in the module.

So that get's all 3 of the controls set to a green-light value if Dirty - but I'm not yet seeing how to tell the Save button that - "hey - the gang's all here and ready to go. You can enable your self now............."

Unless I have a routine that checks for all 3 statuses everytime one of the controls becomes dirty.............

I think I am about to make this more complex than it might need to be - - maybe not
 
Unless I have a routine that checks for all 3 statuses everytime one of the controls becomes dirty.............

I think I am about to make this more complex than it might need to be - - maybe not

What I COULD do is set all controls to enabled = False except for the very first required one and when it receives a value enable the next required control and so on and so forth - - and really - that may not be a such a bad idea.....because after the 3rd and last required control is enabled I can turn everything on including my Save button.

BUT - - I would love to hear other ideas!! As always!
 
How did you tell the code WHEN?

1. I put a flag for each relevant control in the form's Declaration area (before any code entry points). A simple BOOLEAN sufficed in most cases.

2. I used the form's OnCurrent event to control all relevant flags. If I made a new record (which I could tell because I had a CREATE command button that left behind evidence) I then cleared the flags. If the OnCurrent event was NOT a new record because of simple navigation, the "evidence" was different. In that case, all the flags were tested based on their current values to see if they were legal. That test was in the OnCurrent routine.

3. I used the "LostFocus" event on each control that needed to be checked. It checked ONLY that control's value but then at the end of each such event, it tested all the flags in the IF-THEN-ELSE-ENDIF block that I showed.

4. EVERY control had a LostFocus event that would enable the appearance of the SAVE button (and UNDO and several others, for that matter) if and only if the user dirtied the control. So the actual test was slightly more complicated. It was something like

Code:
IF the form is dirty AND all flags say OK THEN
    Enable the SAVE button
Else
    Disable the SAVE button
End If
If the firm is dirty THEN
    Enable the UNDO button
    Disable the CREATE button
    Disable the REMOVE button
    Disable the CLOSE button
    Disable navigation controls
Else
    Disable the UNDO button
    Enable the CREATE button
    Enable the REMOVE button
    Enable the CLOSE button
    Enable navigation controls
End If

Something like that. The navigation controls are enabled using the form Properties under Format, I think. The other things I mentioned were command-button controls that could be used on my forms in many cases. And there are button wizards for each of those functions so it was easy to set them up.
 
1. I put a flag for each relevant control in the form's Declaration area (before any code entry points). A simple BOOLEAN sufficed in most cases.

2. I used the form's OnCurrent event to control all relevant flags. If I made a new record (which I could tell because I had a CREATE command button that left behind evidence) I then cleared the flags. If the OnCurrent event was NOT a new record because of simple navigation, the "evidence" was different. In that case, all the flags were tested based on their current values to see if they were legal. That test was in the OnCurrent routine.

3. I used the "LostFocus" event on each control that needed to be checked. It checked ONLY that control's value but then at the end of each such event, it tested all the flags in the IF-THEN-ELSE-ENDIF block that I showed.

4. EVERY control had a LostFocus event that would enable the appearance of the SAVE button (and UNDO and several others, for that matter) if and only if the user dirtied the control. So the actual test was slightly more complicated. It was something like

Code:
IF the form is dirty AND all flags say OK THEN
    Enable the SAVE button
Else
    Disable the SAVE button
End If
If the firm is dirty THEN
    Enable the UNDO button
    Disable the CREATE button
    Disable the REMOVE button
    Disable the CLOSE button
    Disable navigation controls
Else
    Disable the UNDO button
    Enable the CREATE button
    Enable the REMOVE button
    Enable the CLOSE button
    Enable navigation controls
End If

Something like that. The navigation controls are enabled using the form Properties under Format, I think. The other things I mentioned were command-button controls that could be used on my forms in many cases. And there are button wizards for each of those functions so it was easy to set them up.

Is there a simple piece of code to disable all controls on Load?
 
Yes, sort of, by looping through the controls collection.
 
Yes, sort of, by looping through the controls collection.

OK....next question.....all 12 controls have been enabled=False - - - -THEN - - - - first required field is enabled (all of this on load).

Once 1st control is enabled and data entered On Dirty of 1st control enables 2nd required control, same thing happens now 3rd control is Dirty..........


When 3rd and final required control is Dirty is there an elegant way to turn ALL remaining controls on to enabled = True at once?

Something totally logical like:

Me.AllControlsOnThisForm.Enabled = True

Something super simple like that? :-)

Thanks!
 
Last edited:
Sadly, no. There is an all or nothing one, but not something like “the rest” or “whatever is left.”
 
Well, actually there IS a way to do this if for every form you ALWAYS use the same names for the command buttons that cover these functions. This is plucking stuff from my ratty old memory now because as I said, I didn't own the actual code so couldn't publish it if I had it.

What I did was create a state variable where the states were things like Normal, DirtyInc, DirtyReady, New, ReadOnly, etc. Just a list of constants defined in a global module declaration area. I used the ENUM structure to do that. Then in that global module, I had a subroutine where you passed in a form and a state variable. It looped through the controls and set them to enabled, disabled, locked, etc. according to that state variable. It was a loop containing a SELECT CASE of the state variable and it had code to test the type of each control. After the end of the loop, the "special" command buttons were handled separately because the loop wouldn't touch a command button.

So the final SELECT CASE would set appropriate states for the command buttons. Like if you were "NORMAL" then you could see CLOSE, CREATE, and REMOVE. If you set"DIRTYINC" (dirty but incomplete) you would see UNDO. If it finally reached "DIRTYREADY" then SAVE would light up. For "NEW" you might see UNDO but I also had a visual indicator that said "NEW RECORD." When it switched to "DIRTYREADY" the indicator for the new record wouldn't go away until you clicked UNDO or SAVE.

The call was something like:

Code:
Public Sub SetCtls( Frm as Access.Form, Stat as Long)

The invocation might look like

Code:
If Me.Dirty Then
    If OK1 And OK2 And OK3 THEN
        SetCtls( Me, lDirtyReady )
    Else
        SetCtls( Me, lDirtyInc )
    End If
Else
    SetCtls( Me, lNormal )
End If

There was actually more logic behind it than that because of the NEW state and cases where the person in question had viewing rights but not editing rights.
 
Last edited:
Well, actually there IS a way to do this if for every form you ALWAYS use the same names for the command buttons that cover these functions.

Thanks Doc! I understand the logic of it. I'll let you know if I am able to pull it off or get stuck.

Thanks again,

Tim
 
Yeah, not very useful in most situations.

So......I got to thinking about your use of tags and how I now have 3 controls all having the value "required" on their tags.

I am wondering about the tags of the other controls ..... I am wondering if I placed the value "secondary" in all them, if after enabling the 3rd required control if there is a way to loop through all controls looking for the value"secondary" in the tag and enabling them via that route.

I have never looped through controls to check values of properties before so I don't know what that looks like.....but do you think that might work? I think it might.

I'm going "deep diving" now to google "looping controls to check values."
 
So......I got to thinking about your use of tags and how I now have 3 controls all having the value "required" on their tags.

I am wondering about the tags of the other controls ..... I am wondering if I placed the value "secondary" in all them, if after enabling the 3rd required control if there is a way to loop through all controls looking for the value"secondary" in the tag and enabling them via that route.

I have never looped through controls to check values of properties before so I don't know what that looks like.....but do you think that might work? I think it might.

I'm going "deep diving" now to google "looping controls to check values."
Hi. Yes, you can loop through all the controls, check the value in the Tag property, and then respond accordingly. For example, the following will hide all controls with a tag of "hide."
Code:
For Each ctl in Me.Controls
    If ctl.Tag="hide" Then
        ctl.Visible = False
    End If
Next
So, basically, you go through each control, check the value of the Tag, and then do whatever you want with the control.
 
Hi. Yes, you can loop through all the controls, check the value in the Tag property, and then respond accordingly. For example, the following will hide all controls with a tag of "hide."
Code:
For Each ctl in Me.Controls
    If ctl.Tag="hide" Then
        ctl.Visible = False
    End If
Next
So, basically, you go through each control, check the value of the Tag, and then do whatever you want with the control.

That seems like a relatively simple way to enable all the remaining controls on the form I think.
 
Hi. Yes, you can loop through all the controls, check the value in the Tag property, and then respond accordingly. For example, the following will hide all controls with a tag of "hide."
Code:
For Each ctl in Me.Controls
    If ctl.Tag="hide" Then
        ctl.Visible = False
    End If
Next
So, basically, you go through each control, check the value of the Tag, and then do whatever you want with the control.

The real overwhelming bugger for me is that I need to figure out how to interject my idea into this existing and critical and functioning On Load code:

Code:
Private Sub Form_Load()
    
    Me.cmdSaveLineStop.Enabled = False
    
  Dim rs As DAO.Recordset
    If Not Trim(Me.OpenArgs & " ") = "" Then
        'See if record exists
        Set rs = Me.Recordset
        'MsgBox Me.OpenArgs
        rs.FindFirst "InspectionEvent_FK = " & CLng(Me.OpenArgs)
            If rs.NoMatch Then  'it does not exist so you need to create it
                DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
                Me.InspectionEvent_FK = Me.OpenArgs
                Me.txtFinalProd_FK = intFinalProdID
                Me.NavigationButtons = False
            End If
    Else
        If IsNull(Me.OpenArgs) Then
            Me.Filter = "LineStopBegin Is Not Null AND LineStopEnd Is Null"
            Me.FilterOn = True
            Me.NavigationButtons = True
        End If
    End If

I think it probably will start after the first "End If"
 
The real overwhelming bugger for me is that I need to figure out how to interject my idea into this existing and critical and functioning On Load code:

Code:
Private Sub Form_Load()
    
    Me.cmdSaveLineStop.Enabled = False
    
  Dim rs As DAO.Recordset
    If Not Trim(Me.OpenArgs & " ") = "" Then
        'See if record exists
        Set rs = Me.Recordset
        'MsgBox Me.OpenArgs
        rs.FindFirst "InspectionEvent_FK = " & CLng(Me.OpenArgs)
            If rs.NoMatch Then  'it does not exist so you need to create it
                DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
                Me.InspectionEvent_FK = Me.OpenArgs
                Me.txtFinalProd_FK = intFinalProdID
                Me.NavigationButtons = False
            End If
    Else
        If IsNull(Me.OpenArgs) Then
            Me.Filter = "LineStopBegin Is Not Null AND LineStopEnd Is Null"
            Me.FilterOn = True
            Me.NavigationButtons = True
        End If
    End If

I think it probably will start after the first "End If"

Nope - I'm wrong - only the code relating to the initial 1 of 3 required controls goes there.....and code to make Enabled = False for all other controls - - -and it goes before the End If
 
This code opens the form in the state I want to see it. I am wondering if there is a more succinct way to code it.......can the Loops be combined somehow?

Code:
Private Sub Form_Load()
    

    
  Dim rs As DAO.Recordset
    If Not Trim(Me.OpenArgs & " ") = "" Then
        'See if record exists
        Set rs = Me.Recordset
        'MsgBox Me.OpenArgs
        rs.FindFirst "InspectionEvent_FK = " & CLng(Me.OpenArgs)
            If rs.NoMatch Then  'it does not exist so you need to create it
                DoCmd.GoToRecord acDataForm, Me.Name, acNewRec
                Me.InspectionEvent_FK = Me.OpenArgs
                Me.txtFinalProd_FK = intFinalProdID
                Me.NavigationButtons = False
                Me.cmdSaveLineStop.Enabled = False
                For Each ctl In Me.Controls
                    If ctl.Tag = "required" Then
                    ctl.Enabled = False
                    End If
                Next
                For Each ctl In Me.Controls
                    If ctl.Tag = "secondary" Then
                    ctl.Enabled = False
                    End If
                Next
                Me.cboLine1Workstation.Enabled = True
            End If
    Else
        If IsNull(Me.OpenArgs) Then
            Me.Filter = "LineStopBegin Is Not Null AND LineStopEnd Is Null"
            Me.FilterOn = True
            Me.NavigationButtons = True
        End If
    End If
End Sub
 
by the way and for what it is worth - - that tag property is my new best friend.
 

Users who are viewing this thread

Back
Top Bottom