473,738 Members | 11,146 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Nested sql on search form - "in" works, "not in" doesn't

41 New Member
Hi..I am trying to make a search form. I am fairly new to access and could well be looking at it completely wrong. I have an unbound form with textboxes in the header for entering different search criteria.
I have a subform for displaying the results, which is bound to Query4. SQL for Query4 (taken from sql view in query):

Expand|Select|Wrap|Line Numbers
  1. SELECT tblRecipes.RecipeName, tblRecipes.FoodCategory, Sum(Query3.IngredCost) AS SumOfIngredCost, Query3.RecipeID
  2. FROM Query3 INNER JOIN tblRecipes ON Query3.RecipeID = tblRecipes.RecipeID
  3. GROUP BY tblRecipes.RecipeName, tblRecipes.FoodCategory, Query3.RecipeID;
  4.  
(Query3 is the recordsource for my main recipe form’s subform, and performs the calculations on the ingredients included in each recipe, then Query4 groups each recipe’s ingredients together using RecipeName field, and sums each recipe’s cost so I can search by cost)
So on my search form I have a command button which builds and applies a filter to the subform according to what is in the textboxes.
Its all working how I wanted it to, I can search by part of a name, by category, by cost or by ingredient perfectly. But when I try to turn the ingredient search around to find recipes that do not include x ingredient ( by replacing “in” with “not in”) I get an error – Run-time error 2001: You canceled the previous operation. Here is my code:
Expand|Select|Wrap|Line Numbers
  1. Private Sub CommandSearch_Click()
  2. Dim strWhere As String
  3. Dim lngLen As Long
  4.  
  5. If Not IsNull(Me.FiltRecName) Then
  6. strWhere = strWhere & "([RecipeName] like ""*" & Me.FiltRecName & "*"") And "
  7. End If
  8.  
  9. If Not IsNull(Me.FiltCat) Then
  10. strWhere = strWhere & "([FoodCategory] = """ & Me.FiltCat & """) And "
  11. End If
  12.  
  13. If Not IsNull(Me.FiltCost) Then
  14. strWhere = strWhere & "([SumOfIngredCost] < " & Me.FiltCost & ") And "
  15. End If
  16.  
  17. If Not IsNull(Me.FiltIng1) Then
  18. strWhere = strWhere & "([RecipeID]in (SELECT [RecipeID] FROM tblRecipeIngredients WHERE[Ingredient] = """ & Me.FiltIng1 & """)) And "
  19. End If
  20.  
  21. If Not IsNull(Me.FiltIng2) Then
  22. strWhere = strWhere & "([RecipeID] not in (SELECT [RecipeID] FROM tblRecipeIngredients WHERE[Ingredient] = """ & Me.FiltIng2 & """)) And "
  23. End If
  24.  
  25. 'Apply the filter
  26. lngLen = Len(strWhere) - 5 'Without trailing " AND "
  27. If lngLen <= 0 Then
  28. MsgBox "No criteria."
  29. Else
  30. ‘’’’’’’‘On pressing debug, this line is highlighted:
  31. Me.subfrmResults.Form.Filter = Left(strWhere, lngLen)
  32. Me.subfrmResults.Form.FilterOn = True
  33. End If
  34. End Sub
  35.  
Can anyone help me with this, or suggest a better way? I’m using Access97.
Mar 12 '07 #1
6 2431
KiwiGenie
41 New Member
I'm still struggling with this - I can't even get "Not In" to work in a straight query:
This query works perfectly:
Expand|Select|Wrap|Line Numbers
  1. SELECT Query4.RecipeName, Query4.FoodCategory, Query4.SumOfIngredCost, Query4.RecipeID
  2. FROM Query4
  3. WHERE (((Query4.RecipeID) In (SELECT [RecipeID] FROM tblRecipeIngredients WHERE[Ingredient] = [Me].[FiltIng1])));
When I replace "In" with "Not In" I get an error - "This expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may contain too many complicated elements. Try simplifying the expression by assigning parts of the expression to variables.":
Expand|Select|Wrap|Line Numbers
  1. SELECT Query4.RecipeName, Query4.FoodCategory, Query4.SumOfIngredCost, Query4.RecipeID
  2. FROM Query4
  3. WHERE (((Query4.RecipeID) Not In (SELECT [RecipeID] FROM tblRecipeIngredients WHERE[Ingredient] = [Me].[FiltIng1])));
Please can someone help me figure this out?
Mar 15 '07 #2
Denburt
1,356 Recognized Expert Top Contributor
Try this should work.
Expand|Select|Wrap|Line Numbers
  1. SELECT Query4.RecipeName, Query4.FoodCategory, Query4.SumOfIngredCost, Query4.RecipeID
  2. FROM Query4
  3. WHERE Not Query4.RecipeID In (SELECT [RecipeID] FROM tblRecipeIngredients WHERE[Ingredient] = [Me].[FiltIng1]);
Mar 15 '07 #3
KiwiGenie
41 New Member
Hi Denburt..thanks for your suggestion, I tried your modified code, unfortunately I still get the same error. I'm sure this must be possible somehow, its really driving me mad!
Mar 15 '07 #4
Denburt
1,356 Recognized Expert Top Contributor
This one is interesting,

If the following statement works for you...
Expand|Select|Wrap|Line Numbers
  1. If Not IsNull(Me.FiltIng1) Then
  2. strWhere = strWhere & "([RecipeID]in (SELECT [RecipeID] FROM tblRecipeIngredients WHERE [Ingredient] = """ & Me!FiltIng1 & """)) And "
  3. End If
Then I dont see why the following wouldn't work unless there is a problem elsewhere then again I could be overlooking something real simple (I do that at the end of a long day sometimes:
Expand|Select|Wrap|Line Numbers
  1. If Not IsNull(Me.FiltIng2) Then
  2. strWhere = strWhere & "( not [RecipeID] in (SELECT [RecipeID] FROM tblRecipeIngredients WHERE [Ingredient] = """ & Me!FiltIng2 & """)) And "
  3. End If
As a tip:
When you are refering to properties use the period after Me.
When you are refering to objects such as a textbox control use the exclamation (could be as simple as that sometimes).

Me!FiltCost
Mar 15 '07 #5
Denburt
1,356 Recognized Expert Top Contributor
OK I reread your post and looking at what you are trying to accomplish I would think you could simply use the following and it would achieve your results.
Expand|Select|Wrap|Line Numbers
  1. If Not IsNull(Me.FiltIng2) Then
  2. strWhere = strWhere & "([RecipeID] in (SELECT [RecipeID] FROM tblRecipeIngredients WHERE[Ingredient] <> """ & Me.FiltIng2 & """)) And "
  3. End If
Mar 15 '07 #6
KiwiGenie
41 New Member
Sorry its taken me so long to get back to this - I couldn't get back to this site on Friday.
I'm still not clear on why this wouldn't work, surely if something works one way it will work the opposite too. Anyway I have got it pulling the right records now, by changing the query that the (results) subform is based on. I couldn't do your last suggestion because of the one-to-many relationship between a recipe and its ingredients - eg if searching for recipes that do not use butter, all recipes include at least one ingredient that is not butter, so all records are returned.
So the new query is:
Expand|Select|Wrap|Line Numbers
  1. SELECT tblRecipes.RecipeID, tblRecipes.RecipeName, tblRecipes.FoodCategory, tblRecipeIngredients.Ingredient, Format(DSum("[Query3]![IngredCost]","[Query3]","[Query3]![RecipeID] =" & [tblRecipes]![RecipeID]),"Currency") AS SumOfIngredCost
  2. FROM tblRecipes INNER JOIN tblRecipeIngredients ON tblRecipes.RecipeID = tblRecipeIngredients.RecipeID;
Then instead of
Expand|Select|Wrap|Line Numbers
  1. Me.subfrmResults.Form.Filter = Left(strWhere, lngLen)
  2. Me.subfrmResults.Form.FilterOn = True
I now have
Expand|Select|Wrap|Line Numbers
  1. DoCmd.OpenForm "frmWait"
  2. Me.subfrmResults.Form.RecordSource = "SELECT DISTINCT Query2.RecipeID, Query2.RecipeName, Query2.FoodCategory, Query2.SumOfIngredCost FROM Query2 WHERE " & (Left(strWhere, lngLen))
  3. DoCmd.Close acForm, "frmWait"
This works, but is slow, which is why I put in the command to open the form frmWait which tells the user to please wait.
Thanks very much for trying to help, this one sure strained the brain! I think even when you don't get the answer from a forum like this, it still helps anyway to look at things differently, or even just to see your problem laid out in plain text.
Mar 19 '07 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

7
4013
by: Kornelius Finkenbein | last post by:
Hello folks! I've got a strange problem with my download-script in conjunction with M$ internet explorer, if the filename I want to link to includes more than one points. In Netscape the problem doesn't exist. For example: input: ... download.php?name=virtualdub_1.4.9.zip
2
5464
by: CES | last post by:
All, I'm at a loss, the code below works in IE but not in Netscape and I'm unskilled enough not to know why. Essentially this code looks thru all of the form fields and if the input box has a attribute of disabled="true", attribute would only be present if the field was disabled, then the label field's class is changed to a grayed out color. --- The first problem seems to be with the v==true statement, I've tried
43
17749
by: JohnB | last post by:
i've noticed some sites have a custom icon in the address bar, next to the url. how can i do that? thanks. john b.
0
1250
by: Alvey Sidecast | last post by:
Well it is to me anyway. ------------------------------ Access Version = 2002 OS = XP Pro User = Ordinary to bumbling ------------------------------ I was writing some sql yesterday which required the user to input a variable number of values. So I wrote (much abbreviated);
7
6923
by: Dan H. | last post by:
Hello, What is the equivalent "string resource file" in c#? And, is there a way to create a beep in C#? Thanks, Dan
14
23486
by: I_AM_DON_AND_YOU? | last post by:
Hello all: Yesterday I put this post regarding the query as to how to open a new form in "modal" style. I am calling the form in the following way: dim frm as form2() frm = new form2() frm.show
12
2027
by: Sean | last post by:
I know I can pass a reference to a String into a Form by overloading the Constructor. Public Sub New(ByRef strDX As String) MyBase.New() InitializeComponent() End Sub
6
2583
by: Vincent Finn | last post by:
Hi, I am trying to store an object in the Session() I have a VB.Net control which is loaded in an asp page and then calls a function in my webservice (also written in VB.Net) The web service function has everything it seems to need the code is at the bottom of the mail
4
2906
by: Påhl Melin | last post by:
I have some problems using conversion operators in C++/CLI. In my project I have two ref class:es Signal and SignalMask and I have an conversion function in Signal to convert Signal:s to SignalMask:s. The reason is I have a free function called WaitSignal that accepts av SignalMask where Signals parameters are supposed to implicitly be converted to SignalMask:s. I'm using the SignalMask class because I want to be able to supply a logic...
10
10049
by: morangolds | last post by:
Hi, I've been having a problem with C++ Windows Forms apps not "ending" when you close the form window. I've searched about this problem all over the place and most searches have lead me to believe that this has to do with unreleased form component events or event handlers. I'm comparatively new to .net and windows forms, in the sense that though I've been using them for over 2 years now, it's been rather sporadic. I work with...
0
8969
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8788
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.