문제
양식 컨트롤에서 값 (때로는 문자열의 첫 번째 부분)을 취하는 쿼리 작업을 얻으려고합니다. 내가 가진 문제는 전체 문자열을 입력 할 때만 레코드 만 반환한다는 것입니다.
즉, 성 상자에서 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;
해결책
그것에 대한 액세스 방법이 있습니다!
양식에 "필터"컨트롤이있는 경우 Application.BuildCriteria 메소드를 사용하지 않는 이유는 무엇입니까? 즉시 조항?
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*" or field1 like "BR*"'
입력하면'LIKE "GR*" OR LIKE "BR*"'
제어에서
PS : 양식의 "필터"컨트롤이 항상 동일한 구문을 가지고 있다면 ( "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 ...
다른 팁
당신은 당신의 좋아요 표현을 거꾸로 가지고 있습니다. 쿼리를 다시 작성하여 불필요한 IIF 명령을 제거하고 같은 연산자에 대한 피연산자 순서를 수정했습니다.
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 레코드 만 뽑아냅니다.
두 가지 일이 진행 중입니다. 비교를 되돌려 야하며 문자열을 제대로 인용하지 않아야합니다.
"부분 문자열 + 와일드 카드"와 같은 [데이터베이스 필드] 여야합니다.
그리고 모든 문자열은 인용문으로 둘러싸여 있어야합니다. 쿼리가 오류를 던지지 않는 이유는 확실하지 않습니다.
따라서 다음은 작동해야합니다.
,[customerforname] Like """" & [Forms]![FrmSearchCustomer]![SearchFore] & "*""" )=True
"" ""는 문자열에 단일 더블 쿼트를 추가하는 유일한 방법입니다.
내 유일한 thoguht는 아마도 그룹을 그룹화하는 데 필요하다는 것입니다.
예를 들어 첫 번째 부분의 스 니펫입니다
,[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)