L
Leslie Coover
Given the following table called tblAlpha:
ID = A,A,B,B,C
Num = 1,2,4,3,5
Where the Data type for ID is text and the Data type for Num is Number;
Field size = Double
qryRegular is based on tblAlpha, first column contains ID, next column
contains Num.
In order to do other calculations I want to place the sum of all As in the
third column adjacent to each "A" ID and sum of all Bs adjacent to each "B"
ID etc.
I create a Totals Query:
SELECT [tblAlpha].[ID], Sum([tblAlpha].[Num]) As SumOfNum FROM tblAlpha
GROUP BY [tblAlpha].[ID];
But how can I put the results into qryRegular?
Here is my attempt:
3rd column qryRegular Field = SumNum_:SumNum([ID])
VBA (in module)
Function SumNum(ID As String)
Dim db As Database, rec As Recordset
Dim strSQL As String
Dim Value1 As Double
strSQL = "SELECT * FROM tblAlpha {what goes here??}
Do Until EOF
{what goes here ?}
Loop
SumNum = Value1
rec.Close
End Function
Perhaps there is an easier way than to use VBA, but I have to do some other
stuff, including finding yield to maturity on bonds given price and coupon,
so I thought if I get a general pattern of the VBA code for this simple
routine it might help later on.
Any help appreciated.
ID = A,A,B,B,C
Num = 1,2,4,3,5
Where the Data type for ID is text and the Data type for Num is Number;
Field size = Double
qryRegular is based on tblAlpha, first column contains ID, next column
contains Num.
In order to do other calculations I want to place the sum of all As in the
third column adjacent to each "A" ID and sum of all Bs adjacent to each "B"
ID etc.
I create a Totals Query:
SELECT [tblAlpha].[ID], Sum([tblAlpha].[Num]) As SumOfNum FROM tblAlpha
GROUP BY [tblAlpha].[ID];
But how can I put the results into qryRegular?
Here is my attempt:
3rd column qryRegular Field = SumNum_:SumNum([ID])
VBA (in module)
Function SumNum(ID As String)
Dim db As Database, rec As Recordset
Dim strSQL As String
Dim Value1 As Double
strSQL = "SELECT * FROM tblAlpha {what goes here??}
Do Until EOF
{what goes here ?}
Loop
SumNum = Value1
rec.Close
End Function
Perhaps there is an easier way than to use VBA, but I have to do some other
stuff, including finding yield to maturity on bonds given price and coupon,
so I thought if I get a general pattern of the VBA code for this simple
routine it might help later on.
Any help appreciated.