2016-07-30 20 views
0

テストアクセスデータベースにクエリを作成する次のvbaがあります。私は2つの複数の選択リストボックスを持っています。問題は、「Me![State]」から複数の項目を選択でき、「Me![Animal]」から何も選択できず、クエリを実行できることです。ただし、これを処理するためにクエリ言語が設定されていないため、これは不可能です。それは私に "私[動物]"から何かを選択させます。複数の選択ボックスの1つから項目を選択しない場合の複数の「複数の選択リストボックス」に基づくクエリ

複数のリストボックスのいずれかに選択されたものがない場合、またはその両方に選択肢がある場合、両方の選択リストボックスでクエリを実行できるようにするにはどうすればよいですか?

Private Sub Command6_Click() 


    Dim Q As QueryDef, DB As Database 
    Dim Criteria As String 
    Dim ctl As Control 
    Dim Itm As Variant 
    Dim ctl2 As Control 
    Dim ctl3 As Control 
    ' Build a list of the selections. 


    Set ctl = Me![Animal] 
    For Each Itm In ctl.ItemsSelected 
     If Len(Criteria) = 0 Then 
     Criteria = Chr(34) & ctl.ItemData(Itm) & Chr(34) 
     Else 
     Criteria = Criteria & "," & Chr(34) & ctl.ItemData(Itm) _ 
      & Chr(34) 
     End If 
    Next Itm 
    If Len(Criteria) = 0 Then 
     Itm = MsgBox("You must select one or more items in the" & _ 
     " list box!", 0, "No Selection Made") 
     Exit Sub 
    End If 



     Set ctl2 = Me![State] 
    For Each Itm In ctl2.ItemsSelected 
     If Len(Criteria2) = 0 Then 
     Criteria2 = Chr(34) & ctl2.ItemData(Itm) & Chr(34) 
     Else 
     Criteria2 = Criteria2 & "," & Chr(34) & ctl2.ItemData(Itm) _ 
      & Chr(34) 
     End If 
    Next Itm 
    If Len(Criteria2) = 0 Then 
     Itm = MsgBox("You must select one or more items in the" & _ 
     " list box!", 0, "No Selection Made") 
     Exit Sub 
    End If 




    ' Modify the Query. 
    Set DB = CurrentDb() 
    Set Q = DB.QueryDefs("animalquery") 
    ' Modify the Query. 
    Set DB = CurrentDb() 
    Set Q = DB.QueryDefs("animalquery") 
    Q.SQL = "Select * From [table1] Where [table1].[type] In (" & "'Animal'" & _ 
    ")" & " and [table1].[animal] in (" & Criteria & _ 
    ")" & " and [table1].[state] in (" & Criteria2 & _ 
    ")" & ";" 
    Q.Close 

    ' Run the query. 
    DoCmd.OpenQuery "animalquery" 
End Sub 

答えて

0

EDIT - あなたはあなたの基準vaiablesの簡単なチェックでこれを行うことができますコメント

あたりとして比較を修正しました。

あなたは既に長さのチェックをしています。後でダイナミックSQLをビルドするときに使用します。

私は、起こっていることを簡単に追跡できるようにstrSQL変数を追加しました。エラーメッセージを調整して、1つまたは他の条件が空になるようにしてください。

Private Sub Command6_Click() 

    Dim Q   As QueryDef 
    Dim DB   As Database 
    Dim Criteria As String 
    Dim ctl   As Control 
    Dim Itm   As Variant 
    Dim ctl2  As Control 
    Dim ctl3  As Control 

    ' Use for dynamic SQL statement' 
    Dim strSQL  As String 

    Set ctl = Me![Animal] 
    For Each Itm In ctl.ItemsSelected 
     If Len(Criteria) = 0 Then 
      Criteria = Chr(34) & ctl.ItemData(Itm) & Chr(34) 
     Else 
      Criteria = Criteria & "," & Chr(34) & ctl.ItemData(Itm) & Chr(34) 
     End If 
    Next Itm 

    Set ctl2 = Me![State] 
    For Each Itm In ctl2.ItemsSelected 
     If Len(Criteria2) = 0 Then 
      Criteria2 = Chr(34) & ctl2.ItemData(Itm) & Chr(34) 
     Else 
      Criteria2 = Criteria2 & "," & Chr(34) & ctl2.ItemData(Itm) & Chr(34) 
     End If 
    Next Itm 

    If (Len(Criteria) = 0) And (Len(Criteria2) = 0) Then 
     Itm = MsgBox("You must select one or more items from one of the list boxes!", 0, "No Selection Made") 
     Exit Sub 
    End If 

    ' Modify the Query. 
    Set DB = CurrentDb() 
    Set Q = DB.QueryDefs("animalquery") 
    ' Modify the Query. 
    Set DB = CurrentDb() 
    Set Q = DB.QueryDefs("animalquery") 

    strSQL = "Select * From [table1] Where [table1].[type] In (" & "'Animal')" 

    If (Len(Criteria) <> 0) Then ' Append Animal Criteria 
     strSQL = strSQL & " AND [table1].[animal] IN (" & Criteria & ")" 
    End If 
    If (Len(Criteria2) <> 0) Then ' Append State Criteria 
     strSQL = strSQL & " AND [table1].[state] IN (" & Criteria2 & ")" 
    End If 

    Q.SQL = strSQL 
    Q.Close 

    ' Run the query. 
    DoCmd.OpenQuery "animalquery" 
End Sub 
+0

ありがとうございました!私はあなたがそこで何をしたかを見ており、それは従うのがより簡単になります!しかし、私は – AlmostThere

+1

でエラーが発生しているそれは完璧だった!それは道に従う方が簡単です!私は1つの変更を行う必要がありました。 "If(Len(Criteria)= 0)Then 'Add Animal Criteria"と "If(Len(Criteria2)= 0)" Add Append State Criteria "については、" = 0 "を" <> = 0 "それは魅力のように働いた! – AlmostThere

+0

良い仕事のデバッグ。それはあなたがコードをもっと必要とするスキルです! – dbmitch

関連する問題