473,396 Members | 1,799 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,396 software developers and data experts.

Use operators in textbox for query

Hi guys,

I have a form with an unbound textbox. It provides the parameter for a number field in a query.
I'd like for the user to be able to enter their own operators (">2" for example) and this gets passed to the query and ran.

The query works perfectly when the number is by itself, however when any operators are entered it errors (too complex or incorrect punctuation). I have tried all the combinations of " ' & etc. i can think of to pass it to the query criteria but it errors without fail.

Any help would be greatly appreciated as I've wasted hours and hours on this through trial and error (error, error) and googling.
It's driving me insane!

Many thanks,

Dan
Sep 20 '08 #1
7 4428
Stewart Ross
2,545 Expert Mod 2GB
Hi. You can't do this by passing a value as a parameter to a query - the database engine has no facilities for interpreting user-supplied parameters in the way you describe, as an operator followed by a value. It can be done by building an SQL string in VBA code, but my advice to you would be not to go this route at all.

Allowing free-form entry which then has to be interpreted can be fraught with error. What would you expect to happen if the user enters something like "> 2 < 5" for instance? This could not be passed to any query even in code without being expanded into the form

>2 AND <5

In my opinion what you want to do is simply not worth it. If you need to provide choices for users over the type of comparison, you could do it by constraining the choices to predefined operators using radio buttons and then build the SQL string yourself - but in my view this is an incredibly complicated approach to take. I would honestly suggest that the return for your efforts will not match the time and cost to achieve it.

As a project to teach myself about elements of compiler design I have in the past written an expression evaluator (in the programming language Pascal) which could evaluate any arithmetic expression entered, including the use of symbolic variables in an expression - say something like

x^2 + y + z - x * y

I remember spending more than three months working on this project. I learnt a great deal about compiler design, and interpreting expressions by doing this - including how an apprently simple expression is far from easy to interpret if you consider all the possible errors that may arise (missing operators, incorrect order, non-allowable characters entered, and so on).

If you expect to be able to pass a simple expression string entered by a user to a query and get it to make sense to the SQL engine more than occasionally I think you are heading down a very rocky path indeed.

-Stewart
Sep 20 '08 #2
FishVal
2,653 Expert 2GB
Hello, gentlemen.

Actually, VBA has Eval() function which could make it an easy trick.

Example:
Expand|Select|Wrap|Line Numbers
  1. SELECT tbl.fld FROM tbl WHERE Eval(tbl.fld & Forms!frm!txbCriteria);
  2.  
Regards,
Fish
Sep 20 '08 #3
Stewart Ross
2,545 Expert Mod 2GB
Thanks, Fish. Eval in the WHERE clause as you have shown is indeed a good way to go - at least so long as the user uses the right syntax for the comparison. As you say it makes it an easy thing to accomplish. I remain unconvinced of its worth, for the reasons I outlined, but it is at least possible using Eval to try it out and see whether or not your users are ok with it.

-Stewart
Sep 20 '08 #4
ADezii
8,834 Expert 8TB
Thanks, Fish. Eval in the WHERE clause as you have shown is indeed a good way to go - at least so long as the user uses the right syntax for the comparison. As you say it makes it an easy thing to accomplish. I remain unconvinced of its worth, for the reasons I outlined, but it is at least possible using Eval to try it out and see whether or not your users are ok with it.

-Stewart
Hello Stewart & FishVal, if I may make a suggestion, the Expression to be Evaluated can be tested in the AfterUpdate() Event of the Text Box (txtCriteria) prior to execution, as in:
Expand|Select|Wrap|Line Numbers
  1. Private Sub txtCriteria_AfterUpdate()
  2. On Error GoTo Err_txtCriteria
  3. Dim strMsg As String
  4.  
  5. strMsg = "The Expression [" & Me![txtCriteria] & _
  6.          " ] cannot be Evaluated"
  7.  
  8. If IsNull(Me![txtCriteria]) Then Exit Sub
  9.  
  10. 'Will generate an Error if the Expression 
  11. 'cannot be evaluated
  12. Debug.Print Eval(Me![txtCriteria])
  13.  
  14. Exit_txtCriteria:
  15.   Exit Sub
  16.  
  17. Err_txtCriteria:
  18.   Me![txtCriteria] = "ERROR!"
  19.   MsgBox strMsg, vbExclamation, "Invalid Expression"
  20.   Resume Exit_txtCriteria
  21. End Sub
