我一直在墙上撞了几个小时试图为办公室整理一个(相当简单的)数据库(和相应的表格)。
我们有两个主要表格 - CLIENT DETAILS
和SALES
(CLIENT NUMBER
之间的1对多关系)。
我创建了一个表单,使用户可以从表单中搜索任何匹配的数据
e.g。
Field: BUSINESS NAME
Table: CLIENT DETAILS
Criteria: Like "*" & [Forms]![FINDClient]![BUSINESS_NAME] & "*" Or Is Null
这适用于大多数表单,但是在尝试合并日期范围时,我一直在遇到重大戏剧。 e.g。
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 *)AND Phone = 555 55555等,并且在CONTACT_DATE,PAID_DATE或CANCELED DATE。<登记/> 用户只能在任何给定时间输入一组日期。
(如有必要,还可以通过复选框启用/禁用各个日期范围,具体取决于所需要的内容 - 这是我设置的但可能毫无意义的事情)
编辑:这是现在的SQL - &gt;
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语句的条目。
答案 0 :(得分:0)
我会在每个日期字段旁边放一个单选按钮。在表单加载时禁用所有日期字段。当您单击单选按钮时,它将启用相应的日期字段并禁用所有其他日期字段(因此,如果他们单击另一个单选按钮,您不会同时启用多个日期字段)。然后,在表单后面的VBA中,仅使用其中一个条件语句根据启用的单选按钮构建搜索SQL。它需要用户点击一次额外的鼠标,但我无法看到你将如何以任何其他方式进行过滤。
你会做这样的事情,这是完全未经测试的&#34; aircode&#34;但应该给你一个开始:
sSQL = "SELECT ClientDetails.*, Sales.* FROM ClientDetails "
sSQL = sSQL & "LEFT JOIN SALES on ClientDetails.ClientNumber = Sales.ClientNumber "
sSQL = sSQL & "WHERE BusinessName Like "*" & [Forms]![FINDClient]![BUSINESS_NAME] & "*" Or Is Null "
If Radio1.Enabled = True then
sSQL = sSQL & "AND ContactDate >=[Forms]![FINDClient]![CONTACT_DATE_1] And <=[Forms]![FINDClient]![CONTACT_DATE_2] Or Is Null;"
ElseIf Radio2.Enabled = True then
sSQL = sSQL & "AND PaidDate >=[Forms]![FINDClient]![PAID_DATE_1] And <=[Forms]![FINDClient]![PAID_DATE_2] Or Is Null;"
Elseif Radio3.Enabled = True then
sSQL = sSQL & "AND CancelledDate >=[Forms]![FINDClient]![CANCELLED_DATE_1] And <=[Forms]![FINDClient]![CANCELLED_DATE_2] Or Is Null ;"
Else
Endif
Forms![FINDClient].RecordSource = sSQL