Mmmk, this is going no where fast. As Gonzalo pointed out, you have given us an abitrary problem "Your code only works when there are 5 items" and then have provided this code:
Private Sub Totalgoods_GotFocus()
Dim Totalgoods
Totalgoods = [Total1]
If Not IsNull([Total2]) Then
Totalgoods = [Total1] + [Total2]
End If
If Not IsNull([Total3]) Then
Totalgoods = [Total1] + [Total2] + [Total3]
End If
If Not IsNull([Total4]) Then
Totalgoods = [Total1] + [Total2] = [Total3] + [Total4]
End If
If Not IsNull([Total5]) Then
Totalgoods = [Total1] + [Total2] + [Total3] = [Total4] + [Total5]
End If
End Sub
And have not really said at which point you are having problems. First up, this code is fundamentally flawed because:
If only your fifth if statement evaluates to true your code would look like this (literally)
Totalgoods = Null + Null + Null + Null + <value>
(Note, you have not stated that if Total3 is not null then Total1 and Total2 will also have a value)
Secondly, you said you were using Access so, I am assuming, this
VB is actually VBA somewhere inside an Access form. Personally, instead of creating 5 different IF statements I would use a query such as:
SELECT Sum(ISNULL([Total1], 0) + ISNULL([Total2], 0) + ISNULL([Total3], 0) + ISNULL([Total4],0) + ISNULL([Total5], 0)) From <table> Where <value> = <value>
That will Sum all the columns for you which, IMHO, is more effecient.
================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
.: Wrox Technical Editor :.
Wrox Books 24 x 7
================================================== =========