Query Based On "yes/no" Switch in Data Base Table

T

Tom

I have on my Form the following:
Sunday "yes/no"
Monday "yes/no"
Tuesday "yes/no"
Wednesday "yes/no"
Thursday "yes/no"
Friday "yes/no"
Saturday "yes/no"
The "yes/no" is in the form of a "check box"
I'd like to design a query that will list only the
day I choose, if it is "checked".
i.e. If I have 500 records, and only 1 record has
Friday checked, then when I query for Friday, Voila!
How do I do this?
Tom
 
T

tina

how is the day of the week stored in the *table*? as a text value " Sunday",
"Monday", etc? or as a date value from which the day of the week can be
extracted? or...?

hth
 
T

Tom

As text value, i.e. "Sunday".

tina said:
how is the day of the week stored in the *table*? as a text value " Sunday",
"Monday", etc? or as a date value from which the day of the week can be
extracted? or...?

hth
 
T

tina

so you have seven fields in the table, named "Sunday", "Monday", "Tuesday",
etc - each with a data type of Yes/No? if so, the first thing i'd do is
normalize the table to one field for the day of the week, and store the day
value (as a number 1-7, or as text) in it. if you have a Date/Time field in
the table, and the date values correspond to the days of the week that are
being stored, then you don't need the "day" field at all because you can
extract the day of week from the date value.

hth
 
T

Tom

Tina,

I can't do it that way. This is a delivery schedule
and on the form, it may have more than one day
scheduled. i.e. mon, wed, fri.
So those boxes would be checked.
I want to be able to pull up for example,
all those records with "mon" checked.

Tom
 
W

Wayne Morgan

Tom,

The simple, basic answer to your question, not withstanding the design, is

WHERE [table name].[Monday] = True

as the criteria for the query. This will return every record that has Monday
set to True (i.e. checked).
 
T

Tom

Wayne,
I tried this in Criteria, and it did not work
it returned every record
"WHERE [ScheduledRuns].[Monday]"=True
Tom

Wayne Morgan said:
Tom,

The simple, basic answer to your question, not withstanding the design, is

WHERE [table name].[Monday] = True

as the criteria for the query. This will return every record that has Monday
set to True (i.e. checked).

--
Wayne Morgan
MS Access MVP


Tom said:
Tina,

I can't do it that way. This is a delivery schedule
and on the form, it may have more than one day
scheduled. i.e. mon, wed, fri.
So those boxes would be checked.
I want to be able to pull up for example,
all those records with "mon" checked.
 
W

Wayne Morgan

Please post the entire SQL of the query, I suspect the problem is that the
quotes shouldn't be there.
 
T

Tom

This is it
"WHERE [ScheduledRuns].[Monday]"=True
I tried taking out the "" but it would not let me
Running Access 2000
Tom

Wayne Morgan said:
Please post the entire SQL of the query, I suspect the problem is that the
quotes shouldn't be there.

--
Wayne Morgan
MS Access MVP


Tom said:
Wayne,
I tried this in Criteria, and it did not work
it returned every record
"WHERE [ScheduledRuns].[Monday]"=True
Tom
 
W

Wayne Morgan

If that's the entire SQL then where are you using it? That is only the WHERE
clause to a query, not the entire query. Please post what you are using and
where you're using it.

--
Wayne Morgan
MS Access MVP


Tom said:
This is it
"WHERE [ScheduledRuns].[Monday]"=True
I tried taking out the "" but it would not let me
Running Access 2000
Tom

Wayne Morgan said:
Please post the entire SQL of the query, I suspect the problem is that
the
quotes shouldn't be there.

--
Wayne Morgan
MS Access MVP


Tom said:
Wayne,
I tried this in Criteria, and it did not work
it returned every record
"WHERE [ScheduledRuns].[Monday]"=True
Tom
 
T

Tom

Wayne,
Here goes.
Access 2000
Database "AbleDispatch.mdb"
Objects
Queries
"ScheduledRunsQuery"
Based on Table "ScheduledRuns"
Design View
Field: Sunday Monday
Tuesday etc.
Criteria: "WHERE [ScheduledRuns].[Monday]"=True
That was placed under Monday criteria

I tried just typing an "=yes" under Monday and that
worked. However - this is for other users. I don't
want them into the file system for this. Is there
a way for them to input, after pressing a command
button, the day of the week? If I put and Input statement
under each day, then obviously it would bring up
7 screens to input. I would think that if you entered
a "yes" or "no" under just the day needed, leaving
all others blank, then it would return just that day.
Hope that helps
Tom

