tags:

views:

38

answers:

3

OK, back again. I have a problem getting a drop down list to populate based on information in two fields.

I have got the SQL correct as to Select just one year if I put DateValue('01/01/2001') in both places, but I am trying now to get it to grab the year data from the MS access form - another drop down named "cboYear".

I'd hate to have to do something in VB, unless necessary.

so far I have gotten this to pull up something (its always incorrect)

SELECT DISTINCT Database_New.ASEC
FROM Database_New
WHERE Database_New.Date>=DateValue('01/01/' & [cboYear]) 
      And Database_New.Date<=DateValue('12/31/' & [cboYear]);

and

SELECT DISTINCT Database_New.ASEC
FROM Database_New
WHERE Database_New.Date>=DateValue('01/01/' + [cboYear]) 
      And Database_New.Date<=DateValue('12/31/' + [cboYear]);


SELECT DISTINCT Database_New.ASEC
FROM Database_New
WHERE Database_New.Date>=DateValue('01/01/' AND [cboYear]) 
     And Database_New.Date<=DateValue('12/31/' AND [cboYear]);

both give errors saying that it is too complex to compute.

Its probably something simple, but where do I go from here?

edit/addition

completely blanked out - yeah sorry about that

cboYear is not linked to anything, it just displays the distinct years avalible in the database, Ex - 2001, 2002, 2003 - just 4 digits.

+3  A: 

What exactly is the value of [cboYear]? As in, does the ComboBox Value property actually contain four digit numeric values?

Does this work?

SELECT DISTINCT Database_New.ASEC
FROM Database_New
WHERE Year(Database_New.Date) = [cboYear];
Tomalak
no, added update to what I said - it only contains the 4 digits of the year.
A: 

Another solution you might try is:

SELECT DISTINCT Database_New.ASEC
FROM Database_New
WHERE Database_New.Date>= DateSerial([cboYear], 1, 1) 
      And Database_New.Date<= DateSerial([cboYear], 12, 31);
Thomas
A: 

Say you have a list box named lstASEC on a from named frmFoo. The RowSource for lstASEC is a query based on the value of a combo box named cboYear.

So I think the RowSource SQL should explicitly identify where to find cboYear.

SELECT DISTINCT Database_New.ASEC
FROM Database_New
WHERE Year(Database_New.[Date]) = Forms!frmFoo!cboYear;

In the after update event of cboYear, requery lstASEC so its content is updated to match the selected year.

Me.lstASEC.Requery

I still think you should change the field's name from Date. In this case I enclosed Date with square brackets to make it unambiguous for Access to recognize it as a field rather than the Date() function.

HansUp
@HansUp has fixed one of the main deficiencies of the original, in that he has qualified the combo box to have a parent. A standalone reference like that used in the original shows confusion as to context. No form control should ever be referred to in SQL or code without a parent form identified, either with the Me keyword (in code) or the fully qualified form, i.e., Forms!MyForm!cboYear.
David-W-Fenton