You need to call the function from the FORM's BeforeUpdate event AND you need to cancel the event if the data is not valid. As I said, I would have done this differently. Uncle should be able to fix the problem now that he knows there is one.
Pat, in my demo post 36 I have call the function in the after update event and entered the following in the FORMS before the update event , should this suffice or am I missing something ? As a user and not a professional I am keen to improve my best practices
Code:
If (IsNull(Me.Gasprice) Or IsNull(Me.amountGas) Or (IsNull(Me.ValueofGas))) Then
MsgBox " data is required for all fields "
If MsgBox("Do you wish to save this incomplete record", vbYesNo + vbQuestion) = vbYes Then
Cancel = False
Else
MsgBox " Record not saved re-enter the correct details"
Cancel = True
End If
You MUST use the BeforeUpdate event because you cannot cancel the AfterUpdate event. Last time I looked, the code is not written to be cancelled. I've mentioned this several times. I would have done this differently. I have tried to stay out but I wanted to make sure you weren't accepting a solution that didn't actually work.
You can continue to run the code as it is from what ever event you prefer. Then in the FORM's BeforeUpdate event, check all three fields.
Code:
If Me.Miles & "" = "" OR Me.Price & "" = "" OR Me.Total & "" = "" Then
Msgbox "Please enter at least two of the three gas fields", vbOKOnly
Cancel = True
Me.Miles.SetFocus
Exit Sub
End If
The values may not be null. Concatenating a ZLS caters to both null and ZLS.
Pat, thank you for your response, I fully except Concatenating a ZLS caters to both null and ZLS and will take it on board , however not withstanding that, my code gave the user the option to ignore any changes or save the record , yours code does not give him that option. I requested that you look at my Post 36 , however it does not appear to have been viewed . It looks a though the originator of the post is opting for Uncle's solution so I will move on.
Your solution works fine now.
I am sorry to make such simple mistake and couldn't catch it.
Now I am wondering about error handling. Please explain the best approach and how to implement it. I have never done this part.
Thank you
Happy Holidays and Merry Christmas
Wish you all the best in new year 2021, healthy and safe.
Pat, thank you for your clarification, I misunderstood the posters needs and tried to give him an out in the Forms before update event , plus the fact that if the function is called from the records Before update event, which you have been advocating from day one , no incorrect record should be saved ..
In my Post 36 demo, I entered some error handling code for the function and will leave it to others who are better qualified than me,
As an eighty year old my effort was liken to the "blind leading the blind", but it was simulating plus a learning curve.
I hope you and the people you love have a safe Christmas and a happy New Year
Ypma