Using Combo box to search from 6 columns? Is it possible?

E

Evoke

Im new to access and created a database a while ago, which looking back at
it, itsn't the best way and is becoming a huge nightmare when something needs
to be added or changed.

I have a table, that stores client details - name, dob, suburb, licences,
notes, jobchoice1, jobchoice 2, job choice3, jobchoice4, jobchoice 5 and
jobchoice 6.

There is another table that lists 80 different jobs. When a user fills in
the form they can select up to 6 different job choices the client is looking
for.

At the moment, i have 80 queries - one for each job, that only shows
results. In the criteria section i have put in "Receptionist" on each 6
columns so only clients that have that job somewhere in the 6 choices appears.
I then have 80 different reports.

This is the nightmare when something needs to be changed - the 80 queries
and 80 reports need to be altered.

What i would like to do - if possible, is have a form, that has a
combo/drop down box that lists all the jobs (from the job table). And they
press a 'Run Query' button or something and below it (preferably) the query
displays (like a datasheet).

Can i have just one query and just one report? And it filters that
select job to only show records that contain that word in the 6 columns?

thank you for any help!
i've searched and searched on the internet and haven't found anything helpful.
As i said, im new to access, i dont know macros, or sql very well.

thanks.
 
P

pietlinden

Im new to access and created a database a while ago, which looking back at
it, itsn't the best way and is becoming a huge nightmare when something needs
to be added or changed.

I have a table, that stores client details - name, dob, suburb, licences,
notes, jobchoice1, jobchoice 2, job choice3, jobchoice4, jobchoice 5 and
jobchoice 6.

There is another table that lists 80 different jobs. When a user fills in
the form they can select up to 6 different job choices the client is looking
for.

At the moment, i have 80 queries - one for each job, that only shows
results. In the criteria section i have put in "Receptionist" on each 6
columns so only clients that have that job somewhere in the 6 choices appears.
I then have 80 different reports.

This is the nightmare when something needs to be changed - the 80 queries
and 80 reports need to be altered.

What i would like to do - if possible,  is have a form, that has a
combo/drop down box that lists all the jobs (from the job table). And they
press a 'Run Query' button or something and below it (preferably) the query
displays (like a datasheet).

Can i have just   one   query and just  one  report? And it filters that
select job to only show records that contain that word in the 6 columns?

thank you for any help!
i've searched and searched on the internet and haven't found anything helpful.
As i said, im new to access, i dont know macros, or sql very well.

thanks.

You're gonna hate me when I say this, but the easiest answer in the
long run is to normalize. ANY time you have a series of fields like
Choice1, Choice2... Choice[n], it should be a dead giveaway that you
are looking at a non-normalized database. Great. Why normalize? One
simple reason - you can query a normalized database and get the right
answer, and the SQL statements are generally small. If you try it the
other way, it's a nightmare. I know. I did it 8+ hours a day for six
months.

You can create union queries... I have written VBA functions that did
this... to loop through the fields in a table and create a sick union
query so that I could query the result as if it were designed
properly...

Painful news, I'm sure, but if you design the database correctly,
everything else becomes easier (and recyclable!).
 
C

Cheese_whiz

Hi Evoke,

First, I strongly agree with pietlinden: you should bite the bullet and
normalize your data. You are likely to run into issues in the future and if
not you, someone else. Non-normalized data shows up in all kinds of ways in
terms of problems, like this one, that wouldn't exist if your data was
properly normalized.

That said, you CAN do what you want. I can tell you basically what to do,
but since I don't have enough info about the data types stored in your query,
I can't say exactly. You'll have to adapt the idea.

**BACKUP YOUR DATABASE**

***I am going to completely ignore your table with the job choices and
instead of using a combo box on the form we make, use a text box. You will
eventually want a combo box based on that table with 80 jobs. Here's the
key, though. When you test this out using the text box, make sure the value
you enter in the text box reflects the values stored in teh job fields in the
query. If the values in the job fields are the names of jobs (receptionist,
plumber, attorney, etc), then that's what you type in the text box on teh
form. If they are numbers. then you type the number of the job. If those
values ARE numbers, don't worry. When you convert the text box to a combo
box and base it on your other table, it will all work out.

Those two issues aside, here you go:

