If you pasted your code instead of pictures of it, we could copy what seems to be the problem rather than having to type that part (or Heaven forbid, most or all of it).
Your 3rd line saves the record, so whatever validation you want to do after that is pointless. Also, only events that have a Cancel argument can be cancelled as you attempt to do. A click event is not one of them. There is a CancelEvent method, but I've never used it and don't know if it just applies to macros or not. You could simplify your code a lot, either by putting multiple validations on one line (e.g. If this OR that OR something OR... Then) or iterating over the form controls to see which required ones are blank and presenting all of them in a message in one go.
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.
sorry this is the code sir
Private Sub Command72_Click()
If IsNull(Me.receipt.Value) Then
MsgBox "OR Field is Empty", vbOKOnly
Cancel = True
Me.Undo
If IsNull(Me.amountpay.Value) Then
MsgBox "Amount Paid is Empty", vbOKOnly
Cancel = True
Me.Undo
If IsNull(Me.datepay.Value) Then
MsgBox "Date Paid is Empty", vbOKOnly
Cancel = True
Me.Undo
Else
DoCmd.RunCommand acCmdSaveRecord
DoCmd.OpenReport "Receipt", acViewPreview, , "[BillingID] = " & Me.BILLINGID, , acHidden
DoCmd.SelectObject acReport, "Receipt"
DoCmd.PrintOut acSelection
MsgBox "Record Updated", vbOKOnly
Me.lstItems.Requery
Me.Form.Requery = ""
End If
End If
End If
End Sub
OK, but that's not the same code now. If that is your new code after the suggestions, does it still not work as you wish?
Perhaps just as important, are the fields being checked on one form (e.g. subform) and the button is on another form (e.g. main form)? If so, that could be another problem. For example, if you do anything to update a subform when you move off of it to click a button, the subform record is already saved.
Unfortunately, I neglected to ask you to post code within code tags (my fault). Please do so next time, it makes it much easier to read as I think you'll see below.
Thanks.
Regardless, something like this may work
The logic here is that if testing one control at a time and any one of them fails the test, simply exit the sub and then there's nothing to Cancel. If all is OK, code moves on to the part you want to have happen. Not that it really matters, but .Value is the default property for textboxes, so you don't need to use it. I removed it to show the difference, but put it back if you prefer.Code:Private Sub Command72_Click() If IsNull(Me.receipt) Then MsgBox "OR Field is Empty", vbOKOnly me.receipt.setfocus exit sub end if If IsNull(Me.amountpay) Then MsgBox "Amount Paid is Empty", vbOKOnly me.amountpay.setfocus exit sub end if If IsNull(Me.datepay) Then MsgBox "Date Paid is Empty", vbOKOnly me.datepay.setfocus exit sub end if DoCmd.RunCommand acCmdSaveRecord DoCmd.OpenReport "Receipt", acViewPreview, , "[BillingID] = " & Me.BILLINGID, , acHidden DoCmd.SelectObject acReport, "Receipt" DoCmd.PrintOut acSelection MsgBox "Record Updated", vbOKOnly Me.lstItems.Requery 'may not be required if you're requerying form anyway 'Me.Form.Requery = "" 'you cannot make anything equal the Requery method, plus Me IS the form Me.Requery End Sub
Hope the code helps.
The more we hear silence, the more we begin to think about our value in this universe.
Paraphrase of Professor Brian Cox.
Tried your code sir but, still it saves automatically. even if I did'nt fill up the date paid and the or#.
It saves automatically when i click anywhere in the form, even when i would tried to fill the textbox of the OR#
It appears that you have a situation where the form is being updated when you get to the button but before actually clicking on it. That could be other code such as a Lost Focus event, or the button code is on one form and the problem controls are on another. I think there are too many possibilities but the most likely is that the controls are on a datasheet subform and it's getting updated when you leave it to click. If that's the case, validation would have to move to the Before Update event of that subform. Hard to tell only by looking at your picture. If you want to post a compacted and zipped copy of your db that would help. You can remove sensitive info from the posted copy.
thanks sir, i've just discovered that there were code left that i've tried and now it's works properly as I would like to do it
Thank you sir @Micron.
Glad that I could help.
You dont have to call me sir - I get called lots of things but not that so much.
What they pay you enter in the Tendered textbox. You would then have to tab out or click somewhere else (to use the after update event for tendered) or have a calculate button. In Change textbox, you would have =Tendered - Total as the control's record source, assuming the amount tendered would always be equal to or greater than the amount owing. If you think it is possible that this could not be the case, such as when taking partial payments, you'd need to handle that.
In doing this, the Change textbox cannot be bound to any table field because it would be a calculated control. If you must store the change amount, you'd need to do this with code.
Last edited by Micron; 10-17-2018 at 09:41 AM. Reason: clarification
thanks sir, I will try this later, I would like to just when the user click the tab it calculates automatically.
but how will the amount paid be posted in the amount paid?does this automatically be save?
then use the control's AfterUpdate event.I would like to just when the user click the tab it calculates automatically.
You have bound that field (Amount Tendered in picture?) to the table or select query that the form is bound to, yes? Then moving off of that control will update the record field it's bound to. That doesn't necessarily mean you will update the record. You still have to know that this part of your form works as it should. I'm thinking that maybe you should research bound forms in Access to gain more understanding of how this works.how will the amount paid be posted in the amount paid?
yes my Tendered textbox is bound, and my Change textbox is bound just to calculate Tendered-Balance.
My question is when the User is saving the form, I want my Amount Paid to post just the exact Balance amount of the bill,so it will be hidden in my query that the criteria is >0.
it should Look like this:
(Before the payment it will show on my form& query.)
Amount Due Penalty DatePaid AmountPaid OR# Amount_Tendered Change Balance(Criteria: >0)
150 7.50 0 157.50
After payment
This will not show in my forms listbox coz it returns 0.
Amount Due Penalty DatePaid AmountPaid OR# Amount_Tendered Change Balance(Criteria: >0)
150 7.50 10/18/2018 157.50 20180010 200 0
Hope you could help me sir/maam, or you can give any suggestion?
If you mean post the balance to a table, it is generally considered bad practice to store calculations such as balance owing. You can easily end up with balance errors because a debit or credit fails to be applied (the balance fails to recalculate). It is best to allow queries/forms/reports to calculate the balance based on the sum of purchases minus the sum of payments.I want my Amount Paid to post just the exact Balance
If you insist on saving the balance to a table, and the balance is calculated on the form by using a calculated control, then as I said, you'd need code to write the calculation to the table because the calculated control cannot be bound. Now you need another control to bind to the table field just to retrieve and show the balance field value that the form can show without that field (as already mentioned). It just makes no sense.
Will suggest that when posting columns of information in your post that you click Go Advanced and insert a table. Not only does your data not line up with your column headings and is therefore difficult to relate to, you're showing 157.50 as the date paid in the first example.