N
nacholibre
Hello,
I've received some help here and used it to try and modify this code to
calculate median values in my table.
I think I'm have a syntax problem.
I'm hoping someone can point to where I've gone wrong..
Thank you!
Here's the code:
Function MedianF(pTable As String, pfield As String, region As String,
informa_fico As String, informa_ltv As String, informa_term As String) As
Single
Dim rs As Recordset
Dim strSQL As String
Dim n As Integer
Dim sglHold As Single
strSQL = "SELECT " & pfield & " from " & pTable & " WHERE " & pfield & ">0
AND REGION = " & region & " AND INFORMA_FICO = " & informa_fico & " AND
INFORMA_LTV = " & informa_ltv & " AND INFORMA_TERM = " & informa_term &
"Order by " & pfield & ";"
Set rs = CurrentDb.OpenRecordset(strSQL)
rs.MoveLast
n = rs.RecordCount
rs.Move -Int(n / 2)
If n Mod 2 = 1 Then 'odd number of elements
MedianF = rs(pfield)
Else 'even number of elements
sglHold = rs(pfield)
rs.MoveNext
sglHold = sglHold + rs(pfield)
MedianF = sglHold / 2
End If
rs.Close
End Function
I've received some help here and used it to try and modify this code to
calculate median values in my table.
I think I'm have a syntax problem.
I'm hoping someone can point to where I've gone wrong..
Thank you!
Here's the code:
Function MedianF(pTable As String, pfield As String, region As String,
informa_fico As String, informa_ltv As String, informa_term As String) As
Single
Dim rs As Recordset
Dim strSQL As String
Dim n As Integer
Dim sglHold As Single
strSQL = "SELECT " & pfield & " from " & pTable & " WHERE " & pfield & ">0
AND REGION = " & region & " AND INFORMA_FICO = " & informa_fico & " AND
INFORMA_LTV = " & informa_ltv & " AND INFORMA_TERM = " & informa_term &
"Order by " & pfield & ";"
Set rs = CurrentDb.OpenRecordset(strSQL)
rs.MoveLast
n = rs.RecordCount
rs.Move -Int(n / 2)
If n Mod 2 = 1 Then 'odd number of elements
MedianF = rs(pfield)
Else 'even number of elements
sglHold = rs(pfield)
rs.MoveNext
sglHold = sglHold + rs(pfield)
MedianF = sglHold / 2
End If
rs.Close
End Function