質問

フォームコントロールから値(文字列の最初の部分のみ)を取得するクエリを機能させようとしています。私が抱えている問題は、完全な文字列が入力されたときにのみレコードを返すことです。

i.e。姓のボックスに「gr」と入力する必要があります。これにより、

緑 グレー グラハム

しかし現在のところ、完全な検索文字列が使用されない限り、何も表示されません。

問題のフォームには4つの検索コントロールがあり、ボックスに入力されている場合にのみクエリで使用されます。

クエリは次のとおりです:

SELECT TabCustomers.*,
       TabCustomers.CustomerForname AS NameSearch,
       TabCustomers.CustomerSurname AS SurnameSearch,
       TabCustomers.CustomerDOB AS DOBSearch,
       TabCustomers.CustomerID AS MemberSearch
FROM TabCustomers
WHERE IIf([Forms]![FrmSearchCustomer]![SearchMember] Is Null
          ,True
          ,[Forms]![FrmSearchCustomer]![SearchMember]=[customerid])=True
      AND IIf([Forms]![FrmSearchCustomer].[SearchFore] Is Null
              ,True
              ,[Forms]![FrmSearchCustomer]![SearchFore] Like [customerforname] & "*")=True
      AND IIf([Forms]![FrmSearchCustomer]![SearchLast] Is Null
              ,True
              ,[Forms]![FrmSearchCustomer]![SearchLast] Like [customersurname] & "*")=True
      AND IIf([Forms]![FrmSearchCustomer]![Searchdate] Is Null
              ,True
              ,[Forms]![FrmSearchCustomer]![Searchdate] Like [customerDOB] & "*")=True;
役に立ちましたか?

解決

そのためのアクセス方法があります!

" filter"がある場合フォームのコントロール、Application.buildCriteriaメソッドを使用して、フィルター条件を文字列に追加し、この文字列からフィルターを作成し、その場でWHERE句を作成できるようにしますか?

selectClause = "SELECT TabCustomers.* FROM TabCustomers"
if not isnull(Forms!FrmSearchCustomer!SearchMember) then
    whereClause = whereClause & application.buildCriteria(your field name, your field type, your control value) &  " AND "
endif
if not isnull(Forms!FrmSearchCustomer!SearchFore) then
    whereClause = whereClause & application.buildCriteria(...) &  " AND "
endif
if not isnull(Forms!FrmSearchCustomer!SearchLast) then
    whereClause = whereClause & application.buildCriteria(...) &  " AND "
endif
if not isnull(Forms!FrmSearchCustomer!SearchDate) then
    whereClause = whereClause & application.buildCriteria(...) & " AND "
endif
--get rid of the last "AND"
if len(whereClause) > 0 then
     whereClause = left(whereClause,len(whereClause)-5)
     selectClause = selectClause & " WHERE " & whereClause
endif
-- your SELECT instruction is ready ...

編集:buildCriteriaは(たとえば)を返します:

  • 'field1 =" GR"' " GR"と入力するとコントロール内
  • 'field1 LIKE" GR *"' コントロールに" GR *" と入力すると
  • 'field1 LIKE" GR *" 'LIKE" GR *"と入力すると、" BR *"' などのfield1または、コントロールの" BR *" 'が好き

PS:" filter"の場合フォーム上のコントロールは常に同じ構文を持ち(たとえば、" search_fieldName"ここで" fieldName"は基になるレコードセットのフィールドに対応します)、常に同じゾーンに配置されます(formHeaderとしましょう)。現在のフォームのフィルターを自動的に生成する関数を作成します。このフィルターは、フォームフィルターとして設定するか、他の用途に使用できます。

For each ctl in myForm.section(acHeader).controls
    if ctl.name like "search_"
        fld = myForm.recordset.fields(mid(ctl.name,8))
        if not isnull(ctl.value) then
           whereClause = whereClause & buildCriteria(fld.name ,fld.type, ctl.value) & " AND "
        endif
    endif
next ctl
if len(whereClause)> 0 then ...

他のヒント

後方にLIKE式があります。クエリを書き直して、不要なIIFコマンドを削除し、LIKE演算子のオペランドの順序を修正しました。

SELECT TabCustomers.*
FROM TabCustomers
WHERE (Forms!FrmSearchCustomer!SearchMember Is Null Or Forms!FrmSearchCustomer!SearchMember=[customerid]) 
And (Forms!FrmSearchCustomer.SearchFore Is Null Or [customerforname] Like Forms!FrmSearchCustomer!SearchFore & "*") 
And (Forms!FrmSearchCustomer!SearchLast Is Null Or [customersurname] Like Forms!FrmSearchCustomer!SearchLast & "*") 
And (Forms!FrmSearchCustomer!Searchdate Is Null Or [customerDOB] Like Forms!FrmSearchCustomer!Searchdate & "*");

最も可能性の高い状況を複製してクエリを作成しました。上記のフィールドを含むダミーテーブルと、フィールドとサブフォームを作成し、検索ボタンを押したときに上記のクエリが更新されるようにしました。必要に応じて、作成したサンプルへのダウンロードリンクを提供できます。この例は期待どおりに機能します。 JはJimとJohnのみをピックアップしますが、JohnまたはJoはJohnレコードのみをプルします。

2つのことが行われています-比較を逆にする必要があり、文字列を適切に引用していません。

「部分文字列+ワイルドカード」のような[データベースフィールド]である必要があります

すべての文字列を引用符で囲む必要があります-クエリがエラーをスローしない理由がわかりません

したがって、次のように動作するはずです:

,[customerforname] Like  """" & [Forms]![FrmSearchCustomer]![SearchFore] & "*""" )=True

""""に注意してください。これが単一の二重引用符を文字列に追加する唯一の方法です。

私の唯一の方法は、グループ化するために多分()が必要なことです

たとえば、最初の部分のスニペット

,[Forms]![FrmSearchCustomer]![SearchFore] Like ([customerforname] & "*"))=True

アクセスを使用してからしばらく経ちましたが、最初に思い浮かぶのは

これは、名前フィールドまたは生年月日フィールドにヌルを許可するための完全な書き直しです。数値のcustomeridフィールドにテキストが入力されても、このクエリは複雑すぎて失敗しません。

SELECT TabCustomers.CustomerForname AS NameSearch, TabCustomers.CustomerSurname AS SurnameSearch, TabCustomers.CustomerDOB AS DOBSearch, TabCustomers.customerid AS MemberSearch
FROM TabCustomers
WHERE TabCustomers.customerid Like IIf([Forms]![FrmSearchCustomer].[Searchmember] Is Null,"*",[Forms]![FrmSearchCustomer]![Searchmember])
AND Trim(TabCustomers.CustomerForname & "") Like IIf([Forms]![FrmSearchCustomer].[SearchFore] Is Null,"*",[Forms]![FrmSearchCustomer]![SearchFore] & "*")
AND Trim(TabCustomers.CustomerSurname & "") like IIf([Forms]![FrmSearchCustomer].[Searchlast] Is Null,"*",[Forms]![FrmSearchCustomer]![SearchLast] & "*")
AND (TabCustomers.CustomerDOB Like IIf([Forms]![FrmSearchCustomer].[SearchDate] Is Null,"*",[Forms]![FrmSearchCustomer]![SearchDate] ) Or TabCustomers.CustomerDOB Is Null)
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top