在Access查询中调用VBA函数

时间:2014-01-05 08:31:59

标签: sql ms-access-2013

我正在尝试将8个不同查询的结果组合回一个查询中。要使用的所有查询都是查询查询的查询。 8个系列的4个查询将球员分开,取决于他们打了多少轮高尔夫球。每个系列中的最后一个查询计算每个玩家的确切差点。

我正在使用的代码可能无法实现我想要做的事情。

功能代码如下:

Function EHC(PlayerID As Long) As Long

    Dim queT20 As Recordset
    Dim que3to6 As Recordset
    Dim que7or8 As Recordset
    Dim que9or10 As Recordset
    Dim que11or12 As Recordset
    Dim que13or14 As Recordset
    Dim que15or16 As Recordset
    Dim que17or18 As Recordset
    Dim que19or20 As Recordset

    Set queT20 = CurrentDb.OpenRecordset("Top20Count")
    queT20.FindFirst ("PlayerID =" & PlayerID)
    If queT20![PlayerID] >= 19 Then
        Set que19or20 = CurrentDb.OpenRecordset("P_19or20ExactHC")
        que19or20.FindFirst ("PlayerID =" & PlayerID)
        ExactHC = que19or20.Exact_HC
        que19or20.Close
        Set que19or20 = Nothing
    ElseIf queT20![PlayerID] >= 17 Then
        Set que17or18 = CurrentDb.OpenRecordset("P_17or18ExactHC")
        que17or18.FindFirst ("PlayerID =" & PlayerID)
        ExactHC = que17or18.Exact_HC
        que17or18.Close
        Set que17or18 = Nothing
    ElseIf queT20![PlayerID] >= 17 Then
        Set que15or16 = CurrentDb.OpenRecordset("P_15or16ExactHC")
        que15or16.FindFirst ("PlayerID =" & PlayerID)
        ExactHC = que15or16.Exact_HC
        que15or16.Close
        Set que15or16 = Nothing
    ElseIf queT20![PlayerID] >= 17 Then
        Set que13or14 = CurrentDb.OpenRecordset("P_13or14ExactHC")
        que13or14.FindFirst ("PlayerID =" & PlayerID)
        ExactHC = que13or14.Exact_HC
        que13or14.Close
        Set que13or14 = Nothing
    ElseIf queT20![PlayerID] >= 17 Then
        Set que11or12 = CurrentDb.OpenRecordset("P_11or12ExactHC")
        que11or12.FindFirst ("PlayerID =" & PlayerID)
        ExactHC = que11or12.Exact_HC
        que11or12.Close
        Set que11or12 = Nothing
    ElseIf queT20![PlayerID] >= 17 Then
        Set que9or10 = CurrentDb.OpenRecordset("P_9or10ExactHC")
        que9or10.FindFirst ("PlayerID =" & PlayerID)
        ExactHC = que9or10.Exact_HC
        que9or10.Close
        Set que9or10 = Nothing
    ElseIf queT20![PlayerID] >= 17 Then
        Set que7or8 = CurrentDb.OpenRecordset("P_7or8ExactHC")
        que7or8.FindFirst ("PlayerID =" & PlayerID)
        ExactHC = que7or8.Exact_HC
        que7or8.Close
        Set que7or8 = Nothing
    ElseIf queT20![PlayerID] >= 17 Then
        Set que3to6 = CurrentDb.OpenRecordset("P_3to6ExactHC")
        que3to6.FindFirst ("PlayerID =" & PlayerID)
        ExactHC = que3to6.Exact_HC
        que3to6.Close
        Set que3to6 = Nothing
    Else: ExactHC = 0
    End If
    queT20.Close
    Set queT20 = Nothing
End Function

和SQL查询语句:

SELECT PlayerInfo.PlayerID, PlayerInfo.Display, EHC([PlayerInfo]![PlayerID]) AS ExactHandicap
FROM ((((((((PlayerInfo INNER JOIN Top20Count ON PlayerInfo.PlayerID = Top20Count.PlayerID) 
    INNER JOIN P_3to6ExactHC ON PlayerInfo.PlayerID = P_3to6ExactHC.PlayerID) 
    INNER JOIN P_7or8ExactHC ON PlayerInfo.PlayerID = P_7or8ExactHC.PlayerID) 
    INNER JOIN P_9or10ExactHC ON PlayerInfo.PlayerID = P_9or10ExactHC.PlayerID) 
    INNER JOIN P_11or12ExactHC ON PlayerInfo.PlayerID = P_11or12ExactHC.PlayerID) 
    INNER JOIN P_13or14ExactHC ON PlayerInfo.PlayerID = P_13or14ExactHC.PlayerID) 
    INNER JOIN P_15or16ExactHC ON PlayerInfo.PlayerID = P_15or16ExactHC.PlayerID) 
    INNER JOIN P_17or18ExactHC ON PlayerInfo.PlayerID = P_17or18ExactHC.PlayerID) 
    INNER JOIN P_19or20ExactHC ON PlayerInfo.PlayerID = P_19or20ExactHC.PlayerID
WHERE (((PlayerInfo.Display)=True));

当我尝试运行此操作时,我收到Undefined function 'EHC' in expression的错误消息。

我还尝试使用IIF或SWITCH语句替换函数调用相同的查询。

我大致基于MS Access 2010 Ranking Query comparing two columns for unique ranks中的文章中的函数调用的想法。

任何帮助都会非常感激,因为我只有这个问题要解决,我希望我能接近完成这个自我强加的噩梦。

1 个答案:

答案 0 :(得分:5)

我在这里看到几个问题:

  1. re:“未定义的函数”错误 - 确保您的函数位于标准VBA模块中。

  2. 确保模块在最顶部附近包含Option Explicit声明。

  3. 确保您的功能实际上对您的查询可见。最好将其明确声明为Public Function

  4. 您的函数实际上并未返回值。它需要在最终EHC = ExactHC声明之前分配End Function

  5. 检查您的查询,看看您是否确实需要所有这些INNER JOIN。我真诚地怀疑你这样做,因为你的EHC()函数会在计算精确障碍的过程中从其他查询中提取所需的信息。

  6. vba1.png


    vba2.png