Microsoft Access - Combo Box - Query

Asked By Nikhil Patil on 13-Apr-11 06:57 AM
Hello,

I want to build a query which uses input from a combo box having the foll. operators as options:
= , >=, <=, > and <. Now, one way of doing it is to build different queries for each of the operators, but that would be an inefficient way of doing it.

How do I connect these different options to a single query?

Thank you for your time,

Nikhil
TSN ... replied to Nikhil Patil on 13-Apr-11 07:08 AM
Hi...

You can have a single Query like this
Here i am selecting the Combox and taling the salary value from another text box...
  string Query = "select * from Emp where salary" + Combobox.Selectedvalue + " " + txtName.Text;

Hope this helps You........
kiran k replied to Nikhil Patil on 13-Apr-11 07:17 AM
 on your combo box you can give the option as
 <asp:dropdown id="dd" runat="Sever">
 <asp:itemlist value="="> =</itemlist>
.... , >=, <=, > and <.

 like this you can create drop down box user can select that and base one the selected value you can run the query (pass parameter)

 sql ="select * from user where sal '"+dd.selectedValue +"' 100 "

 here user salary will be retrieve base on the user drop down value
Jitendra Faye replied to Nikhil Patil on 13-Apr-11 08:54 AM
Based on your selection in ComboBox control, you can add operator in query,

 you can build query like this- 

 "select col1,col2 from tablename where colname " + ComboBox1.Text + "  value ";

I hope this will help you.
Nikhil Patil replied to TSN ... on 13-Apr-11 09:51 AM
This is what I am using currently for >=, similarly for < and so on..

SELECT List.Feld3, List.Feld7, Mid([Feld3],((InStr(1,[Feld3],"/"))-3),3) AS Expr1
FROM List
WHERE (((Mid([Feld3],((InStr(1,[Feld3],"/"))-3),3))>=[Forms]![Filter]![Text23]) AND ((InStr(1,[Feld3],"/"))>3))
ORDER BY Mid([Feld3],((InStr(1,[Feld3],"/"))-3),3);

Now, as you suggested I tried the following:

SELECT List.Feld3, List.Feld7, Mid([Feld3],((InStr(1,[Feld3],"/"))-3),3) AS Expr1
FROM List
WHERE (((Mid([Feld3],((InStr(1,[Feld3],"/"))-3),3))=[Forms]![Filter]![cmb_tw_sel].[Text]+[Forms]![Filter]![Text23]) AND ((InStr(1,[Feld3],"/"))>3))
ORDER BY Mid([Feld3],((InStr(1,[Feld3],"/"))-3),3);

However, this is not working. Can you suggest me the correct syntax?

thanks,
Nikhil Patil replied to kiran k on 13-Apr-11 09:55 AM

Where exactly are these options located in the property sheet of the combo box?

I do not understand what you mean by :
<asp:dropdown id="dd" runat="Sever">
 <asp:itemlist value="="> =</itemlist>
.... , >=, <=, > and <.

Pat Hartman replied to Nikhil Patil on 13-Apr-11 01:11 PM
Relational operators are part of the structure of the query and cannot be changed on the fly.  Querydefs are "compiled" when they are saved and part of that process includes producing a plan for how to produce the requested results and the relational operator may have an influence on that decision.

The only way to have variable operators, is to build the queries in VBA as a string and then either create a querydef with it or execute it.

With a single query, you can accommodate =, between, >=, and <=.  By using a single value at both ends of the criteria.

Where somefield >= Forms!yourform!Field1 And somefield <= Forms!yourform!Field2

Before you run the query copy field 1 to field 2 if field2 is empty. 
Nikhil Patil replied to Pat Hartman on 13-Apr-11 07:19 PM
Alright. Thanks, I get the idea. I will implement it using If...Else...

Now I tried using SQL statements in VB instead of running the Queries built in the design view.

I used the following but I am getting a run time error 13.

Dim strquery_tw as String

strquery_tw ="SELECT List.Feld3, List.Feld7 FROM List WHERE (((Mid([Feld3],((InStr(1,[Feld3],"/"))-3),3))=[Forms]![Filter]![Text19]) AND ((InStr(1,[Feld3],"/"))>3));"  // in a single line

DoCmd.RunSQL strquery_tw

why am I getting the run time error. ?

Thanks,





Pat Hartman replied to Nikhil Patil on 13-Apr-11 10:56 PM
Strings must be enclosed in single or double quotes.

strquery_tw ="SELECT List.Feld3, List.Feld7 FROM List WHERE (((Mid([Feld3],((InStr(1,[Feld3],"/"))-3),3))= """ & [Forms]![Filter]![Text19]) & """ AND ((InStr(1,[Feld3],"/"))>3));"

PS - Only use single quotes if you are absolutely certain that the string will never contain a single quote.
Nikhil Patil replied to Pat Hartman on 14-Apr-11 09:10 AM

I tried the following string but I am still getting an error sometimes and whenever I open the form, it asks me for a value for the Forms!Filter!Text19. Also, I tried putting ( ) around [Forms]![Filter]![Text19], but it does not make any difference. Can  you help me out ? Also, when it runs. I get a warning saying that the query is too complex.

Also, I was curious to know if it is okay to use the SQL by creating a query in the design form and then just copy paste the query in the VBA window. Do I need to delete the query once I have used its SQL in my code?

Sorry but I am getting distressed with this unpredictable behavior of Access...

Thanks,
Pat Hartman replied to Nikhil Patil on 14-Apr-11 05:04 PM
This is not a bug.  The syntax of the query is incorrect.  Stop the code at the statement after the one that builds the string.  Print the string in the immediate window and copy it and paste it into the SQL window.  That should make it easier to find the syntax error.

There is no reason that this query needs to be build in your code.  You could use a saved querydef that takes a parameter at runtime.  When your back end is Jet/ACE, using querydefs rather than dynamic SQL is the superior method since it is faster and doesn't produce database bloat.
Nikhil Patil replied to Pat Hartman on 15-Apr-11 08:01 AM
Do you have any online reference for creating QueryDefs? I looked through the Access 2007 Bible book, but I could not find anything worthwhile.

Thanks,
Pat Hartman replied to Nikhil Patil on 15-Apr-11 03:47 PM
Querydefs are stored queries.  You can use the graphical interface to build them or SQL view if you like typing.  In most cases you can switch back and forth between views but there are some constructs that the GUI does not support such as Union queries, pass-through queries, and queries that use non-equi joins.  I generally use design view to build the basic select clause because I don't like to type and I don't want to have to remember every column and table name.  Once the select part is built, I may switch to SQL view if the selection criteria is complex.  If it is simple, I stick with the designer and never switch views.