Expand|Select|Wrap|Line Numbers
  1. 'Will pass the Test and Evaluate to 30
  2. (((2 ^ 4) + 16 + 100 - 122) * 3)
  3.  
  4. 'Will generate an Error and display ERROR! in txtCriteria
  5. A * B / J
I was thinking basically to Trap any potential Evaluations prior to being used in an SQL Statement. What's your thought on this, guys?
Sep 21 '08 #5
Stewart Ross
2,545 Expert Mod 2GB
Many thanks ADezii. Using error checking from the After Update event combined with Fish's suggestion of use of Eval for the Where clause is as good a solution as there is to this particular problem. Eval resolves the issue of interpreting user criteria, and the error checking copes with the potential for malformed expressions.

Thank you both for your well-informed suggestions.

Dan, if you follow Fish and ADezii's posts you will indeed have a viable way to interpret user expressions in a way which does not fall down if a malformed expression is entered. I think this is a good way forward - and I honestly didn't think there was one when I responded in post 2; I stand corrected there!

-Stewart
Sep 21 '08 #6
Thankyou for the help Stuart, FishVal and Adezii.
I have a very simple and effective form set-up now to do the job.

Much appreciated,

Dan
Sep 21 '08 #7
ADezii
8,834 Expert 8TB
One final point as far as Evaluating Expressions is the Order of Precedence. You must know the priority of Operators when being used within more complex Expressions, unless you rely exclusively on parenthesis. A couple of cases in point will demonstrate this point. In my previous example:
Expand|Select|Wrap|Line Numbers
  1. (((2 ^ 4) + 16 + 100 - 122) * 3) will Evaluate to 30
whereas
Expand|Select|Wrap|Line Numbers
  1. 2 ^ 4 + 16 + 100 - 122 * 3 will Evaluate to -234
and
Expand|Select|Wrap|Line Numbers
  1. (24 - 16) ^ 4 will Evaluate to 4096
whereas
Expand|Select|Wrap|Line Numbers
  1. 24 - 16 ^ 4  will Evaluate to -65512 
Sep 21 '08 #8

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

Similar topics

2
by: Jeroen van vliet | last post by:
Hello How can i run a query from a mdb database and show it in a datagrid control when i put in a letter in a textbox? for instance. when i type the letter "A" in the textbox i want to run a...
14
by: greg | last post by:
Discussion is invited on the following proto-PEP. ------------------------------------------------------------- PEP ??? - Overloadable Boolean Operators...
1
by: Thats Me | last post by:
TMALSS: Task With maintenance of Access Database I did not develop, don't ask about non-commented code problems or where data dictionary is (LOL). Have table with Inventory data for all...
0
by: Paul Rees | last post by:
I am wondering if anyone has encountered a similar problem to the one that I am having. I have an ASP.NET page which has a table and a dropdownlist that was added at design time. The majority of...
2
by: Michael Glaesemann | last post by:
Just finding out about the tinterval type (thanks again, Alvaro!), I've been looking to see what operators and functions are already built-in to PostgreSQL that involve this type. I thought I'd...
6
by: Drum2001 | last post by:
I have a database where I need to query multiple items. Is it possible to run a query based on a textbox where the information is delimited by a comma. Example: Show me all names where...
1
by: saundap | last post by:
Hi, Access 2000, Windows 2000 Professional I have a form I've developed that is designed for those not familiar with access and its query tool. WIthin a subform in the form there is presented...
1
by: viral123 | last post by:
Hi all I am using Crystal report and I am running my report successfully for my query but I want to make my query for one specific date. I want to get the date value from the textbox value. ...
3
LoanB
by: LoanB | last post by:
Hi I'm looking to ensure that my textbox only accepts numbers .. AND ... mathematical operators + - . / *. an dmaybe a blank space I can ensure that only number are included by using: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...
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...

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.