Here you go:
- Public Sub SetTax()
-
-
'Find taxrate
-
Dim dbTaxRate As Double
-
Dim strTaxState As String
-
If Me!StateIn = "TN" Or Me!ShiptoState = "TN" Then
-
strTaxState = "TN"
-
ElseIf Me!StateIn = "FL" Or Me!ShiptoState = "FL" Then
-
strTaxState = "FL"
-
ElseIf Me!StateIn = "NC" Or Me!ShiptoState = "NC" Then
-
strTaxState = "NC"
-
Else
-
strTaxState = ""
-
End If
-
-
-
'Lookup taxrate from table
-
dbTaxRate = Nz(DLookup("taxrate", "tax", "taxstate='" & strTaxState & "'"), 0)
-
-
'Set Tax
-
Me!Tax = (Me!PartTTL + Postage + LaborTTL - Discount) * dbTaxRate
-
-
-
End Sub
Let me just take the chance to give some advice on variable/field/table naming.
When your looking at the domain lookup function (DLookup), it can be hard to "guess/remember" what its doing with those field/table names. Try to imagine instead naming the table of taxrates "tbl_TaxRates", and the field within the table:
tx_State, Textfield, Primary Key,
db_TaxRate, Number-Double.
The tx indicating its a textfield, the db indicating its a double field.
Now try looking at the same dlookup function call:
- dbTaxRate = Nz(DLookup("db_TaxRate", "tbl_TaxRates", "tx_State='" & strTaxState & "'"), 0)
Its immediatly obvous that we are getting a taxrate, represented by a double, from a table "tbl_TaxRates.
You might ask why I have a underscore in one of the db_TaxRate / dbTaxRate, well thats because if its a Fieldname in a table, I use the underscore db_TaxRate, and if its a variable in my code I go without the underscore dbTaxRate.
Why do I call its tx_State in 1 place, and strTaxState in another place? Again because the underscore tells me its a field in a table, the tx tells me its a field of type Text. A Text field can max hold 255 charecters, possibly less depending on what you have specified in the table design. the strTaxState tells me its a string, and a string can hold way more then 255 chars (Im actually not sure what the limit is on a string, I have not encountered any limit yet).
But when I look at code saying Me.tx_State=strTaxState for instance, I would immediatly know that I should be sure that the textfield can hold the size of the variable in strTaxState.