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.

Query based on form parameters

Dear group,

Can anyone tell me how to do the following?

I have a basic list of numbers in a table

I want to be able to search these by letting the user type "<10" or
">=50" on a simple form that has a list box showing the results

I have tried creating a query that uses the criteria the user types on
the form – as below

SELECT tblLocations.ICEID, tblLocations.LectureRoomTheatre
FROM tblLocations
WHERE (((tblLocations.LectureRoomTheatre)=[Forms]![frmVenueSelect]![txtTSCap]));

The form has a free text box that you can type anything.

But I get an error "The expression is typed incorrectly or is too
complex" etc

If I type <10 directly into the query it works just fine.


Best regards

James
Nov 13 '05 #1
4 6252
> WHERE
(((tblLocations.LectureRoomTheatre)=[Forms]![frmVenueSelect]![txtTSCap]));

If you replace your parameter with the text from the textbox, you will get

1) WHERE (((tblLocations.LectureRoomTheatre)=<10 Or >=50));
or if one at a time,
2) WHERE (((tblLocations.LectureRoomTheatre)=>=50));

Access will accept <= or =<, but not =>=. Also, in the first one it should
be
WHERE (((tblLocations.LectureRoomTheatre)=<10 Or
(tblLocations.LectureRoomTheatre)>=50));

If you are going to do this, the best thing may be to rewrite the SQL of the
query, concatenating in the text from the textbox.

