Calculate Margin on every change (1 Viewer)

LeeHutchins

New member
Local time
Today, 20:38
Joined
Nov 3, 2020
Messages
13
Hi,

I want my margin to calculate with every change of PAPrice usually would use me.FIELDNAME.text but this is a numeric value and doesn't seem to work that way.

this is what I have so far, but might be wayyyyyyy off.

Code:
Private Sub PAPrice_Change()
   If Len(Me.PAPrice) > 0 Then
         Me.Margin = (Me.PAPrice - Me.StandardCost) / Me.StandardCost
         Me.Margin.Enabled = False
   End If
End Sub

with thanks,

Lee
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:38
Joined
Oct 29, 2018
Messages
21,473
Hi. I don't understand your comment about using the Change event on a numeric field. I just tried it, and it was working fine. What was happening when you tried it?
 

LeeHutchins

New member
Local time
Today, 20:38
Joined
Nov 3, 2020
Messages
13
Hi. I don't understand your comment about using the Change event on a numeric field. I just tried it, and it was working fine. What was happening when you tried it?
The margin field is still blank with no change?

What I mean is that on every new number entered eg for 1.75 it updates on the '1', then the '1.' then the '1.7', then the '1.75',
 

theDBguy

I’m here to help
Staff member
Local time
Today, 12:38
Joined
Oct 29, 2018
Messages
21,473
The margin field is still blank with no change?

What I mean is that on every new number entered eg for 1.75 it updates on the '1', then the '1.' then the '1.7', then the '1.75',
This seemed to work for me...
Code:
CDbl(Me.ControlName.Text)
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:38
Joined
May 7, 2009
Messages
19,242
are you talking abount Report here, since you mentioned Margin?
instead of changing the margin, change the Font size(smaller or normal) of the Textbox
on the Format Event of the report.
 

missinglinq

AWF VIP
Local time
Today, 15:38
Joined
Jun 20, 2003
Messages
6,423
DBguy is right, of course!

PAPrice

is shorthand, if you will, for

PAPrice.Value

and since this is being done without you actually leaving the PAPrice Control...it has no Value, as yet...so you have to use the Text Property:

PAPrice.Text

Anytime you refer to PAPrice.

You probably also need to take into account the possibility that StandardCost could be Null (empty) when your end users enter the data in PAPrice.

Linq ;0)>
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Tomorrow, 03:38
Joined
May 7, 2009
Messages
19,242
so it is not a Report:

Code:
Private Sub PAPrice_Change()
   If Len(Me.PAPrice.Text) > 0 Then
         Me.Margin = Val(Me.PAPrice.Text) - Nz(Me.StandardCost, 0) / Nz(Me.StandardCost, 1)
         Me.Margin.Enabled = False
   End If
End Sub
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 15:38
Joined
Feb 19, 2002
Messages
43,274
The change event runs once for EACH character typed into the control. Since you are doing arithmetic on the value, this makes no sense at all. Do the calculation in the PAPrice's AfterUpdate event so it happens only ONCE when the data entry is complete.
 

Users who are viewing this thread

Top Bottom