2017-01-26 21 views
0

フォームフィールドを使用してテーブルの列の値をチェックして、これを実行するクエリを事前認定しようとしていますが、実行しているコードですエラー。VBAでif、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

ようこそスタックオーバーフロー。問題が発生したときに表示されるエラーメッセージを投稿することをお勧めします。これにより、ユーザーは何が起こっているのかを判断することができます。 – 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 1つの方法は、ループが終了した後にチェックできるブール値を含めることです。私はそれに応じて私の答えを編集しました – Leroy

+0

これはあなたの助けていただきありがとうございますが、私はそれを使用して各テーブルからの希望のマスターIDの単一のテーブルを作成するクエリを使用して取得し、私は(DAVG()> 0) 。もう一度、このアイデアを参考にしていただき、ありがとうございました。あなたのアイデアは、私が別の方向から問題を見るのに必要なものでした。 – Corey

関連する問題