几个小时以来,我一直在努力为办公室组装一个(相当简单的)数据库(和相应的表格)。
我们有两个主表 -CLIENT DETAILS
和SALES
(通过 1 对多关系CLIENT NUMBER
)。
我创建了一个表单,使用户能够从表单中搜索任何匹配的数据,例如
Field: BUSINESS NAME
Table: CLIENT DETAILS
Criteria: Like "*" & [Forms]![FINDClient]![BUSINESS_NAME] & "*" Or Is Null
这适用于大多数形式,但是我在尝试合并日期范围时遇到了主要的戏剧。例如
Field: CONTACT DATE
Table: SALES
Criteria: >=[Forms]![FINDClient]![CONTACT_DATE_1] And <=[Forms]![FINDClient]!
[CONTACT_DATE_2] Or Is Null
Field: PAID DATE<br>
Table: SALES<br>
Criteria: >=[Forms]![FINDClient]![PAID_DATE_1] And <=[Forms]![FINDClient]![PAID_DATE_2] Or Is Null
Field: CANCELLED DATE<br>
Table: SALES<br>
Criteria: >=[Forms]![FINDClient]![CANCELLED_DATE_1] And <=[Forms]![FINDClient]![CANCELLED_DATE_2] Or Is Null
从本质上讲,我想要实现的是一个查询,它返回与搜索词匹配的记录,即企业名称 = Mybiz(或 Myb*)和电话 = 555 55555 等,并且在 CONTACT_DATE、PAID_DATE 或 CANCELED DATE 上。
用户在任何给定时间只能输入一组日期。
(如有必要,也可以根据需要通过复选框启用/禁用各个日期范围 - 这是我设置的,但可能毫无意义)
编辑:这是现在的 SQL ->
SELECT [CLIENT DETAILS].*, SALES.*
FROM [CLIENT DETAILS] INNER JOIN SALES ON [CLIENT DETAILS].[CLIENT NUMBER] = SALES.[CLIENT NUMBER]
WHERE ((([CLIENT DETAILS].[BUSINESS NAME]) Like "*" & [Forms]![FINDClient]![BUSINESS_NAME] & "*" Or ([CLIENT DETAILS].[BUSINESS NAME]) Is Null)
AND (([CLIENT DETAILS].ADDRESS) Like "*" & [Forms]![FINDClient]![ADDRESS] & "*" Or ([CLIENT DETAILS].ADDRESS) Is Null)
AND (([CLIENT DETAILS].CITY) Like "*" & [Forms]![FINDClient]![CITY] & "*" Or ([CLIENT DETAILS].CITY) Is Null)
AND (([CLIENT DETAILS].STATE) Like "*" & [Forms]![FINDClient]![STATE] & "*" Or ([CLIENT DETAILS].STATE) Is Null)
AND (([CLIENT DETAILS].POSTCODE) Like "*" & [Forms]![FINDClient]![POSTCODE] & "*" Or ([CLIENT DETAILS].POSTCODE) Is Null)
AND (([CLIENT DETAILS].PHONE) Like "*" & [Forms]![FINDClient]![PHONE] & "*" Or ([CLIENT DETAILS].PHONE) Is Null)
AND (([CLIENT DETAILS].[FAX NUMBER]) Like "*" & [Forms]![FINDClient]![FAX] & "*" Or ([CLIENT DETAILS].[FAX NUMBER]) Is Null)
AND (([CLIENT DETAILS].EMAIL) Like "*" & [Forms]![FINDClient]![EMAIL] & "*" Or ([CLIENT DETAILS].EMAIL) Is Null)
AND (([CLIENT DETAILS].WEBSITE) Like "*" & [Forms]![FINDClient]![WEBSITE] & "*" Or ([CLIENT DETAILS].WEBSITE) Is Null)
AND (([CLIENT DETAILS].COMMENTS) Like "*" & [Forms]![FINDClient]![COMMENTS] & "*" Or ([CLIENT DETAILS].COMMENTS) Is Null)
AND (([CLIENT DETAILS].[PHONE 2]) Like "*" & [Forms]![FINDClient]![PHONE] & "*" Or ([CLIENT DETAILS].[PHONE 2]) Is Null)
AND (([CLIENT DETAILS].[ADDRESS 2]) Like "*" & [Forms]![FINDClient]![Address] & "*" Or ([CLIENT DETAILS].[ADDRESS 2]) Is Null)
AND (([CLIENT DETAILS].[CITY 2]) Like "*" & [Forms]![FINDClient]![City] & "*" Or ([CLIENT DETAILS].[CITY 2]) Is Null)
AND (([CLIENT DETAILS].[STATE 2]) Like "*" & [Forms]![FINDClient]![State] & "*" Or ([CLIENT DETAILS].[STATE 2]) Is Null)
AND (([CLIENT DETAILS].[POSTCODE 2]) Like "*" & [Forms]![FINDClient]![POSTCODE] & "*" Or ([CLIENT DETAILS].[POSTCODE 2]) Is Null)
AND (([CLIENT DETAILS].[EMAIL 2]) Like "*" & [Forms]![FINDClient]![EMAIL] & "*" Or ([CLIENT DETAILS].[EMAIL 2]) Is Null)
AND ((SALES.[SALE NUMBER]) Like "*" & [Forms]![FINDClient]![SALE_NUMBER] & "*" Or (SALES.[SALE NUMBER]) Is Null)
AND ((SALES.BOOK) Like "*" & [Forms]![FINDClient]![BOOK] & "*" Or (SALES.BOOK) Is Null)
AND ((SALES.SALESPERSON) Like "*" & [Forms]![FINDClient]![SALESPERSON] & "*" Or (SALES.SALESPERSON) Is Null)
AND ((SALES.[CONTACT PERSON]) Like "*" & [Forms]![FINDClient]![CONTACT] & "*" Or (SALES.[CONTACT PERSON]) Is Null)
AND ((SALES.[PAID DATE])>=[Forms]![FINDClient]![PAID_DATE_1] And (SALES.[PAID DATE])<=[Forms]![FINDClient]![PAID_DATE_2])
AND ((SALES.[CANCELLED DATE])>=[Forms]![FINDClient]![CANCELLED_DATE_1] And (SALES.[CANCELLED DATE])<=[Forms]![FINDClient]![CANCELLED_DATE_2]));
最后两个条目应该是依赖于 IF 语句的条目。