2
votes

I have a bound pop up form with a Clear data button. It runs the Undo command.

The Form_Current event sets this button's enable property to False.

The Form_Dirty event sets this button's enable property to True.

The button's property is always set to False even after I enter data into the form. I think this is because my Form_Load event is populating two fields. One is passed from the main form as an OpenArgs, the other is the unique ID which is calculated based on the OpenArgs value.

Any suggestions as to how to get the Dirty event to activate under these circumstances? If not, then an alternative approach perhaps?

Thanks in advance.

Code below:

Private Sub cmdUndoChanges_Click()

    CustID_temp = Me!CustID
    FacNo_temp = Me!Unique_ID

    DoCmd.RunCommand acCmdUndo

    Me!CustID = CustID_temp
    Me!Unique_ID = FacNo_temp

End Sub

Private Sub Form_Current()
    Me!cmdUndoChanges.Enabled = False
End Sub

Private Sub Form_Dirty(Cancel As Integer)
    Me!cmdUndoChanges.Enabled = True
End Sub

Private Sub Form_Load()
Dim test As Integer

    Me!CustID = OpenArgs

    test = DCount("Unique_ID", "tbl_Table2", "CustID = '" & Me!CustID & "'")
    If IsNull(Me!UNIQUE_No) Then
        test = test + 1
        Me!Unique_ID = CustID & "-" & test
        MsgBox "No Previous Data"
    Else
    ' these will be turned back on when the user selects the
    ' modify data button or add new data button.

        For Each ctl In Me.Controls
        Select Case ctl.ControlType
        Case acTextBox, acComboBox, acOptionGroup, acCheckBox
            ctl.Locked = True
            ctl.BackColor = 15066597
            Box40.BackColor = 15066597
        End Select
        Next ctl
    End If

MsgBox Me!Unique_ID
End Sub
2
Put a breakpoint in Form_Dirty to confirm the event does not fire at all. And if it never fires, it may be because Access occasionally "loses track" of event procedures. If you go back to the form's property sheet and click the 3 dots on the far right side of the On Dirty property box, you can remind Access about the existing event procedure.HansUp

2 Answers

0
votes

Have you tried Form_BeforeInsert() instead of Form_Dirty()?

0
votes

I found another case where the Form_Dirty event never fires, the hard way. I thought I'd add it here since this is the top search result for the "not firing" question.

If your Form_Load function does anything to change the value of an existing record, the form will be set to dirty immediately, apparently before the logic that fires the event begins to run. Because of that there will never be an event if other data on the form is changed.

We had a "smart" Save button that was disabled until Form_Dirty fired. After I added a fixup to Form_Load for old records with a junk field, editing those records no longer ever enabled the Save button.