Wayne Morgan said:
If that's the entire SQL then where are you using it? That is only the WHERE
clause to a query, not the entire query. Please post what you are using and
where you're using it.

--
Wayne Morgan
MS Access MVP


Tom said:
This is it
"WHERE [ScheduledRuns].[Monday]"=True
I tried taking out the "" but it would not let me
Running Access 2000
Tom

Wayne Morgan said:
Please post the entire SQL of the query, I suspect the problem is that
the
quotes shouldn't be there.

--
Wayne Morgan
MS Access MVP


Wayne,
I tried this in Criteria, and it did not work
it returned every record
"WHERE [ScheduledRuns].[Monday]"=True
Tom
 
T

tina

when you're in the Design view of a query (not the SQL pane), the correct
entry on the Criteria line for a Yes/No data type field is simply True or
False, without any quotes. however, to refer to a control on a form to get
the criteria value (rather than "hard-coding" a specific value into the
query), use a form reference on the criteria line, as

Forms!FormName!CheckboxControlName

replace FormName with the actual name of the form, and CheckboxControlName
with the actual name of the checkbox control, of course.

in your query, you'll need a reference for each checkbox field. the trick
here is to set up the criteria as OR references, not AND references. to do
this, put the reference criteria on the first Criteria line of the first day
field. then move over to the next day field, and put the reference criteria
on the second Criteria line - leaving the first line blank. then move over
to the next day field, and put the reference criteria on the thrid Criteria
line - leaving the first and second lines blank. continue in this fashion,
"stair-stepping" the reference criteria, until you've added criteria for
each day field that refers to the corresponding "day" checkbox on the form.

next, add the following code to the Click event procedure of the command
button on the form, as

DoCmd.OpenQuery "NameOfQuery"
With Me
!chkMon = Null
!chkTues = Null
!chkWed = Null
!chkThurs = Null
!chkFri = Null
!chkSat = Null
!chkSun = Null
End With

substitute the correct name of the query and the correct names of the
checkbox controls on the form, of course. the above code opens the query,
obviously; if you want to open a form or report that's bound to the query
(usually preferable to opening a query directly), then change the Open
command accordingly.

hth


Tom said:
Wayne,
Here goes.
Access 2000
Database "AbleDispatch.mdb"
Objects
Queries
"ScheduledRunsQuery"
Based on Table "ScheduledRuns"
Design View
Field: Sunday Monday
Tuesday etc.
Criteria: "WHERE [ScheduledRuns].[Monday]"=True
That was placed under Monday criteria

I tried just typing an "=yes" under Monday and that
worked. However - this is for other users. I don't
want them into the file system for this. Is there
a way for them to input, after pressing a command
button, the day of the week? If I put and Input statement
under each day, then obviously it would bring up
7 screens to input. I would think that if you entered
a "yes" or "no" under just the day needed, leaving
all others blank, then it would return just that day.
Hope that helps
Tom

Wayne Morgan said:
If that's the entire SQL then where are you using it? That is only the WHERE
clause to a query, not the entire query. Please post what you are using and
where you're using it.

--
Wayne Morgan
MS Access MVP


Tom said:
This is it
"WHERE [ScheduledRuns].[Monday]"=True
I tried taking out the "" but it would not let me
Running Access 2000
Tom

:

Please post the entire SQL of the query, I suspect the problem is that
the
quotes shouldn't be there.

--
Wayne Morgan
MS Access MVP


Wayne,
I tried this in Criteria, and it did not work
it returned every record
"WHERE [ScheduledRuns].[Monday]"=True
Tom
 
T

Tom

Tina,
Here is what I have, and it is not working.

Private Sub Command115_Click()
On Error GoTo Err_Command115_Click

Dim stDocName As String

stDocName = "ScheduledRunsTodayList"
DoCmd.OpenReport stDocName, acPreview
DoCmd.OpenReport "ScheduledRunsTodayList"
With Me
!chkSunday = Null
!chkMonday = Null
!chkTuesday = Null
!chkWednesday = Null
!chkThursday = Null
!chkFriday = Null
!chkSaturday = Null
End With
Exit_Command115_Click:
Exit Sub

