473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Passing Complex? Criteria From A Form Variable to a Query

I have an MS access Database (2000) and Have created a form that sets a variable to a value I would like (Based on other selections in the form) to pass to my query criteria and I get the following results.

If the Form Variable = "10" - everything works fine
If the Form Variable = "Between 10 And 12" - it tells me the criteria is too complex
If I paste the exact text from the variable ie "Between 10 And 12" into the query it works fine

Is this a limitation that means I need to create the whole SQL select statement from code ?

Thanks in advance.
If I need to create the SQL code I will use MS Examples http://support.microsoft.com/kb/136062 to save anyone retyping how to do it here....unless you can advise of a better method..
Sep 11 '07 #1
5 5444
MMcCarthy
14,534 Expert Mod 8TB
Two things ...

First is the variable a string or a number data type and is the field its checking a text or a number?

Secondly can we see how you are passing this value to the query. If you are using VBA can we see the code.
Sep 13 '07 #2
Thanks for the response
Following are the details of my code etc.

NB: The criteria is being used on a number field of the query

I have a form with an option group that updates the value of a text box on change of selection via VBA code as follows:
Private Sub VarCustomGrp_AfterUpdate()
If VarCustomGrp.Value = "1" Then
VarCustFilter = "10"
Else:
If VarCustomGrp.Value = "2" Then
VarCustFilter = "Between 14 And 30"
Else:
VarCustFilter = "Between 0 And 30"
End If
End If
End Sub
I then use the following detail in the query criteria to use th value of the text box:
[Forms]![MainReport]![VarCustFilter]
If I select option 1 and the Text Box value is 10 my query qorks
If I select option 2 or 3 I get the following error:
"The expression is typed incorrectly, or it is too complex to be evaluated. For example, a numeric expression may containtoo many complicated elements. Try simplifying the expression by assigning parts of the expressions to variablee"
If I copy and paste the value from the text box in question when option 2 or 3 are selected and past that value into the query instead of using the pointer to the text box it works fine.

Therefore It seems that using a variable in the criteria of the query cannot contain anything but the value to be used as part of an expression and passing an expression (even a simple one) is not possible via this method.

I realise I could assign the value of 2 variables and create an expression in the criteria that uses the 2 variables (as basic values only) (eg "Between [Forms]![MainReport]![VarCustFilter1] And [Forms]![MainReport]![VarCustFilter2]") but I need to use the same method for another selection group that is more complex and may contain multiple between statements.

What I cant understand is why the more complex value is not accepted..it does not make logical sense..(to me...anyway)

I am hoping that I may have done something wrong, or there is another simple method to do what I desire but I have a feeling it is a software limitation that means I need to write the selest SQL statment myself, which currently I have no experience with and will make it hader for me to experiment so I would like to avoid this for the timebeing.

Again thanks for the response. I look forward to futher advice if possible.

