2016-12-20 26 views
-2

我需要帮助;我已经创建了存储基于以下标准的保险费的表:从一个表访问另一个表时获取特定字段

Table_Policy_Premium 
1. Category: A 
2. Min Age: 0 
3. Max Age: 20 
4. Premium: USD1000 

相同的类别可以具有的年龄其它范围,最小21最多64个,收费在下一字段中。

我有另一张表,即employee_data,哪里有一个溢价字段;我希望根据每位员工的以下标准从TPP表中更新。

如果Table_Policy_Premium - Category匹配与Categoryemployee_data,并且如果年龄在employee_data是0-20之间,从Table_Policy_Premium与适当的溢价更新Employee_Table保费领域。

+0

确定。你有尝试过什么吗?何时/如何进行此更新?什么事件会触发它? –

回答

0

只需在模块中创建公用函数,您将在查询(或表单)中调用该函数,该函数将返回给定类别和年龄的溢价值。

Public Function getPremium(strCat As String, intAge as Integer) As Double 

    'variables 
    Dim strQuery As String 
    Dim rstQuery As dao.Recordset 
    Dim dbs As dao.Database 

    'set database 
    Set dbs = CurrentDb 

    'prepare query 
    strQuery = "SELECT Premium " & _ 
       "FROM Table_Policy_Premium " & _ 
       "WHERE Category = '" & strCat & "' AND " & _ 
       "[Min Age] <= " & intAge & " AND " & _ 
       "[Max Age] >= " & intAge & ";" 


    'open recordset 
    Set rstQuery = dbs.OpenRecordset(strQuery) 

    'check if there is a record 
    If rstQuery.EOF Then 

     'no record - set premium to 0 
     getPremium = 0 
    Else 

     'record found 
     getPremium = rstQuery!Premium 
    End If 

    Set rstQuery = Nothing 
    Set dbs = Nothing 

End Function 

只需在查询中插入这一领域的作为通话功能:getPremium([类别],[年龄])

相关问题