如何为Microsoft Access表单中的ComboBox <select all =“”>添加功能</select>

时间:2013-01-24 14:08:04

标签: sql database ms-access database-design access-vba

所以我前几天发布了一个关于访问表单的问题,该表单需要在2个ComoBox中添加“全选”选项。我能够使用union添加其中2个选项。但是,这些选项暂时无效。我找到了从ComboBox获取表单参数的Query,这是我需要在选项中添加所有内容的地方,除了盯着它几个小时之后我再也没有线索了。

数据库不是由我编写的,它大约有10年的历史,并且被赋予我添加一些新功能。我已经这样做了,主人抱怨说“全选”按钮从未起作用。经过研究,按钮具有VB脚本,可将ComboBox输入清除为无效值。我正在计划废弃那些,因为我已经将选项添加到ComboBox本身。

读取组合输入的SQL查询如下所示:

PARAMETERS [Forms]![ReportCentre]![cboTreatmentType] Short, [Forms]![ReportCentre]!      [cboTreatmentDate] Short;


SELECT addresses.*,
       [firstname] & "" & [lastname]
       AS Name,
       [street] & "," & [suburb] & "" & [stateorprovince] & "" & [postalcode]
       AS
       Address
FROM   addresses
WHERE  ( ( ( addresses.treatmentid ) = [forms] ! [reportcentre] !
                                                [cbotreatmenttype].[Value] )
         AND ( ( addresses.treatmentdate ) = [forms] ! [reportcentre] !
                                                 [cbotreatmentdate].[Value] )
         AND ( ( addresses.birthmonth ) LIKE [forms] ! [reportcentre] !
                                             [txtbirthmonth].[Value]
                                                 & "*" ) )
        OR ( ( ( addresses.treatmentid ) IS NULL )
             AND
       ( ( addresses.treatmentdate ) = [forms] ! [reportcentre] !
                                           [cbotreatmentdate].[Value] )
             AND ( ( addresses.birthmonth ) LIKE [forms] ! [reportcentre] !
                                                 [txtbirthmonth].[Value]
                                                     & "*" ) )
        OR ( ( ( addresses.treatmentid ) = [forms] ! [reportcentre] !
                                                 [cbotreatmenttype].[Value] )
             AND ( ( addresses.treatmentdate ) IS NULL )
             AND ( ( addresses.birthmonth ) LIKE [forms] ! [reportcentre] !
                                                 [txtbirthmonth].[Value]
                                                     & "*" ) )
        OR ( ( ( addresses.treatmentid ) IS NULL )
             AND ( ( addresses.treatmentdate ) IS NULL )
             AND ( ( addresses.birthmonth ) LIKE [forms] ! [reportcentre] !
                                                 [txtbirthmonth].[Value]
                                                     & "*" ) )
        OR ( ( ( addresses.treatmentid ) IS NULL )
             AND
       ( ( addresses.treatmentdate ) = [forms] ! [reportcentre] !
                                           [cbotreatmentdate].[Value] )
             AND ( ( addresses.birthmonth ) IS NULL ) )
        OR ( ( ( addresses.treatmentid ) = [forms] ! [reportcentre] !
                                                 [cbotreatmenttype].[Value] )
             AND ( ( addresses.treatmentdate ) IS NULL )
             AND ( ( addresses.birthmonth ) IS NULL ) )
        OR ( ( ( addresses.treatmentid ) = [forms] ! [reportcentre] !
                                                 [cbotreatmenttype].[Value] )
             AND
       ( ( addresses.treatmentdate ) = [forms] ! [reportcentre] !
                                           [cbotreatmentdate].[Value] )
             AND ( ( addresses.birthmonth ) IS NULL ) ); 

我知道它很乱,很难理解,这就是为什么我要求帮助。如何验证两个ComboBox的“全选”选项?

1 个答案:

答案 0 :(得分:1)

一种非常简单的方法是将组合的绑定列设置为*:

 SELECT "*" As ID, "Select All" As AText 
 FROM Table1 
 UNION SELECT Table1.ID, Table1.AText 
 FROM Table1;

使用您的组合:

 Select "*" As TreatmentID, "<<All Records>>" As Treatment 
 FROM Treatment 
 UNION 
 Select Treatment.TreatmentID, Treatment.Treatment 
 From Treatment;

然后你可以使用LIKE:

SELECT Table1.ID
FROM Table1
WHERE Table1.ID Like [forms]![MainForm]![Combo]

使用您的SQL:

... WHERE (((Addresses.TreatmentID) 
  Like [Forms]![ReportCentre]![cboTreatmentType]) AND ...

如果您只有一列,则可以使用:

SELECT Table1.Atext
FROM Table1
WHERE AText Like 
   IIf(Forms![MainForm]!Combo="Select All","*",Forms![MainForm]!Combo)