jsc3489 said:
Ok. I figured out how to put (ex. =[EQ cost]*[EQ OH %]) expressions
in my form boxes to perform my calcs. My problem now is my code for
two of my boxes. I have created a table (of sorts) in code and I'm
not quite sure how to implement. My code was taken from my Excel
sheet and converted (somewhat) to work in Access. Here is the code:
Option Compare Database
---------------------------------------------------------------------- --------
Private Sub EQ_Commission___BeforeUpdate(Cancel As Integer)
If Form_Sales.EQ_Profit_%.Value >= 0.445 Then
Form_Sales.EQ_Commission_%.Value = 0.21
ElseIf Form_Sales.EQ_Profit_%.Value >= 0.345 Then
Form_Sales.EQ_Commission_%.Value = 0.2
ElseIf Form_Sales.EQ_Profit_%.Value >= 0.295 Then
Form_Sales.EQ_Commission_%.Value = 0.19
ElseIf Form_Sales.EQ_Profit_%.Value >= 0.245 Then
Form_Sales.EQ_Commission_%.Value = 0.18
ElseIf Form_Sales.EQ_Profit_%.Value >= 0.195 Then
Form_Sales.EQ_Commission_%.Value = 0.17
ElseIf Form_Sales.EQ_Profit_%.Value >= 0.145 Then
Form_Sales.EQ_Commission_%.Value = 0.15
ElseIf Form_Sales.EQ_Profit_%.Value >= 0.095 Then
Form_Sales.EQ_Commission_%.Value = 0.13
ElseIf Form_Sales.EQ_Profit_%.Value >= 0.045 Then
Form_Sales.EQ_Commission_%.Value = 0.1
Else
Form_Sales.EQ_Commission_%.Value = 0.05
End If
End Sub
---------------------------------------------------------------------- ---------
Private Sub Labor_Commission___BeforeUpdate(Cancel As Integer)
If Form_Sales.Labor_OH_%.Value >= 0.945 Then
Form_Sales.Labor_Commission_%.Value = 0.21
ElseIf Form_Sales.Labor_OH_%.Value >= 0.825 Then
Form_Sales.Labor_Commission_%.Value = 0.2
ElseIf Form_Sales.Labor_OH_%.Value >= 0.695 Then
Form_Sales.Labor_Commission_%.Value = 0.19
ElseIf Form_Sales.Labor_OH_%.Value >= 0.575 Then
Form_Sales.Labor_Commission_%.Value = 0.18
ElseIf Form_Sales.Labor_OH_%.Value >= 0.445 Then
Form_Sales.Labor_Commission_%.Value = 0.17
ElseIf Form_Sales.Labor_OH_%.Value >= 0.325 Then
Form_Sales.Labor_Commission_%.Value = 0.15
ElseIf Form_Sales.Labor_OH_%.Value >= 0.195 Then
Form_Sales.Labor_Commission_%.Value = 0.13
ElseIf Form_Sales.Labor_OH_%.Value >= 0.075 Then
Form_Sales.Labor_Commission_%.Value = 0.1
Else
Form_Sales.Labor_Commission_%.Value = 0.05
End If
End Sub
---------------------------------------------------------------------- ---
In some of the form boxes my expressions work, in other I get #Name?.
I think it's because of my code is not being utilized properly or
it's bad code.
There are problems with the code, but whether that's why you're getting
the #Name? error is not clear to me. That generally means that an
expression contains a name -- the name of a control or function,
usually -- that Access can't resolve. One common cause of this is a
circular reference: a control contains an expression that refers to
itself, or to another control that contains an expression that refers to
the first control, etc. Or, of course, you could have simply misspelled
the name of the control, or, since your control names contain invalid
characters (spaces, the percent sign), you may have failed to quote it
properly with square brackets ([]).
Looking at your code above, I'm not sure what you're trying to do, but
I'm pretty sure you'd do better to use the AfterUpdate event of each
control, rather than the BeforeUpdate event -- the BeforeUpdate event is
mainly for validation of the value entered by the user. Are these
controls whose values you're calculating, [EQ Commission %] and [Labor
Commission %], going to be manually updatable after you've calculated
their initial values here? If so, they must not be calculated controls
(that is, their ControlSource properties must not begin with an equals
sign), but other than that your approach is correct. Your code could
use some adjusting, though:
'----- start of revised code ------
Option Compare Database
Option Explicit
Private Sub EQ_Commission___AfterUpdate()
Select Case Me![EQ Profit %].Value
Case Is >= 0.445
Me![EQ Commission %] = 0.21
Case Is >= 0.345
Me![EQ Commission %] = 0.2
Case Is >= 0.295
Me![EQ Commission %] = 0.19
Case Is >= 0.245
Me![EQ Commission %] = 0.18
Case Is >= 0.195
Me![EQ Commission %] = 0.17
Case Is >= 0.145
Me![EQ Commission %] = 0.15
Case Is >= 0.095
Me![EQ Commission %] = 0.13
Case Is >= 0.045
Me![EQ Commission %] = 0.1
Case Else
Me![EQ Commission %] = 0.05
End Select
End Sub
' ... and similar for Labor_Commission___AfterUpdate()
'----- end of revised code -----
Note, by the way, the line "Option Explicit" near the top of the module.
You should have that, so that the VB Editor can tell if you've
misspelled a variable or control name. You can tell the VB Editor to
add that for all new modules you create, by checking the "Require
variable declaration" option in the Options dialog of the VB Editor.
--
Dirk Goldgar, MS Access MVP
www.datagnostics.com
(please reply to the newsgroup)