Question : How could I select and sum a table from a range of fields?

I have this table:
ID     Period1   Period2   Period3  Period4  Period5 Period6 Period7 Period8 Period9
12        31            24         35             6            33      24         54             34      63
13        30            14         65             16          33      214       52              4         3
13        20            74         61             46          43      14           2              14       33

Case 1:
I want to select and sum all periods from Period1 to Period4.  And put the quantity on Period5, which has a quantity to be added too.
case 2: Get the sum and spread them in a range of periods.

Answer : How could I select and sum a table from a range of fields?

1.  how are you displaying the data from this table.
2.  How are you "selecting" the fields to sum and the field to put the data into.
3.  Do you want to do this for only one record at a time, or all records? (I assume that the ID value in the third row of your sample data should be 14).

For summing across multiple fields, I use a function I created:

Public Function fnSum(ParamArray ValList() As Variant) As Variant

   Dim intLoop As Integer
   Dim myVal As Variant
   
   myVal = 0
   For intLoop = LBound(ValList) To UBound(ValList)
      If IsNull(ValList(intLoop)) Then
        'do nothing
      ElseIf IsNumeric(ValList(intLoop)) Then
        myVal = myVal + ValList(intLoop)
      End If
   Next
   fnSum = myVal
   
End Function

In a query, you can call this like:

SELECT fnSum([Period1], [Period2], [Period3], [Period4]) as SumOfSomething
FROM yourTable

The bigger problem is that your table structure is flawed.  For a table with this type of data, your structure should look more like:

ID - Autonumber
SomeOtherID - long integer (this would be a foreign key to some other table that describes what the values belong in your current ID field have in common).  It might be a projectID, or something like that
Period_Num - Integer (this is where you would identify the specific period - rather than in a field name)
Period_Val - long integer, this is where you would actually store the value

ID    ProjectID        Period_Num     Period_Val
1          12                      1                      31
2          12                      2                      24
3          12                      3                      35
4          12                      4                      6

Random Solutions  
 
programming4us programming4us