2011-05-12 62 views
1

我創建了此函數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; 

回答

2

功能無法識別的原因是您尚未完全指定名稱。需要使用表單名稱指定表單模塊中的公共函數:

Forms!MyForm.GetSubName() 

但是,這是錯誤的方法,而且您的代碼過於複雜。您可以在查詢中直接訪問該組合框的值:

Forms!MyForm!SubcontractorCombo 

現在,您使用.SelText建議我要麼你正在做的事情非常非常棘手的事實,或者你有你組合框設置錯誤。組合框可以有一個找到的字段和一個顯示值,這樣一個員工列表可能會顯示員工姓氏/名字,而組合框實際上有一個綁定字段爲EmployeeID。

如果你的組合框有一個隱藏的綁定字段,但你想顯示的值,你不需要使用.SelText - 只需使用適當的。組合框的列():

Forms!MyForm!SubcontractorCombo.Column(1) 

(列計數從零開始,所以隱藏的列是列0,假設它是被隱藏的第一列)

此外,還有一個如果用戶在組合框中選擇文本的一部分,則會出現不完整的匹配,因此您完全不想使用.SelText。

所以,你的SQL語句的WHERE子句將最終被這個(假設我已經確診一切正常):

WHERE Projects.Completed<>True 
    AND Subs.Subcontractor=Forms!MyForm!SubcontractorCombo.Column(1) 

...你可能會失去所有的標記代碼:

Option Compare Database 
Option Explicit 
<strike>Private stSubName As String</strike> 

Private Sub Command2_Click() 
On Error GoTo Err_Command2_Click 
    Dim stDocName As String 
    Dim stSubName As String 

    SubcontractorCombo.SetFocus 
    <strike>stSubName = SubcontractorCombo.SelText</strike> 
    'Confirm that stSubName variable is holding correct value' 
    <strike>MsgBox "Name of Subcontractor Selected is " & stSubName</strike> 

    <strike>SetSubName stSubName</strike> 
    <strike>GetSubName</strike> 

    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 

<strike>Public Sub SetSubName(Value As String) 
    'Set the module variable to be the value passed in from externally' 
    stSubName = Value 
End Sub</strike> 

<strike>Public Function GetSubName() As String 
    'Returns the value of the module variable' 
    GetSubName = stSubName 
    'MsgBox "GetSubName Variable is " & stSubName'  
End Function</strike> 
0

請問一種替代方法的工作?

創建一個表(SubNameTable)與一個字段:子名稱

向其中添加一條記錄。

那麼你的子改成這樣:

Public Sub SetSubName(Value As String) 
    CurrentDb.Execute ("Update SubNameTable Set SubName = '" & Value & "'") 
End Sub 

現在你可以刪除功能和模塊化的變量。

然後,改變你的SQL這樣:

SELECT 
    *BlahBlahBlahFields* 
FROM 
    *BlahBlahBlahTables* 
       INNER JOIN Subs 
       INNER JOIN SubNameTable ON Subs.SubContractor = SubNameTable.SubName 
WHERE (((Projects.Completed)<>True) 
GROUP BY 
      Subs.Subcontractor, 
      Counties.County, 
      Projects.ContractID, 
      Projects.PlanQuantity, 
      Project_Items.Sub 
+0

感謝您的解決方法。它效果很好。 – JustinDoesWork

+0

我絕不會使用這種方法。它堅持數據表中的標準,而且它的標準可以通過其他方式完全訪問,而不需要任何堅持。 –

+0

評論家徽章,woot!它可能不是這個問題的最佳答案,但它至少起作用:P無論如何,你的答案更好,因此+1。 – ray

0

不,這是最大的解決方案,但應該解決一些未來的努力的形式來引用函數的問題。您可以在查詢中使用參數並將其值設置爲組合框。

您應該將公共功能放在模塊中。我叫這個單詞數

Option Compare Database 
Option Explicit 


Private stSubName As String 

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 

你的形式將引用模塊中的功能:

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 

Module2.SetSubName stSubName 
Module2.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 

查詢將能夠找到模塊中的公共職能。

相關問題