1. Make a new query and base it on the table that has the client details
(name, dob, etc, jobChoice1, jobChoice2 etc...that table). Pull down all the
fields you want to use in your report, including all the job fields.

2. In the 'criteria' row under field jobChoice1, add the following:

Like "*" & Forms!frmSearchJobChoices!txtJobChoice & "*"

3. In the next job field (jobChoice2), in the row BELOW CRITERIA (the row
labeled 'or'), type the same thing. In the next job field (jobChoice3), in
the row TWO ROWS BELOW CRITERIA, type the same thing. Do that for all the
jobChoice fields. Keep stepping down one row for each successive field.

Save/Close the query.

4. You need a report based on that query. I'll let you worry about the
report design. For testing purposes, just use the report wizard and base it
on the query you made here. Include the name fields of the person, and all
the job fields Don't worry about grouping/sorting. You can do that after
you verify you are getting the results you want.

The only other thing I'll say about report design is that you will have a
small headache using the jobChoice fields in your report because no single
job choice field will return with the value of the job you are looking for.
You will see when the report opens what I mean.

So, to get past that you can pull the value of teh job you searched for from
the form you use to filter the query, which we'll get to now. If you want to
list all the jobs those people listed in the six fields (read: the job you
searched for and the other five each of those people listed), then it's going
to be a little more painful to work around to get things to look right since
you will, again, not know which one of the fields has the job you searched
for since it will be different depending on what field that person entered
that job. Hello Normalization!

Save and close your report.

5. That criteria you applied to the different fields in the query we made
includes a reference to a form control. It was
Forms!frmMyJobChoices!txtJobChoice. You need to make that form, the control
referenced, and a command button. So create a new form (in design view).
You will name it when you close/save it.

6. create a text box. I used text box because I wasn't sure how your table
with the jobs was setup, and I didn't want to have to make up a table of my
own to test this with. For now, just do the text box. You will eventually
want a combo box based on that table with the 80 jobs in it. If you get all
this working, you can go back and change teh control to a combo box. It
really won't be much aditional work at all.

You can draw the text box using the toolbox from the view menu. Open the
toolbox and click on the text box button, then click in your form and drag
down and to the right and release.

7. Make sure the new text box is selected, open teh properties window (f4),
click the 'other' tab, and name it txtJobChoice

