2014-01-05 84 views
1

我試圖將8個不同查詢的結果合併到一個查詢中。所有要使用的查詢都是查詢查詢查詢的查詢。 8系列的4個查詢根據他們打過多少場高爾夫球將球員分開。每個系列中的最後一個查詢計算每個球員的確切障礙。在訪問查詢中調用VBA函數

我想要做的事可能無法與我使用的代碼。

功能代碼如下:

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文章中的函數調用的想法。

任何幫助將不勝感激,因爲我只有這個問題來解決,我希望我將接近完成這個自我強加的惡夢。

回答

4

我在這裏看到幾個問題:

  1. 重新:「未定義函數」錯誤 - 請確保您的函數是一個標準的VBA模塊中。

  2. 確保模塊在最頂端附近包含Option Explicit語句。

  3. 確保您的函數實際上對您的查詢可見。最好明確聲明爲Public Function

  4. 你的函數實際上並不返回一個值。它需要在最後的End Function聲明之前分配EHC = ExactHC

  5. 檢查您的查詢,看看是否確實需要所有這些INNER JOIN。我真誠地懷疑你這麼做,因爲你的EHC()函數將在計算精確障礙的過程中從這些其他查詢中提取所需的信息。

vba1.png


vba2.png

+0

感謝您的輸入戈德。我現在正在'ExactHC = que19or20.Exact_HC'行上發現'Compile Error:Method or data member not found'。我爲變量「ExactHC」添加了一個暗淡的聲明,因爲我意識到沒有一個聲明,因爲我修復了您指出的其他內容。它正在查詢的SQL語句是「SELECT P_19或20RForHC.PlayerID,Avg(P_19或20RF.PlayedTo)AS [Avg],Round([Avg] * 0.93,1)AS Exact_HC FROM P_19或20RFHCGROUP BY P_19或20RFHC.PlayerID ORDER BY P_19or20RforHC.PlayerID;'。有任何想法嗎? – Teamgilla

+0

我已經整理出代碼中出了什麼問題,並且您對連接是正確的。代碼應該是'ExactHC = que19or20![Exact_HC]'。但是我仍然收到一個'Undefined Function'錯誤。在解決這個問題的過程中,我提出了一些其他的代碼,但是基於相同的原理,從頭開始計算,運行得很好,很快,但幾秒後又重新運行,不會停下來。我可能會將其作爲另一個問題發佈。 – Teamgilla