Isaac
Lifelong Learner
- Local time
- Today, 10:44
- Joined
- Mar 14, 2017
- Messages
- 11,108
There are various things that could and maybe should be improved about this function, but I'm just doing it fairly quickly to give you the general idea of how to turn something into a VBA function and then use it in a query.
The name should probably be changed. Local variables inside the function should probably be declared and set to the cdbl() version once and for all near the beginning instead of constantly converted throughout. I used Variant in case Nulls come in, although per your message they shouldn't be able to.
You use this in a query like: Expr2: ReturnData([tblEmployees].[Marital Status],[Basic Salary],[Lower],[Exemption credit])
The name should probably be changed. Local variables inside the function should probably be declared and set to the cdbl() version once and for all near the beginning instead of constantly converted throughout. I used Variant in case Nulls come in, although per your message they shouldn't be able to.
You use this in a query like: Expr2: ReturnData([tblEmployees].[Marital Status],[Basic Salary],[Lower],[Exemption credit])
Code:
Function ReturnData(lngMaritalStatus As Variant, dblBasicSalary As Variant, dblLower As Variant, dblExemptionCredit As Variant) As Double
If Len("" & lngMaritalStatus) = 0 Or Len("" & dblBasicSalary) = 0 Or Len("" & dblLower) = 0 Or Len("" & dblExemptionCredit) = 0 Then
ReturnData = 0
Exit Function
End If
If (lngMaritalStatus = "1" And CDbl(dblBasicSalary) > 25727) Then
ReturnData = CDbl(dblBasicSalary) - ((CDbl(dblBasicSalary) - CDbl(dblLower)) * 0.2) - CDbl(dblExemptionCredit)
ElseIf (lngMaritalStatus = "2" And CDbl(dblBasicSalary) > 17780) Then
ReturnData = CDbl(dblBasicSalary) - ((CDbl(dblBasicSalary) - CDbl(dblLower)) * 0.12) - CDbl(dblExemptionCredit)
Else 'all other cases
ReturnData = 0 '
End If
End Function