473,503 Members | 1,648 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

query by form

I am trying to build a form that has certain criteria on it (combo
boxes) which a user can pick from a range of values to specify the
criteria he wants when he runs a query on the form. I am trying out
different ways to do this, but nothing is working. Does anyone know
the easiest way to do this?
Nov 13 '05 #1
3 6367
It would be pretentious to say that this is the *best* way, but here's a
simplified example:

Two comboboxes, with:
cboCity.rowsource = "SELECT city FROM lu_city_table"
cboState.rowsource = "SELECT state FROM lu_state_table"

When you click your query button:

lstResults.rowsource = "SELECT add_house, add_street, add_apt, add_city,
add_state, add_zip FROM address WHERE add_city = ' " & Me.cboCity & " ' AND
add_state = ' " & Me.cboState & " ' "

lstResults.requery
Darryl Kerkeslager
"Matthew" <ma************@eaton.com> wrote in message
news:7f**************************@posting.google.c om...
I am trying to build a form that has certain criteria on it (combo
boxes) which a user can pick from a range of values to specify the
criteria he wants when he runs a query on the form. I am trying out
different ways to do this, but nothing is working. Does anyone know
the easiest way to do this?

Nov 13 '05 #2
I'm not sure if you are trying to write a Query By Form solution.

If you are, I have seen a good textbook solution. It used checkboxes
down the left side of the form and choices to the right of them.

For example, to the right of checkbox 1, you might have a combo box
where the user can select a single value. To the right of checkbox 2,
you might have two textboxes where the user can enter a From date and
a To date.

By default, the checkboxes down the left are enabled and the choices
down the right are disabled.

When the user selects a checkbox, its After_Update event handler
enables the choice to its right, so the user can now make a selection
or enter a value.

And vice versa, if the user then deselects the checkbox, the
After_Update event handler disables the choice to the right.

So the After_Update event handler would contain code like:
Me.cboChoice1.Enabled = (Me.chkChoice1=True)
or (same thing):
Me.cboChoice1.Enabled = Me.chkChoice1
or
Me.txtFromDate.Enabled = Me.chkChoice2
Me.txtToDate.Enabled = Me.chkChoice2

Presumably, you have a command button on the form to signal the user
has made all necessary choices. This button's Click event handler
should check which checkboxes are true and build an SQL statement
accordingly from the appropriate combo selections or textbox entries.
Then you can run or use the SQL statement.

There a numerous possibilities, but in principle that's how it could
be done.

As I said at the beginning, I realise your question could be
interpreted at meaning something quite different, but hope this was
your intended meaning.

Geoff

"Matthew" <ma************@eaton.com> wrote in message
news:7f**************************@posting.google.c om...
I am trying to build a form that has certain criteria on it (combo
boxes) which a user can pick from a range of values to specify the
criteria he wants when he runs a query on the form. I am trying out
different ways to do this, but nothing is working. Does anyone know
the easiest way to do this?

Nov 13 '05 #3
If there are several criteria and they are not always used, I like to
construct a Where string and then use that string in opening the query, form
or report for which the criteria apply. I usually use a user-defined
function like GeteWhereString() to return the string:

-----------------

GetWhereString() as String

Dim strWhere as String

strWhere = vbNullString

' FIRST NAME
If Not IsNull(Me!txtPersFirstName) Then
strWhere = strWhere & " AND [PersFirstName] Like " & Chr(34) &
Me![txtPersFirstName] & Chr(34)
End If

' LAST NAME
If Not IsNull(Me!txtPersLastName) Then
strWhere = strWhere & " AND [PersLastName] Like " & Chr(34) &
Me![txtPersLastName] & Chr(34)
End If

' STREET NAME
If Not IsNull(Me!txtPersStreetAddress) Then
strWhere = strWhere & " AND [PersStreetAddress] Like " & Chr(34)
& Me![txtPersStreetAddress] & Chr(34)
End If

' CITY
If Not IsNull(Me!txtPersCity) Then
strWhere = strWhere & " AND [PersCity] Like " & Chr(34) &
Me![txtPersCity] & Chr(34)
End If

If strWhere <> vbNullString then strWhere = Mid(strWhere,6) ' remove
initial " AND "

GetWhereString = strWhere

--------------

In the example above, if the user supplies criteria for any of the items
listed, they will be included in the Where string. If the user leaves some
out (no First Name, for example) the Where string simply ignores that field.
Using "Like" enables the user to use wild cards; the Chr(34)s are just text
delimiters.

This would be used in opening a form like this, assuming the form is
based on a table or query that includes the required fields.

DoCmd.OpenForm "MyForm", , , GetWhereString

If you want to use it with a query, you have to modify the querydef's
SQL property, so it's probably simpler to create a separate form in
datasheet view and open that.

"Matthew" <ma************@eaton.com> wrote in message
news:7f**************************@posting.google.c om...
I am trying to build a form that has certain criteria on it (combo
boxes) which a user can pick from a range of values to specify the
criteria he wants when he runs a query on the form. I am trying out
different ways to do this, but nothing is working. Does anyone know
the easiest way to do this?

Nov 13 '05 #4

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

Similar topics

3
5372
by: Harvey | last post by:
Hi, I try to write an asp query form that lets client search any text-string and display all pages in my web server that contain the text. I have IIS 6.0 on a server 2003. The MSDN site says...
3
28996
by: MX1 | last post by:
I have a query written in MS Access that has a few calculated fields. Is it possible to refer to that query in a form field. I'd like the form field to show the sum of one of the columns from the...
3
4967
by: Steve | last post by:
Form FrmRestock's recordsource is QryFrmRestock. The TransactionDate field's criteria is set ats: Forms!FrmRestock!LastXDays. LastXDays on the form is a combobox where the selections are 30, 60...
1
3075
by: Nicolae Fieraru | last post by:
Hi All, I want to find if there is a different way than the way I am working now. Lets say I have a table, tblCustomers containing address details. I want a report with all the customers from...
6
17139
by: Brian | last post by:
Hello, Basically, I'm running a query on a form's activation, and I'd like to have the results of the query be placed into other fields on the same form automatically. Does anybody know how...
13
4201
by: Lee | last post by:
Hello All, First of all I would like to say thank you for all of the help I have received here. I have been teaching myself Access for about 4 years now and I've always been able to find a...
3
3499
by: rhobson2 | last post by:
Hello, I wrote a database applicaiton using Access XP (2002) and everything has been working good for the client until they purchased a couple of new computers with Access 2003. The meetings...
1
2662
by: bgreenspan | last post by:
Hi Everyone, I'm back for some more expert help. Here's what I am doing and what I tried. My database has entries with Contract Names and Expiry Dates, among other fields. I have a form...
3
2543
by: pbd22 | last post by:
Hi. I need some help with structuring my query strings. I have a form with a search bar and some links. Each link is a search type (such as "community"). The HREF for the link's anchor looks...
2
1951
by: lindabaldwin | last post by:
Hello everyone, I am fairly new to VBA. I have a worksheet in Excel, named "Data Sheet" from which I am trying to query data. This worksheet contains the following data: unit (column A), date...
0
7198
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
7072
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
7271
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,...
1
6979
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
5570
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,...
1
4998
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...
0
1498
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
730
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
373
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...

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.