Hi All,
I have a query which is causing me some problems. I've attached a snapshot of the design view. I'd like the query results to be as follows
1. If i choose nothing then i get all the results in the table (CTMain)
2. If i choose a Plant i see all results for that plant only
3. If i choose a Location...same as above
4. If i choose 2 plants (using the OR function in the criteria) i'd like to see all the results from both Plants...same applies to Location
I'd certainly appreciate any help you could offer
Thanks,
Em
Always better to copy/paste the full sql and surround with code tags rather than a screenshot. Then we can copy paste the names of your fields/formulae when responding
1. If i choose nothing then i get all the results in the table (CTMain)
2. If i choose a Plant i see all results for that plant only
3. If i choose a Location...same as above
Ok sorry i should have posted the SQL. Thanks for your help with this
Code:
SELECT tbl_CTMain.DateRaised, tbl_CTMain.Plant, tbl_CTMain.Location
FROM tbl_CTMain
WHERE (((tbl_CTMain.DateRaised)>=[Forms]![frm_DLFilter]![txtD1] And (tbl_CTMain.DateRaised)<=[Forms]![frm_DLFilter]![txtD2]) AND ((tbl_CTMain.Plant) Like "*" & [Forms]![frm_DLFilter]![cboPlant] & "*") AND ((tbl_CTMain.Location) Like "*" & [Forms]![frm_DLFilter]![cboBlock] & "*"));
thanks - so try this. I've taken out all the unnecessary brackets and table names to make it easier to read - the query builder will put them back in when you save it
Code:
SELECT DateRaised, Plant, Location
FROM tbl_CTMain
WHERE (DateRaised>=[Forms]![frm_DLFilter]![txtD1] OR [Forms]![frm_DLFilter]![txtD1] is null) And (DateRaised<=[Forms]![frm_DLFilter]![txtD2] OR [Forms]![frm_DLFilter]![txtD2] is null) AND (Plant Like "*" & [Forms]![frm_DLFilter]![cboPlant] & "*" OR [Forms]![frm_DLFilter]![cboPlant] is null) & "*" AND (Location Like "*" & [Forms]![frm_DLFilter]![cboBlock] & "*" OR [Forms]![frm_DLFilter]![cboBlock] is null);
I'm a bit confused by your form - if you are selecting from comboboxes you should have an exact match in which case I would expect to use
Code:
SELECT DateRaised, Plant, Location
FROM tbl_CTMain
WHERE (DateRaised>=[Forms]![frm_DLFilter]![txtD1] OR [Forms]![frm_DLFilter]![txtD1] is null) And (DateRaised<=[Forms]![frm_DLFilter]![txtD2] OR [Forms]![frm_DLFilter]![txtD2] is null) AND (Plant =[Forms]![frm_DLFilter]![cboPlant] OR [Forms]![frm_DLFilter]![cboPlant] is null) & "*" AND (Location = [Forms]![frm_DLFilter]![cboBlock] OR [Forms]![frm_DLFilter]![cboBlock] is null);
information not provided for your 4th requirement
also in sql you are passing dates as string when referencing the form (it is a textbox control), date formats should be in the US format of mm/dd/yyyy and the string should be surrounded by the # character to tell sql that what is between them is to be treated as a date in which case you would have
Code:
SELECT DateRaised, Plant, Location
FROM tbl_CTMain
WHERE (DateRaised>="#" & format ([Forms]![frm_DLFilter]![txtD1],"mm/dd/yyyy") & "#" OR [Forms]![frm_DLFilter]![txtD1] is null) And (DateRaised<="#" & format([Forms]![frm_DLFilter]![txtD2],"mm/dd/yyyy") & "#" OR [Forms]![frm_DLFilter]![txtD2] is null) AND (Plant =[Forms]![frm_DLFilter]![cboPlant] OR [Forms]![frm_DLFilter]![cboPlant] is null) & "*" AND (Location = [Forms]![frm_DLFilter]![cboBlock] OR [Forms]![frm_DLFilter]![cboBlock] is null);
If you already use the US format then you don't need the format function but you still need the # chars
dates are stored as a special form of decimal number, what you see is based on the format property. So today is 43649 and now (11:52am) is 43649.4944212963 where the bit after the decimal point is the based on the number of seconds to now for the day, divided by 86400 (the number of seconds in a day)
The reason for mentioning this is because if your dateraised field is populated with the now function then it will include a time element, even if you can't see it due to the format. So your code
Thanks again for your time with this.....i actually haven't a clue what i'm looking at when viewing code so would it be possible to show me how to do this just using Design View in the query ?. Sorry for being a dunce
I swapped your code for my own and it all looks like a bit of a mess now
Ok i've pasted it code in but it looks like nothing i've ever seen in Design View. When i try the query, the dates function works but the Location and Plant just return all records.
that comes from freetyping - had left a ' & "*" ' in there. try this
Code:
SELECT DateRaised, Plant, Location
FROM tbl_CTMain
WHERE (DateRaised>="#" & format ([Forms]![frm_DLFilter]![txtD1],"mm/dd/yyyy") & "#" OR [Forms]![frm_DLFilter]![txtD1] is null) And (DateRaised<="#" & format([Forms]![frm_DLFilter]![txtD2],"mm/dd/yyyy") & "#" OR [Forms]![frm_DLFilter]![txtD2] is null) AND (Plant =[Forms]![frm_DLFilter]![cboPlant] OR [Forms]![frm_DLFilter]![cboPlant] is null) AND (Location = [Forms]![frm_DLFilter]![cboBlock] OR [Forms]![frm_DLFilter]![cboBlock] is null);
my bad - I'm used to writing and running queries in VBA - when referencing a text box in a query you don't need the # chars or it would appear the format function either
try this
Code:
SELECT DateRaised, Plant, Location
FROM tbl_CTMain
WHERE (DateRaised>=[Forms]![frm_DLFilter]![txtD1] OR [Forms]![frm_DLFilter]![txtD1] is null) And (DateRaised<= [Forms]![frm_DLFilter]![txtD2] OR [Forms]![frm_DLFilter]![txtD2] is null) AND (Plant =[Forms]![frm_DLFilter]![cboPlant] OR [Forms]![frm_DLFilter]![cboPlant] is null) AND (Location = [Forms]![frm_DLFilter]![cboBlock] OR [Forms]![frm_DLFilter]![cboBlock] is null);
Thanks again...i'm hopeless with code sorry. Your amended code works fine for the dates and the plant but returns nothing for the Block. I tried adding in a second plant to your code but it doesn't work....i need to be able to select 2 plants. Sorry this is turning into a marathon
Code:
SELECT DateRaised, Plant, Location
FROM tbl_CTMain
WHERE (DateRaised>=[Forms]![frm_DLFilter]![txtD1] OR [Forms]![frm_DLFilter]![txtD1] is null) And (DateRaised<= [Forms]![frm_DLFilter]![txtD2] OR [Forms]![frm_DLFilter]![txtD2] is null) AND (Plant =[Forms]![frm_DLFilter]![cboPlant] OR [Forms]![frm_DLFilter]![cboPlant] is null) AND (Plant =[Forms]![frm_DLFilter]![cboPlant2] OR [Forms]![frm_DLFilter]![cboPlant2] is null) AND (Location = [Forms]![frm_DLFilter]![cboBlock] OR [Forms]![frm_DLFilter]![cboBlock] is null);
sounds like you may be using lookup fields in your table. If so remove them - it is likely that whatever you have set the bound column to in your table is not the same as the value stored in your table - lookups in tables hide this
I tried adding in a second plant to your code but it doesn't work....i need to be able to select 2 plants
Yes everything is set as text. Sorry i was a bit confused....cboBlock is looking at a table of possible blocks which i set up to populate the combo box. This was just for getting results from the query which looks at the main table
There is a table of values which i set up to populate the combo box cboBlock and this is used to feed the query which looks at the main table. Incidentally a block is a work area and a plant is a smaller section within that area.