One of the easiest ways is to use SQL to query the table fo only the values that you want. You can use the math functions in SQL to get the AVG easily and just show the result in whatever field that you want.
Sample code for a table query:
Dim strSQL As String
Dim dbs As Database
Dim rstemp As Recordset
'set the value of home or work adrress to mail to
If Me.MemberAddressHome.Value = 1 Then
strAddrType = "S"
ElseIf Me.MemberAddressHome.Value = 2 Then
strAddrType = "H"
Else
MsgBox "Please check either Home or Work Address for mailing"
Me.frmeChooseAddress.SetFocus
End If
'build the SQL query for either SIN or EID
strSQL = "SELECT DC_DBA_CME_PERSON.FIRST_NAME, DC_DBA_CME_PERSON.LAST_NAME, DC_DBA_CME_PERSON.SSN,"
strSQL = strSQL & "DC_DBA_CME_PERSON_ADDR.ADDR_LINE1, DC_DBA_CME_PERSON_ADDR.ADDR_LINE2,"
strSQL = strSQL & "DC_DBA_CME_PERSON_ADDR.ADDR_LINE3 , DC_DBA_CME_PERSON_ADDR.ADDR_LINE4, DC_DBA_CME_PERSON_ADDR.CITY, "
strSQL = strSQL & "DC_DBA_CME_PERSON_ADDR.STATE_CD, DC_DBA_CME_PERSON_ADDR.POSTAL_CD, DC_DBA_CME_PERSON_ADDR.COUNTRY_CD,DC_DBA_CME_ASSIGNED_PID.BASE_ID FROM "
strSQL = strSQL & " (DC_DBA_CME_ASSIGNED_PID INNER JOIN DC_DBA_CME_PERSON ON DC_DBA_CME_ASSIGNED_PID.PERSON_ID = DC_DBA_CME_PERSON.PERSON_ID) "
strSQL = strSQL & " INNER JOIN DC_DBA_CME_PERSON_ADDR ON DC_DBA_CME_PERSON.PERSON_ID = DC_DBA_CME_PERSON_ADDR.PERSON_ID WHERE "
'check for data to be filled in
If (Nz(Me.EmployeeNo.Value)) = "" And (Nz(Me.SIN.Value)) = "" Then
MsgBox "Please enter an Employee ID or a SIN into the approriate field"
DoCmd.CancelEvent
ElseIf Me.EmployeeNo.Value <> "" And (Nz(Me.SIN.Value)) = "" Then
strSQL = strSQL & " (DC_DBA_CME_ASSIGNED_PID.BASE_ID) Like '" & Me.EmployeeNo.Value & "'"
ElseIf Me.SIN.Value <> "" And (Nz(Me.EmployeeNo.Value)) = "" Then
strSQL = strSQL & " (DC_DBA_CME_PERSON.SSN) Like '" & Me.SIN.Value & "'"
ElseIf Me.EmployeeNo.Value <> "" And Me.SIN.Value <> "" Then
strSQL = strSQL & " (DC_DBA_CME_PERSON.SSN) Like '" & Me.SIN.Value & "'"
End If
'set the address type for the query
strSQL = strSQL & " and (DC_DBA_CME_PERSON_ADDR.ADDR_TYPE) Like '" & strAddrType & "'"
'MsgBox strSQL
Set dbs = CurrentDb()
'run the query
Set rstemp = dbs.OpenRecordset(strSQL)
'assign the values
Dim strName As String
Dim strAddress As String
Dim strSIN As String
'check to see if there is a record returned and cancel the event or fill in the values
If rstemp.RecordCount = 0 Then
MsgBox "No address found for this person. Change address type"
rstemp.Close
Set rstemp = Nothing
Set dbs = Nothing
If Me.EmployeeNo.Value <> "" Then
Me.EmployeeNo.SetFocus
Else
Me.SIN.SetFocus
End If
Else
strName = rstemp(1).Value & ", " & rstemp(0).Value
If rstemp(3).Value <> " " Then
strAddress = rstemp(3).Value & vbNewLine
End If
If rstemp(4).Value <> " " Then
strAddress = strAddress & rstemp(4).Value
End If
If rstemp(5).Value <> " " Then
strAddress = strAddress & ", " & rstemp(5).Value & vbNewLine
End If
If rstemp(6).Value <> " " Then
strAddress = strAddress & rstemp(6).Value
End If
If rstemp(7).Value <> " " Then
strAddress = strAddress & ", " & rstemp(7).Value & vbNewLine
End If
strAddress = strAddress & rstemp(8).Value & vbNewLine
strAddress = strAddress & rstemp(10).Value & vbNewLine
strAddress = strAddress & rstemp(9).Value
strSIN = rstemp(2).Value
Me.EmployeeNo.Value = rstemp(11).Value
Me.EmployeeName.Value = strName
Me.MemberAddress.Value = strAddress
Me.SIN.Value = strSIN
rstemp.Close
End If
Set rstemp = Nothing
Set dbs = Nothing
hth
Bastien
There are many ways to skin this cat,
but it still tastes like chicken