Err_Command115_Click:
MsgBox Err.Description
Resume Exit_Command115_Click

End Sub

The problem is, it comes up with a message box for each
day, If I leave all of them blank, clicking "ok" for each one,
a blank report is listed. If I put in "true" for any day of the
week, leaving others blank, or I put "true" in one and "false" in
the others or answer all "true", it comes up
with the following:
"This expression is typed incorrectly, or it is too complex to be evaluated
.. . ."

This same thing happens when I'm in query design and test by
using the "!"

Tom
 
T

tina

ok, first let's fix your code. it opens the report twice. take out one of
the two DoCmd.OpenReport lines. if you want to *see* the report rather than
immediately print it, take out the second line.

next, sounds like your form references are incorrect, because you should not
be getting any message boxes at all when you click the command button.
please tell us the name of your form - the name that shows on the list of
forms on the Form "tab" in the database window. and tell us the name of each
checkbox control on the form - in form design, click on a checkbox control
and, in the Properties box, click on the Other tab, and look at the Name
property.

next, open your query in design view. on the menu bar, click View | SQL
View. higlight the *whole* SQL statement from beginning to end, copy it, and
paste it into your post so we can look at it.

hth
 
T

Tom

I found the first problem, checkbox control name, but have not fixed it yet.

Name of Form: ScheduledRuns
Name of each checkbox control:
Sun
Mon
Tue
Wed
Thu
Fri
Sat
SQL Statement:
SELECT ScheduledRuns.PtLastName, ScheduledRuns.StartDate,
ScheduledRuns.EndDate, ScheduledRuns.ApptTime, ScheduledRuns.ScheduledTime,
ScheduledRuns.TrFacility, ScheduledRuns.RecFacility, ScheduledRuns.Sunday,
ScheduledRuns.Monday, ScheduledRuns.Tuesday, ScheduledRuns.Wednesday,
ScheduledRuns.Thursday, ScheduledRuns.Friday, ScheduledRuns.Saturday
FROM ScheduledRuns
WHERE (((ScheduledRuns.StartDate) Between IIf(Date()<[EndDate],Date()) And
[StartDate]) AND ((ScheduledRuns.EndDate) Between
IIf(Date()>[StartDate],Date()) And [EndDate]) AND
((ScheduledRuns.Sunday)=[Form]![ScheduledRuns]![Sunday])) OR
(((ScheduledRuns.Monday)=[Form]![ScheduledRuns]![Monday])) OR
(((ScheduledRuns.Tuesday)=[Form]![ScheduledRuns]![Tuesday])) OR
(((ScheduledRuns.Wednesday)=[Form]![ScheduledRuns]![Wednesday])) OR
(((ScheduledRuns.Thursday)=[Form]![ScheduledRuns]![Thursday])) OR
(((ScheduledRuns.Friday)=[Form]![ScheduledRuns]![Friday])) OR
(((ScheduledRuns.Saturday)=[Form]![ScheduledRuns]![Saturday]));

I appologize for not informing you of the other part of the query. This
limits the
reports to dates between the "StartDate" and "EndDate" but >= today.
That part needs to be there and it is working fine.

Tom
 
J

John Vinson

I found the first problem, checkbox control name, but have not fixed it yet.

Name of Form: ScheduledRuns
Name of each checkbox control:
Sun
Mon
Tue
Wed
Thu
Fri
Sat
SQL Statement:
SELECT ScheduledRuns.PtLastName, ScheduledRuns.StartDate,
ScheduledRuns.EndDate, ScheduledRuns.ApptTime, ScheduledRuns.ScheduledTime,
ScheduledRuns.TrFacility, ScheduledRuns.RecFacility, ScheduledRuns.Sunday,
ScheduledRuns.Monday, ScheduledRuns.Tuesday, ScheduledRuns.Wednesday,
ScheduledRuns.Thursday, ScheduledRuns.Friday, ScheduledRuns.Saturday
FROM ScheduledRuns
WHERE (((ScheduledRuns.StartDate) Between IIf(Date()<[EndDate],Date()) And
[StartDate]) AND ((ScheduledRuns.EndDate) Between
IIf(Date()>[StartDate],Date()) And [EndDate]) AND
((ScheduledRuns.Sunday)=[Form]![ScheduledRuns]![Sunday])) OR
(((ScheduledRuns.Monday)=[Form]![ScheduledRuns]![Monday])) OR
(((ScheduledRuns.Tuesday)=[Form]![ScheduledRuns]![Tuesday])) OR
(((ScheduledRuns.Wednesday)=[Form]![ScheduledRuns]![Wednesday])) OR
(((ScheduledRuns.Thursday)=[Form]![ScheduledRuns]![Thursday])) OR
(((ScheduledRuns.Friday)=[Form]![ScheduledRuns]![Friday])) OR
(((ScheduledRuns.Saturday)=[Form]![ScheduledRuns]![Saturday]));