CurrentDb.QueryDefs("MyQuery").SQL = "SELECT tblLocations.ICEID,
tblLocations.LectureRoomTheatre FROM tblLocations WHERE
(((tblLocations.LectureRoomTheatre)" & [Forms]![frmVenueSelect]![txtTSCap] &
"));"

This will work for a single item, such as in #2 above. It wil get more
complicated, but the basic idea remains the same, if you choose to allow
Ands and Ors.

--
Wayne Morgan
MS Access MVP
"James" <ja***********@hotmail.com> wrote in message
news:c8*************************@posting.google.co m...
Dear group,

Can anyone tell me how to do the following?

I have a basic list of numbers in a table

I want to be able to search these by letting the user type "<10" or
">=50" on a simple form that has a list box showing the results

I have tried creating a query that uses the criteria the user types on
the form - as below

SELECT tblLocations.ICEID, tblLocations.LectureRoomTheatre
FROM tblLocations
WHERE (((tblLocations.LectureRoomTheatre)=[Forms]![frmVenueSelect]![txtTSCap]));
The form has a free text box that you can type anything.

But I get an error "The expression is typed incorrectly or is too
complex" etc

If I type <10 directly into the query it works just fine.


Best regards

James

Nov 13 '05 #2
Wayne,

Thanks for the reply – still a bit stuck.

I replaced the query with the exact SQL you suggested – I got a nasty
error about syntax.

Then I made a slight change to this to make it work – I took out the
quotes.

SELECT tblLocations.ICEID, tblLocations.LectureRoomTheatre,
([tblLocations].[LectureRoomTheatre]) &
[Forms]![frmVenueSelect]![txtTSCap]
FROM tblLocations
WHERE (((([tblLocations].[LectureRoomTheatre]) &
[Forms]![frmVenueSelect]![txtTSCap])));

But the result I get is as follows:-

ICEID LectureRoomTheatre Expr1
6249 10 10>1
6734 2 2>1
7431 0 0>1
7432 0 0>1
7433 0 0>1
7434 0 0>1
Etc.

When I look at the SQL it has been changed to the following?!?!?!??!

SELECT tblLocations.ICEID, tblLocations.LectureRoomTheatre,
([tblLocations].[LectureRoomTheatre]) &
[Forms]![frmVenueSelect]![txtTSCap] AS Expr1
FROM tblLocations
WHERE (((([tblLocations].[LectureRoomTheatre]) &
[Forms]![frmVenueSelect]![txtTSCap])<>False));

What am I doing wrong?
James

Nov 13 '05 #3
You can't replace the SQL with what I suggested by placing it directly in
the SQL view of the query. What I suggested was doing this in code, which
would have concatenated the value of the textbox into the SQL statement.

--
Wayne Morgan
MS Access MVP
"James" <ja***********@hotmail.com> wrote in message
news:c8**************************@posting.google.c om...
Wayne,

Thanks for the reply - still a bit stuck.

I replaced the query with the exact SQL you suggested - I got a nasty
error about syntax.

Then I made a slight change to this to make it work - I took out the
quotes.

SELECT tblLocations.ICEID, tblLocations.LectureRoomTheatre,
([tblLocations].[LectureRoomTheatre]) &
[Forms]![frmVenueSelect]![txtTSCap]
FROM tblLocations
WHERE (((([tblLocations].[LectureRoomTheatre]) &
[Forms]![frmVenueSelect]![txtTSCap])));

But the result I get is as follows:-

ICEID LectureRoomTheatre Expr1
6249 10 10>1
6734 2 2>1
7431 0 0>1
7432 0 0>1
7433 0 0>1
7434 0 0>1
Etc.

When I look at the SQL it has been changed to the following?!?!?!??!

SELECT tblLocations.ICEID, tblLocations.LectureRoomTheatre,
([tblLocations].[LectureRoomTheatre]) &
[Forms]![frmVenueSelect]![txtTSCap] AS Expr1
FROM tblLocations
WHERE (((([tblLocations].[LectureRoomTheatre]) &
[Forms]![frmVenueSelect]![txtTSCap])<>False));

What am I doing wrong?
James

Nov 13 '05 #4
This example uses minimum and maximum dates which is the same idea as mimimum
and maximum numbers.

The free downloadable sample database at www.bullschmidt.com/access uses the
query by form concept so that on the invoices dialog one can optionally choose
a rep, a customer, and perhaps a date range, click on a button that says
"Input," and then have the invoice form open up showing all the invoices that
match the criteria.

And here is how the query by form concept can work.

On the invoices dialog there are the following controls:
InvDateMin with DefaultValue of =DateSerial(Year(Date())-1,1,1)
InvDateMax with DefaultValue of =Date()
InvRepNum with DefaultValue of *
InvCustNum with DefaultValue of *

Also on the invoices dialog there is a command button called cmdInput to open
the invoices form with the following code behind the OnClick property:
DoCmd.OpenForm "frmInv"

And of course there could be a button to open a report the same way:
DoCmd.OpenReport "rptInv", acViewPreview

The invoices form (frmInv) has RecordSource property of qryInv.

And the qryInv query's criteria for the InvDate field has:
Between [Forms]![frmInvDialog]![InvDateMin] And
[Forms]![frmInvDialog]![InvDateMax]

And the qryInv query's criteria for the RepNum field has:
Like [Forms]![frmInvDialog]![InvRepNum]

And the qryInv query's criteria for the CustNum field has:
Like [Forms]![frmInvDialog]![CustNum]

One related point is that you probably wouldn't want to allow blanks (i.e.
Nulls) in fields that are going to be used with Like in any criteria for that
field. Otherwise the blanks wouldn't be shown.

For example (based on what is entered into a last name search field):

Like 'Smith' would show Smith records

Like '' would show no records (probably not what one would want)

Like '*' would show all records

And to counter that I like to have the search fields have a DefaultValue of *
and not allow the search fields to be blank.

Or a more complicated solution would be to create the query's SQL statement
dynamically so that the criteria on a particular field isn't used unless
needed.

For example:

' Set strSQL.
strSQL = "SELECT * FROM MyTable WHERE (1=1)"
If Not IsNull(Rep) Then
strSQL = strSQL & " AND (Rep='" & Rep & "')"
End If
If Not IsNull(Customer) Then
strSQL = strSQL & " AND (Customer='" & Customer & "')"
End If

Best regards,
J. Paul Schmidt, Freelance Access and ASP Web Developer
www.Bullschmidt.com
Classic ASP Design Tips, ASP Web Database Demo, ASP Bar Chart Tool...
Nov 13 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: JDJones | last post by:
Using PHP and MySQL. Trying to put a list of categories into a drop down select option of a form like: <form name="form" action="<? print $_SERVER?>" method="get"> <select name="subject">...
1
by: Matthew | last post by:
Hey, I have built a form that has certain combo and text boxes on it, which a user specifies his criteria and then clicks on a search button to run a query based on that criteria. To build to...
3
by: dskillingstad | last post by:
I'd appreciate any help I can get. I'm not sure what I'm doing wrong, but.... I've searched these groups for some solutions but no luck. I have an unbound form (frmSearch), with several unbound...
7
by: J-P-W | last post by:
Hi, in the past I've spent ages writing VB routines to get around this problem, is there a simple way? I have three tables for a membership system: tblMembership - MembershipNumber; Names etc...
4
by: roryok | last post by:
Hi. (Access 2002 - Windows XP Pro) I have a select query called "Find Suppliers". One of its fields is "supplierName" which is derived from a linked "Suppliers" table. I have a form called...
5
by: Ferasse | last post by:
Hi, I'm an occasional Ms-Access developer, so there is still a lot of stuff that I don't get... Right now, I'm working on a database that stores contractual information. One of the form that...
1
by: samtymom | last post by:
I have a database that is used for Salvage Vehicles. (At present I am using 2002, but will be moving to 2007 shortly.) Presently there is a Switchboard that opens the Form View of the Salvage...
4
by: Joe | last post by:
This is my HTML form: <form method=get action="home.php"> <INPUT TYPE = "Text" VALUE = "" NAME = "title"><br> <select name="searchby"> <option value="Title">Title</option> <option...
1
by: Karen D | last post by:
Help Again!! I’m using Access 2003 and I have a form that allows users to enter criteria for selecting tables and queries as well as the query parameters that will be used to generate a report. The...
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...
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
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
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
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,...

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.