![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi Everyone, I am using a query to populate a listbox on a form. There is a criteria for this query that is based on the value of another listbox. The tables behind are normalised and the 'heirarchy' of relationships between the tables is reflected in the heirarchy in usage of the listboxes. The 'parent' listbox is called lstCategory, and has a return value of Long (its an autonumber value that is being returned) The 'child' listbox is called lstSubCategory and has a query as its datasource. In this query is a criteria. This criteria is itself a query with a WHERE clause based on a DLookup. The criteria is as follows: Not In (SELECT raw_value FROM dictSubcategory WHERE parent_id = DLookup ("dictionary_id", "dictCategory","[category_id]" = [lstCategory] ) ) In theory this should work. If I use this expression, then I am receiving incorrect results as the 'Not In' does not appear to be producing the correct list of values. If I do the 'Not In' part by hand (ie/ manually see what the results should be with SQL) then I am receiving a list of values to exclude that is correct. I am under the assumption that my DLookup statement is wrong somewhere, but when I try and alter the statement in any way I am receiving Invalid String error messages. For example I have tried the following based on the Access 2000 Bible (p770) instructions: Not In(SELECT raw_value FROM dictSubCategory WHERE parent_id = DLookup("dictionary_id","dictCategory","[category_id]='"&[lstCatego ry]&"'")) I have also tried it without the single quotes as it is a numeric value that is being passed along but I receive the same error message about strings. I have the correct number of open and close quotes but I am dammned if I can figure this out. Effectively what my criteria should be is: [category_id] = [lstCategory] Cant seem to get it to function. Can anyone help me with this one? It is so seemingly simple and its driving me insane. I am at a loss and would appreciate anyones guidance. Cheers and Thanks The >Frog |
#3
| |||
| |||
|
|
Hi Everyone, I am using a query to populate a listbox on a form. There is a criteria for this query that is based on the value of another listbox. The tables behind are normalised and the 'heirarchy' of relationships between the tables is reflected in the heirarchy in usage of the listboxes. The 'parent' listbox is called lstCategory, and has a return value of Long (its an autonumber value that is being returned) The 'child' listbox is called lstSubCategory and has a query as its datasource. In this query is a criteria. This criteria is itself a query with a WHERE clause based on a DLookup. The criteria is as follows: Not In (SELECT raw_value FROM dictSubcategory WHERE parent_id = DLookup ("dictionary_id", "dictCategory","[category_id]" = [lstCategory] ) ) In theory this should work. If I use this expression, then I am receiving incorrect results as the 'Not In' does not appear to be producing the correct list of values. If I do the 'Not In' part by hand (ie/ manually see what the results should be with SQL) then I am receiving a list of values to exclude that is correct. I am under the assumption that my DLookup statement is wrong somewhere, but when I try and alter the statement in any way I am receiving Invalid String error messages. For example I have tried the following based on the Access 2000 Bible (p770) instructions: Not In(SELECT raw_value FROM dictSubCategory WHERE parent_id = DLookup("dictionary_id","dictCategory","[category_id]='"&[lstCategory]&"'")) I have also tried it without the single quotes as it is a numeric value that is being passed along but I receive the same error message about strings. I have the correct number of open and close quotes but I am dammned if I can figure this out. Effectively what my criteria should be is: [category_id] = [lstCategory] Cant seem to get it to function. Can anyone help me with this one? It is so seemingly simple and its driving me insane. I am at a loss and would appreciate anyones guidance. Cheers and Thanks The >Frog |
#4
| |||
| |||
|
#5
| |||
| |||
|
|
Hi Guys, Thanks for the feedback. I have tried this thinking that it was the right thing to do but I am still getting the 'string' error when trying to save the query. This is what I have following the above advice that produces the same error: Not In (SELECT raw_value FROM dictSubcategory WHERE parent_id = DLookup ("dictionary_id", "dictCategory","[category_id] = " & Forms! [frmDictionarySubCategory]![lstCategory] ) ) If I use the VBA Immediate window while the form is running and test the DLookup I am getting the right data back, it just doesnt seem to want to work inside of a queries criteria. I was thinking of swapping the DLookup out with another nested SELECT statement and trying to ensure that I get only a single response back. It seems a shame as the DLookup is perfect for this situation. The full SQL statement is as follows: SELECT DISTINCT raw_OSA.SubCategory FROM raw_OSA WHERE (((raw_OSA.SubCategory) Not In (SELECT raw_value FROM dictSubcategory WHERE parent_id = DLookup ("dictionary_id", "dictCategory","[category_id]" = [lstCategory] ) )) AND ((raw_OSA.Category) In (SELECT raw_value FROM dictCategory WHERE category_id = [lstCategory]))) ORDER BY raw_OSA.SubCategory; Please note that this 'full' SQL has the non-functional DLookup in it. The only way I was able to save the query is with this non-functional form hence the silly locations for the quotation marks - I just didnt want to lose my thinking on the way through developing this app so I saved it as I could. The table structure is that there are four tables, category, subcategory, dictCategory, dictSubcategory. Subcategory is linked to Category via the category_id field (fk in Subcategory and pk in Category). DictCategory is linked to Category in the same way. DictSubcategory is linked to both subcategory and dictCategory. DictSubcategory to SubCategory on subcategory_id, and to dictCategory on parent_id (fk) which is the dictionary_id field in dictCategory. I hope that makes sense. Cheers The Frog |
#6
| |||
| |||
|
#7
| |||
| |||
|
|
Hi Salad, Yes and no. Yes it is what is currently saved, but no it doesnt work. It is also not what I want to work - I just cant save the query unless it is typed this way as all other (seemingly) combinations result in the String error and you cannot save the query with that error present. What I tried in place of what is saved is as above. The criteria condition for the DLookup should read: "[category_id] = " & [lstCategory] . If I use this I get the error. If I do the same with single quotation marks around the [lstCategory] value then I get an error. If I remove all spaces after the = sign I get the same error. It doesnt seem to matter where or how I try and build the criteria I keep getting the 'String' error. I only kept it saved as above so that I would not lose my place so to speak. In the Access 2000 Bible it states to remove all the spaces after the equals sign when typing in your description of the criteria. I have done this and it still doesnt work. eg/ "[category_id]='"&[lstCategory]&"'" . No luck there with or without a space between the [category_id] and the = sign. In short anything that should reasonably work doesnt seem to and will produce an error. The only thing that doessnt produce an error is a badly formed criteria that wont function. For the timebeing I have changed the DLookup to a SELECT DISTINCT statement that gets the job done. Nested SQL is starting to get to what I consider an acceptable limit when it hits the 3rd level. I prefer to only have two levels deep if I can - else I find things start to run slow, especially if you are using correlated subqueries (but we all know they are slow anyway...). I am stumped as to what is going on with the query syntax here. The DLookup works fine outside of the query, but I just dont seem to be able to set it correctly in a criteria. Cheers The Frog |
#8
| |||
| |||
|
|
I tend to stay away from sub selects. I had performance issues with them years ago so I haven't used them since. |
#9
| |||
| |||
|
#10
| ||||
| ||||
|
|
In the end I solved it with the following nested subquery: Not In (SELECT raw_value FROM dictSubcategory WHERE parent_id = (SELECT DISTINCT dictionary_id FROM dictCategory WHERE category_id = [lstCategory]) ) |
|
Its a little sluggish, but not too bad. I dont know how it will perform over a network. Would it help if I took the criteria subquery above and broke it into two separate queries and saved them as such? |
|
Then refer to the 'outer' query of the two for the criteria? Or will Access (2003) optimise the query just the same leaving the raw SQL in the criteria field? |
|
I am still buggered by the DLookup thing. I am going to come back to this at a later stage because I just want to know why it doesnt behave itself - it should! Damned if I know what it could be. |
![]() |
| Thread Tools | |
| Display Modes | |
| |