2017-01-26 65 views
0

我想使用表单字段来检查表列值来预审查查询运行这是我有的代码,但我得到一个运行时间错误。VBA无法获得,如果elseif,else语句工作

' Set Warnings 
DoCmd.SetWarnings False 
If Forms!FrmCopyRoutingMenu!TextTarget = tblQuoteSection1Lines!tblQuoteMstrID Then 
    MsgBox ("Quote Section 1 Already Started! Please Copy Individual Sections.") 

ElseIf Forms!FrmCopyRoutingMenu!TextTarget = Table!tblQuoteSection2Lines!tblQuoteMstrID Then 
    MsgBox ("Quote Section 2 Already Started! Please Copy Individual Sections.") 

ElseIf Forms!FrmCopyRoutingMenu!TextTarget = Table!tblQuoteSection3Lines!tblQuoteMstrID Then 
    MsgBox ("Quote Section 3 Already Started! Please Copy Individual Sections.") 

ElseIf Forms!FrmCopyRoutingMenu!TextTarget = Table!tblQuoteSection4Lines!tblQuoteMstrID Then 
    MsgBox ("Quote Section 4 Already Started! Please Copy Individual Sections.") 

ElseIf Forms!FrmCopyRoutingMenu!TextTarget = Table!tblQuoteSection5Lines!tblQuoteMstrID Then 
    MsgBox ("Quote Section 5 Already Started! Please Copy Individual Sections.") 

Else 
+0

欢迎来到Stack Overflow。发布您遇到的问题的错误消息是明智的。这将使用户能够确定发生了什么。 – Brian

+0

运行时错误'424'对象要求 – Corey

+0

我不认为你可以链接到这样的表。您将不得不用记录集对象打开表格并以这种方式抓取列。 – abraxascarab

回答

2

是否要检查TextTarget中的值是否在每个表的ID字段中的任何位置?这是否工作?

Dim target As String 
Dim Found As Boolean 

target = Forms!FrmCopyRoutingMenu!TextTarget 
Found = False 

For i = 1 To 5 
If DCount("tblQuoteMstrID", "tblQuoteSection" & i & "Lines", "tblQuoteMstrID = '" & target & "'") > 0 Then 
    MsgBox ("Quote Section " & i & " Already Started! Please Copy Individual Sections.") 
    Found = True 
End If 
Next i 

If Not Found Then 
    'code to run query goes here 
End If 
+0

这看起来像可能会工作,但我不能让它继续运行到我的else语句,如果它不是真实的运行查询。 – Corey

+0

@Corey一种方法是在循环结束后包含一个可以检查的布尔值。我相应地编辑了我的答案 – Leroy

+0

感谢您的帮助,但是我通过使用查询从每个表创建了所需主标识的单个表,然后使用if(DAVG()> 0) 。再次感谢您在这方面的帮助,您的想法正是我需要从另一个方向开始审视这个问题。 – Corey