Move code from button to field instead (1 Viewer)

stoicy

Registered User.
Local time
Today, 10:22
Joined
Feb 16, 2019
Messages
40
Hello all

I have a Form named frnOrder and that form contain a button which has an event code on click.
I need to delete that button and put its code in the subform "frmOrderDetails" and to let that code take action when the field named "Total" get updated.
 

Attachments

  • Test99.accdb
    916 KB · Views: 39

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:22
Joined
Feb 28, 2001
Messages
27,001
So move the code to a field_Change event. I don't believe those fire if all you do is tab through the field.
 

stoicy

Registered User.
Local time
Today, 10:22
Joined
Feb 16, 2019
Messages
40
So move the code to a field_Change event. I don't believe those fire if all you do is tab through the field.
Thank you man

I did on change event but nothing happened!!

Sent from my SM-N900V using Tapatalk
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 03:22
Joined
Aug 30, 2003
Messages
36,118
I can't look at the sample right now, but if Total is a calculated field or control its update events don't fire when the value changes. You'd need code in the after update events of the controls involved in the calculation.
 

stoicy

Registered User.
Local time
Today, 10:22
Joined
Feb 16, 2019
Messages
40
I can't look at the sample right now, but if Total is a calculated field or control its update events don't fire when the value changes. You'd need code in the after update events of the controls involved in the calculation.
Thank you for replay

The Total field is a calculated field and I try to put that code on it after update but also nothing change, there is something wrong!

Sent from my SM-N900V using Tapatalk
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 03:22
Joined
Aug 30, 2003
Messages
36,118
Read my post again. if you have A+B=C, your code needs to be in A and B, not C.
 

stoicy

Registered User.
Local time
Today, 10:22
Joined
Feb 16, 2019
Messages
40
The total after event code which in the subform should update multi field in the main form when its value changed after auto calculation.
If you see the database will understand what I mean
Appreacted

Sent from my SM-N900V using Tapatalk
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 03:22
Joined
Aug 30, 2003
Messages
36,118
I understand. The events for the total field do not fire when the value changes. You can't do it there. You have to use the after update events of [RealReQu] and [UnitPrice], the fields involved in the calculation.
 

stoicy

Registered User.
Local time
Today, 10:22
Joined
Feb 16, 2019
Messages
40
I understand. The events for the total field do not fire when the value changes. You can't do it there. You have to use the after update events of [RealReQu] and [UnitPrice], the fields involved in the calculation.
OK I put it in [Unit Price]
But it didnot update the first prodect record line until I record another purchased line.
It means it update one purchased records of two saved record and the total is not correct because it calculated one record only!!

Sent from my SM-N900V using Tapatalk
 

pbaldy

Wino Moderator
Staff member
Local time
Today, 03:22
Joined
Aug 30, 2003
Messages
36,118
Are you copying the sum textbox in the subform footer? I'd probably use the after update event of the subform, which won't fire until the record is saved.
 

AccessBlaster

Registered User.
Local time
Today, 03:22
Joined
May 22, 2010
Messages
5,828
Here is the OP's code on click event from post#1
This will terminate after executing line one.

Code:
Private Sub Command42_Click()
[Forms]![frmOrder]![HeaderTotal] = [Forms]![frmOrder]![frmOrderDetails]![Text22]

Exit Sub


[Forms]![frmOrder]![InvoiceNetTotal] = [Forms]![frmOrder]![HeaderTotal] - [Forms]![frmOrder]![Discount]
Exit Sub


DoCmd.Save acForm, "frmOrder"
End Sub
 

June7

AWF VIP
Local time
Today, 02:22
Joined
Mar 9, 2014
Messages
5,425
Remove the Exit Sub lines.

Saving calculated data is usually unnecessary and can be dangerous. Saved results can get 'out of sync' with raw data.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 05:22
Joined
Feb 28, 2001
Messages
27,001
The Total field is a calculated field and I try to put that code on it after update but also nothing change, there is something wrong!

Let's start with this fact: You cannot update a calculated field; you must update the factors contributing to its calculation. An unbound field can be altered. A bound field can be altered (and will update the underlying dataset if the change is not cancelled). A computed field is neither of those things.
 

Cronk

Registered User.
Local time
Today, 21:22
Joined
Jul 4, 2013
Messages
2,770
The OP has a sub form of Order details. The Total field for each order item is a calculated field. The footer of the sub form has a text box set to equal the sum of all the line Totals. (The sub form is set to DataSheet so this footer control is not visible in normal form view)

The OP has a button in the main Order form which copies the total in the footer to the Total text box in the Main order form which is bound to the Orders table. The requirement is to replace the button with code in the sub form.

The easiest way is to put the following in the sub form's AfterUpdate event but as Paul has noted, the event won't be triggered until after the record is saved, say by moving off the order detail line.
Code:
Me.Parent!HeaderTotal = Me.Text22
 

stoicy

Registered User.
Local time
Today, 10:22
Joined
Feb 16, 2019
Messages
40
Are you copying the sum textbox in the subform footer? I'd probably use the after update event of the subform, which won't fire until the record is saved.
Thank you so much this slove it and no need for that button anymore

I really appreacted your help

Sent from my SM-N900V using Tapatalk
 

stoicy

Registered User.
Local time
Today, 10:22
Joined
Feb 16, 2019
Messages
40
The OP has a sub form of Order details. The Total field for each order item is a calculated field. The footer of the sub form has a text box set to equal the sum of all the line Totals. (The sub form is set to DataSheet so this footer control is not visible in normal form view)

The OP has a button in the main Order form which copies the total in the footer to the Total text box in the Main order form which is bound to the Orders table. The requirement is to replace the button with code in the sub form.

The easiest way is to put the following in the sub form's AfterUpdate event but as Paul has noted, the event won't be triggered until after the record is saved, say by moving off the order detail line.
Code:
Me.Parent!HeaderTotal = Me.Text22
Thank you Cronk

You are right and I got the code you put

Thank you all

Sent from my SM-N900V using Tapatalk
 

Users who are viewing this thread

Top Bottom