Solved Setting a field to null on Dirty

dullster

Member
Local time
Today, 07:15
Joined
Mar 10, 2025
Messages
43
I'm trying to change a Text Box "OT Hours" to null when i go to a new or next record.

I have tried this on the form and field Expression and it's not working.

Private Sub Form_Dirty(Cancel As Integer)
If Me.OT_Hours Then Me.Undo
End Sub

Private Sub OT_Hours_Dirty(Cancel As Integer)
If Me.Dirty Then Me.Undo
End Sub
 
Dirty only happens when you Dirty the form (make an entry)

try the current event with Me.OT_Hours = Null
 
That worked. Thank you. You guys are amazing when I can't work through stupid errors.
 
Do NOT dirty a record before a user does. So, that means NEVER put code in the Current event to dirty a record. PERIOD. If your validation code is MIA totally or not correct or not in the correct event, you end up giving the user spurious error messages or simply saving bad data.

Why does the field show a value on an empty record? If you are seeing 0 and don't want to, then change the default for the field to null rather than zero. Maybe you already have bad code that is placing a value in that field on new records.
 
Your reply makes no sense. If the field is calculated, you can't set it to anything so the code you think solved your problem should return an error. If it doesn't then the field is NOT calculated. IS the field calculated? If so, you can change the calculation so that if it is not need, it will return null rather than 0.

None of this alters my advice. The code should NEVER dirty a record before the user has started typing into it. Please tell us? Are you seeing a 0? in the field for a new record? Have you opened the table in design view and changed the default value there? Is the problem that you don't understand how to use a numeric field which might be null in a calculation.

That is THREE separate questions. Please do your best to answer all of them.
 
The field is only a number field that i enter overtime hours for the employee to show me how much is owed for overtime. I enter the hours in the field and have another field that tells me how much overtime to pay. It does not fill another field with information. Next payroll, the employee may not have overtime so I want it to 0 when the form is closed.

I am not seeing a 0, the number I entered to calculate the overtime stayed in the field. I want it to go back to blank or 0.
 
Next payroll, the employee may not have overtime so I want it to 0 when the form is closed.
The field, assuming it is bound, will always show the default value when a new record is started. WHAT IS THE DEFAULT VALUE as defined on the table?
As i said at 11:54, that fixed my error.
And I warned you that dirtying a record with your own code BEFORE the user types a single character is seriously poor practice and can lead to many types of errors. You are using a band aid when you should be addressing the problem and the band aid can cause other, worse problems.

You are very new here but we are not mind readers although sometimes it may seem that we are and when we ask you questions, you should answer the questions specifically. They are trying to lead you/us to figuring out what is causing the issue. We cannot see what you are seeing and you didn't post a picture and you didn't directly answer any of my questions. So, if you are happy, simply close your eyes and ignore my advice. Mark the thread as closed and move on.
 

Users who are viewing this thread

Back
Top Bottom