PS Incase it helps here is the SQL of my Query. The criteria causing the problem is at the end in bold.
SELECT tlkpStkPurchHist.tryear, tlkpStkPurchHist.trmonth, tlkpStkPurchHist.trdate, tlkpStkPurchHist.ref, tlkpStkPurchHist.type, tlkpDrMaster.name, tlkpDrMaster.type, tlkpStkPurchHist.areaCode, tlkpStkPurchHist.stkcode, tlkpStkPurchHist.stkDesc, tlkpStkPurchHist.per, tlkpStkMaster.Category, tlkpStkMaster.catDesc, tlkpStkPurchHist.qty, tlkpStkPurchHist.netCost, tlkpStkPurchHist.sellPrice, tlkpStkPurchHist.netVal, tlkpStkPurchHist.Desc2, tlkpStkPurchHist.CustRef, tlkpStkPurchHist.OrderNo, tlkpStkPurchHist.LastCost, tlkpStkPurchHist.AveCost, tlkpStkPurchHist.drmst, tlkpStkPurchHist.crmst, tlkpStkPurchHist.drCode, tlkpStkPurchHist.crCode, tlkpStkPurchHist.netCost_signed, tlkpStkPurchHist.qty_moved
FROM tlkpStkMaster INNER JOIN (tlkpDrMaster INNER JOIN tlkpStkPurchHist ON tlkpDrMaster.drcode = tlkpStkPurchHist.drCode) ON tlkpStkMaster.stkcode = tlkpStkPurchHist.stkcode
WHERE (((tlkpStkPurchHist.tryear)=[Forms]![MainReport]![VarYear]) AND ((tlkpStkPurchHist.trmonth)=[Forms]![MainReport]![VarMonth]) AND ((tlkpStkPurchHist.trdate)>#1/1/2003#) AND ((tlkpStkPurchHist.type)="i" Or (tlkpStkPurchHist.type)="c") AND ((tlkpDrMaster.type)=[Forms]![MainReport]![VarCustFilter]));
Sep 14 '07 #3
MMcCarthy
14,534 Expert Mod 8TB
OK your query won't work on "Between 14 And 30" because it would equate to "=Between 14 And 30" which doesn't make any sense. Try this instead.

Create a query or use your existing one. The contents won't matter as the following code will replace them. For this exercise I will call it query1 but change that to the name of your query.

Now change your code as follows:

Expand|Select|Wrap|Line Numbers
  1. Private Sub VarCustomGrp_AfterUpdate()
  2. Dim db As DAO.database
  3. Dim qdf As QueryDef
  4. Dim strQuery As String
  5.  
  6.  
  7.     Set db = CurrentDb
  8.     Set qdf = db.QueryDefs("Query10")
  9.  
  10.     strQuery = "SELECT tlkpStkPurchHist.tryear, tlkpStkPurchHist.trmonth, " & _
  11.         "tlkpStkPurchHist.trdate, tlkpStkPurchHist.ref, tlkpStkPurchHist.type, " & _
  12.         "tlkpDrMaster.name, tlkpDrMaster.type, tlkpStkPurchHist.areaCode, " & _
  13.         "tlkpStkPurchHist.stkcode, tlkpStkPurchHist.stkDesc, tlkpStkPurchHist.per, " & _
  14.         "tlkpStkMaster.Category, tlkpStkMaster.catDesc, tlkpStkPurchHist.qty, " & _
  15.         "tlkpStkPurchHist.netCost, tlkpStkPurchHist.sellPrice, tlkpStkPurchHist.netVal, " & _
  16.         "tlkpStkPurchHist.Desc2, tlkpStkPurchHist.CustRef, tlkpStkPurchHist.OrderNo, " & _
  17.         "tlkpStkPurchHist.LastCost, tlkpStkPurchHist.AveCost, tlkpStkPurchHist.drmst, " & _
  18.         "tlkpStkPurchHist.crmst, tlkpStkPurchHist.drCode, tlkpStkPurchHist.crCode, " & _
  19.         "tlkpStkPurchHist.netCost_signed, tlkpStkPurchHist.qty_moved " & _
  20.         "FROM tlkpStkMaster INNER JOIN (tlkpDrMaster INNER JOIN tlkpStkPurchHist " & _
  21.         "ON tlkpDrMaster.drcode = tlkpStkPurchHist.drCode) " & _
  22.         "ON tlkpStkMaster.stkcode = tlkpStkPurchHist.stkcode " & _
  23.         "WHERE (((tlkpStkPurchHist.tryear)=[Forms]![MainReport]![VarYear]) " & _
  24.         "AND ((tlkpStkPurchHist.trmonth)=[Forms]![MainReport]![VarMonth]) " & _
  25.         "AND ((tlkpStkPurchHist.trdate)>#1/1/2003#) " & _
  26.         "AND ((tlkpStkPurchHist.type)='i' Or (tlkpStkPurchHist.type)='c')"
  27.  
  28.     strQuery = strQuery & " AND ((tlkpDrMaster.type)"
  29.  
  30.     If Me!VarCustomGrp.Value = "1" Then
  31.         strQuery = strQuery & "=10"
  32.     ElseIf VarCustomGrp.Value = "2" Then
  33.         strQuery = strQuery & " Between 14 And 30"
  34.     Else
  35.         strQuery = strQuery & " Between 0 And 30"
  36.     End If
  37.  
  38.     strQuery = strQuery & "))"
  39.     Debug.Print strQuery
  40.     qdf.SQL = strQuery
  41.  
  42.     Set qdf = Nothing
  43.     Set db = Nothing
  44.  
  45. End Sub
  46.  
Sep 14 '07 #4
Great Help.

Works perfectly except for the typo on making a query called Query1 when you call it Query10 in the code.

I will use this to consolidate several option group choices when I click on the report button rather than updating a vaue with each change of an option group.


Thank you very much for your assitance.
Sep 19 '07 #5
MMcCarthy
14,534 Expert Mod 8TB
Great Help.

Works perfectly except for the typo on making a query called Query1 when you call it Query10 in the code.

I will use this to consolidate several option group choices when I click on the report button rather than updating a vaue with each change of an option group.


Thank you very much for your assitance.
Glad its working for you. Sorry for the typo.

Mary
Sep 19 '07 #6

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

Similar topics

3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
2
by: Matthew Clement | last post by:
I'm currently building a form (called frmReports) to set the criteria for a query, but I'm having some trouble with syntax and hope that one of the guru's here can help me achieve what I'm do. ...
2
by: MX1 | last post by:
HELP! I have a query that gets a few values from a form. The problem I'm having is a date field in the query. When I put the value in the criteria, it works fine. When I put the same value as...
3
by: MX1 | last post by:
I'm ready to pull the hair out of my head. I have a query with a couple of parameters that I want to get from combo boxes on a form. One parameter is a date with a dynamically calculated year and...
4
by: Regnab | last post by:
How can I pass a variable (Dim Property As String) to the query criteria? I figure I could do it the long way and send it to a form and have the query access it from there but I figure there has...
8
by: Steve Jorgensen | last post by:
Mailing List management is a good example of a case where my conundrum arises. Say there is a m-m relationship between parties and groups - anyone can be a member of any combintation of groups. ...
3
by: Steven J. Reed | last post by:
I must be missing something, because this should be easy to do. After the user has selected several options on a page, I do a database query which returns a few records that I want to pass to...
1
by: rfr | last post by:
I have a need to use a single version of a Visitor Response Feedback Form on numerous HTML documents. Rather than have numerous versions of this, one on each HTML document, it makes more sense to...
11
by: Purdue02 | last post by:
I am trying to pass a global variable to criteria in a query using the below code, but the query is returning no results. I have the function ReturnStrCriteria() included in the query's criteria....
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.