8. Add a command button. If you use the toolbox, and you have the control
wizard button clicked, a wizard will start to run automatically after you
drag out the command button. Just cancel it. Make sure the command button
is selected, make sure the properties window is open (f4 if it isn't), click
on the 'events' tab, and find the 'click' event in the list. There is a
white box beside where it says 'click'. Double left click in it to
automatically bring up the words 'event procedure'. If you can't get that to
work, use the arrow further to the right and choose 'event proecudure' from
the drop down. Once you have that, click the button still further to the
right with the dots on it (...). The vba editor will open and you will see a
line that says Sub txtJobChoice_Click()

A little below that line will be an 'End Sub'

9. Between those two lines, type this:

DoCmd.OpenReport "rptYourReportName", acViewPreview

NOTE: rptYourReportName is the name of your report that you made, so replace
rptYourReportName appropriately.

10. Close the vba editor. Close the form. Name it frmMyJobChoices

11. Open the form back up in normal mode. Fill in an appropriate job
choice. (remember my comments at the top about what is an appropriate choice)
Then, click the button.

If my instructions weren't too complicated or long-winded, you should see
the results: 1 record for every person who put the job you searched for in
one of the six jobChoice fields. Remember, if the values in your query for
thoe jobChoice fields are numbers, you need to enter a number in the
txtJobChoice for things to work. When you convert the text box to a combo
box, you will be able to resolve that so that the users are selecting job
names.

HTH,
CW
 
P

pietlinden

Okay, since I posted such an ugly response... I'll see if I can help
you out. But I definitely think you should normalize. It will make
your querying *infinitely* easier.

If your field names are consistent or in a predictable sequence, you
can use DAO (but not ADO) to loop through the fields collection of a
table and process just the ones you want. Works a champ once you get
the hang of it.

Say you want to get all the columns that start with "jobchoice" and
you want to include the ClientID (what? your clients don't have a
unique ID?!!! They should!)


dim tdf as dao.tabledef
dim fld as dao.field
dim strSQL as string

set tdf=DBEngine(0)(0).TableDefs("MyTable")
for each fld in tdf.fields
if left$(fld.name,6)="Choice" then
'do something
if len(strsql)=0 then
strSQL = "SELECT [ClientID], [" & fld.name & "] UNION ALL
"
else
strSQL = strsql & "SELECT [ClientID], [" & fld.name & "]
UNION ALL "
end if
end if
next fld

'trim off final UNION ALL "
strsql = left$(strsql,len(strsql)-11)
set tdf=nothing

then you can use the text in strSQL to create a new query...
 
C

Clif McIrvin

Okay, since I posted such an ugly response... I'll see if I can help
you out. But I definitely think you should normalize. It will make
your querying *infinitely* easier.



Piet, thank you! for having second thoughts and posting an example. I
was going to reply and ask about your VBA function to create union
queries, as I also am using union queries to cause a linked spreadsheet
to appear somewhat normalized. So far, so good but I'm always looking
for better methods! (At some future date I hope to convert the
spreadsheet based processes into Access.)
 
E

Evoke

Hi Cheese_whiz,

thank you for the reply.

This is like a forgein language to me! but slowly (very slowly) getting there.

I followed your instructions, and they kinda worked!. I created the query,
report and form. I made the text box a list box, and i can get the command
button to work and bring up a report to show clients who listed that
particular job.

The only thing that comes up is, when i click the command button i get a
little box popping up - with space to type in

"Enter Parameter Value"
Forms!frmSearchJobChoices!textJobChoice

this was entered in the query. If i type the job in again, in the space
provided, the report displays correctly.

Is there a way for this not to display, so people dont have to type in
something, they click and go straight to the report!

thank you once again!
 
E

Evoke

Hi Again,

I have managed to get the report to show by just clicking on it (yay!
Exactly what i wanted).

The problem i have now, is that it shows *every* client. The clients with
that particular job seems to be the 1st few but all clients show.

Is there away to have it show only the clients for that job?

(E.g. If someone selects 'Waiter' it only shows clients who have selected
waiter in job1,2,3,4,5 or 6. No other clients appear.)

On the report that is displayed, only client information will show - the job
selection choices (1,2,3,4,5,6) wont actually be displayed. Just need it to
search them to bring up the clients!

thank you very much again!
 
C

Cheese_whiz

Hi Evoke,

Check the query for the string you used and make sure it matches what you
named the control on the form. My previous post was based on using a text
box, and the name I gave that text box was actually txtJobChoice, NOT
tExtJobChoice.

You say you've used a list box instead. I suspect you didn't name the list
box the same thing as you used in the criteria expressions in the query.
Since that parameter window is popping up, it means the query isn't getting
the data it needs, which means it can't find the control reference.

Also, you may have to alter the reference since you used the list box,
depending on how you set it up. That's why I used a text box. I was trying
to take out the possible confusion added by using a control that might have
different referencing depending on how you set it up.

HTH,
CW
 
R

Regina Parker

I've got a report that pulls from a similar setup and what I did was have
the report reference a hidden text box on the form and had the list box on
the "after update" stick the value in the hidden text box. lol..actually,
the setup I'm using is a series of list boxes. The first list box gives you
a list of choices (customers!) and the second list box gives you another
series of choices (multiselect list box of specific invoices for customer
you chose in the first list box). The second list box feeds to a hidden
text box on the page which gives the criteria for the report. Before the
report opens, it looks to see if the form is open, then the query checks for
the value in the hidden text box and then builds the invoice based on
customer id and invoice number. All you have to do is make sure that you
reference the correct text box. lol..main thing I had a problem with was
remembering that the columns on a list box begin with .column(0)! and I've
been programming these things since Access 2. We just upgraded to Access
2007 Friday and I just started rebuilding all of our databases...so I'm
going to be visiting frequently :) <waves>
 
E

Evoke

Hi Again,

THANK YOU SO MUCH!

I checked through all names of controls etc - did something and it WORKED!
I've made it a list box from the Job Table. A selection can be made, press
the command button and a report shows only clients for that job!

I didn't actually think i'd be able to get it.

Thank you so much for your help! you've made my day!

this has been a nightmare for weeks now!

:)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top