我創建了此函數GetSubName,我需要返回從下拉框中保存的名稱。這樣做很好,因爲我用過的對話框顯示它正確設置了變量。問題是,當下面的SQL運行在查詢中時,我得到錯誤:「表達式中未定義函數'GetSubName'」。我是VBA的新手,所以任何幫助都將非常感謝。無法從Access 2007中的用戶定義函數中提取值
下面是代碼:
Option Compare Database
Option Explicit
Private stSubName As String
Private Sub Command2_Click()
On Error GoTo Err_Command2_Click
Dim stDocName As String
Dim stSubName As String
SubcontractorCombo.SetFocus
stSubName = SubcontractorCombo.SelText
'Confirm that stSubName variable is holding correct value'
MsgBox "Name of Subcontractor Selected is " & stSubName
SetSubName stSubName
GetSubName
DoCmd.Close
stDocName = "Summary Asphalt Production for Subcontractor"
DoCmd.OpenQuery stDocName
Exit_Command2_Click:
Exit Sub
Err_Command2_Click:
MsgBox Err.Description
Resume Exit_Command2_Click
End Sub
Public Sub SetSubName(Value As String)
'Set the module variable to be the value passed in from externally'
stSubName = Value
End Sub
Public Function GetSubName() As String
'Returns the value of the module variable'
GetSubName = stSubName
'MsgBox "GetSubName Variable is " & stSubName'
End Function
這裏是我的SQL從Access 2007的內部:
SELECT DISTINCTROW Subs.Subcontractor, Counties.County, Projects.ContractID,
Sum(Project_Items.USTons) AS SumOfUSTons, Projects.PlanQuantity,
Max(Project_Items.EstDate) AS MaxOfEstDate, Project_Items.Sub
FROM Counties INNER JOIN (Subs INNER JOIN (Projects INNER JOIN Project_Items ON
Projects.ContractID = Project_Items.ProjectID) ON Subs.VendID = Project_Items.Sub) ON
Counties.ID = Project_Items.County
WHERE (((Projects.Completed)<>True) AND ((Subs.Subcontractor)=GetSubName()))
GROUP BY Subs.Subcontractor, Counties.County, Projects.ContractID,
Projects.PlanQuantity, Project_Items.Sub;
感謝您的解決方法。它效果很好。 – JustinDoesWork
我絕不會使用這種方法。它堅持數據表中的標準,而且它的標準可以通過其他方式完全訪問,而不需要任何堅持。 –
評論家徽章,woot!它可能不是這個問題的最佳答案,但它至少起作用:P無論如何,你的答案更好,因此+1。 – ray