The correct syntax is [Forms]! , with an S, not [Form]; and you say
that the checkbox controls are named Sun, Mon, etc. but you're not
ASKING by those names - you're using the full name of the day. The
text strings "Sun" and "Sunday" are different, and Access will NOT
treat them as being the same.

The fact that you're storing data (dates) in fieldnames
(ScheduledRuns.Sunday for example) strongly suggests that your table
is incorrectly normalized. You'll really do better to store data (a
date) *in a date/time field* in your table, rather than naming yes/no
fields in this way.

John W. Vinson[MVP]
 
T

Tom

John,
I'm not storing "dates" in a yes/no field.
My form is asking the question "does this occurance
happen on a Sunday, Monday, etc. all, or part?"

Thank you for the "form(s)"!!
Tom

John Vinson said:
I found the first problem, checkbox control name, but have not fixed it yet.

Name of Form: ScheduledRuns
Name of each checkbox control:
Sun
Mon
Tue
Wed
Thu
Fri
Sat
SQL Statement:
SELECT ScheduledRuns.PtLastName, ScheduledRuns.StartDate,
ScheduledRuns.EndDate, ScheduledRuns.ApptTime, ScheduledRuns.ScheduledTime,
ScheduledRuns.TrFacility, ScheduledRuns.RecFacility, ScheduledRuns.Sunday,
ScheduledRuns.Monday, ScheduledRuns.Tuesday, ScheduledRuns.Wednesday,
ScheduledRuns.Thursday, ScheduledRuns.Friday, ScheduledRuns.Saturday
FROM ScheduledRuns
WHERE (((ScheduledRuns.StartDate) Between IIf(Date()<[EndDate],Date()) And
[StartDate]) AND ((ScheduledRuns.EndDate) Between
IIf(Date()>[StartDate],Date()) And [EndDate]) AND
((ScheduledRuns.Sunday)=[Form]![ScheduledRuns]![Sunday])) OR
(((ScheduledRuns.Monday)=[Form]![ScheduledRuns]![Monday])) OR
(((ScheduledRuns.Tuesday)=[Form]![ScheduledRuns]![Tuesday])) OR
(((ScheduledRuns.Wednesday)=[Form]![ScheduledRuns]![Wednesday])) OR
(((ScheduledRuns.Thursday)=[Form]![ScheduledRuns]![Thursday])) OR
(((ScheduledRuns.Friday)=[Form]![ScheduledRuns]![Friday])) OR
(((ScheduledRuns.Saturday)=[Form]![ScheduledRuns]![Saturday]));

The correct syntax is [Forms]! , with an S, not [Form]; and you say
that the checkbox controls are named Sun, Mon, etc. but you're not
ASKING by those names - you're using the full name of the day. The
text strings "Sun" and "Sunday" are different, and Access will NOT
treat them as being the same.

The fact that you're storing data (dates) in fieldnames
(ScheduledRuns.Sunday for example) strongly suggests that your table
is incorrectly normalized. You'll really do better to store data (a
date) *in a date/time field* in your table, rather than naming yes/no
fields in this way.

John W. Vinson[MVP]
 
J

John Vinson

I'm not storing "dates" in a yes/no field.
My form is asking the question "does this occurance
happen on a Sunday, Monday, etc. all, or part?"

I'd argue that you are. The fact that the name of the field is
"Sunday" means that you are storing date information - the day of the
week - in the fieldname. This violates the relational principle that
fields should be independent and nonrepeating. You have seven repeated
fields storing information which could be stored in one date/time
field.

I see WHY you're doing it - for data entry convenience, so you can
just check a checkbox - but the relational purist in me winces at this
example of letting the user interface dictate the table structure. It
should be the other way around.

Just to clarify - I have NO objection to presenting the information in
this way *on the Form*. My concern is storing the data in this way in
the Table.

John W. Vinson[MVP]
 

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