次の方法で共有


ユーザー入力を使用してフィルターの抽出条件を作成する

BuildCriteria メソッドを使用すると、ユーザーの入力を基にフィルターの抽出条件を簡単に生成することができます。 このメソッドは、クエリ デザイン グリッド、フォーム フィルター、フォーム サーバー フィルター モードで式を入力する場合と同じ方法で引数 expression をパーズします。

次の例では、ユーザーに商品名の先頭部分の文字をいくつか入力するように促し、 BuildCriteria メソッドを使用して、ユーザーの入力に基づいた抽出条件文字列を生成します。 Next, the procedure provides this string as an argument to the Filter property of a form named Products. Finally, the FilterOn property is set to apply the filter.

Sub SetFilter() 
    Dim frm As Form, strMsg As String 
    Dim strInput As String, strFilter As String 
 
    ' Open Products form in Form view. 
    DoCmd.OpenForm "Products" 
 
    ' Return Form object variable pointing to Products form. 
    Set frm = Forms!Products 
 
    strMsg = "Enter one or more letters of product name " _ 
        & "followed by an asterisk." 
 
    ' Prompt user for input. 
    strInput = InputBox(strMsg) 
 
    ' Build criteria string. 
    strFilter = BuildCriteria("ProductName", dbText, strInput) 
 
    ' Set Filter property to apply filter. 
    frm.Filter = strFilter 
 
    ' Set FilterOn property; form now shows filtered records. 
    frm.FilterOn = True 
End Sub

サポートとフィードバック

Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。