Finding a value in several different columns

N

Nick Xylas

Sorry if this is a really easy question, but I'm still learning when
it comes to Access 2003's capabilities. I have a table from which I
want to create either a report or a query to show the record number
(primary key) of all instances where the value "1" occurs in field A
OR field B OR field C, followed by all instances of "2" then "3" and
so on. I'd imagine it's an easy thing to do for an experienced user,
but I'm a novice, so any help would be appreciated.
 
C

Clifford Bass via AccessMonster.com

Hi Nick,

Interesting question. Here is one way to do it. Create a table named
"tblNumbers" with one number (integer or long integer) column. Fill it with
ten records with the values from 0 to 9. Next create a query that yields
enough values to cover all the values you want to report. So if you are
reporting for values 0 through 99 your query, named "qryNumbers 0-99" would
look like this:

SELECT [Tens].[The_Number]*10+[Ones].[The_Number] AS The_Number
FROM tblNumbers AS Tens, tblNumbers AS Ones;

Or if you you need 0-999 you would have:

SELECT [Hundreds].[The_Number]*100+[Tens].[The_Number]*10+[Ones].[The_Number]
AS The_Number
FROM tblNumbers AS Hundreds, tblNumbers AS Tens, tblNumbers AS Ones;

The above method of creating sequential lists of numbers was provided by
another poster--sorry I do not remember his name. Then you can use that
query with your table to check each field for each number value like so:

SELECT [qryNumbers 0-99].The_Number, tblMy_Table.PK_Column, tblMy_Table.A,
tblMy_Table.B, tblMy_Table.C
FROM tblMy_Table, [qryNumbers 0-99]
WHERE (((tblMy_Table.A)=[The_Number])) OR (((tblMy_Table.B)=[The_Number])) OR
(((tblMy_Table.C)=[The_Number]))
ORDER BY [qryNumbers 0-99].The_Number, tblMy_Table.PK_Column;

This of course will show only the numbers that are found in the columns.
If you need to report missing numbers with blank primary key values, use the
numbers query on the left side of a left outer join on the above query,
joining on The_Number.

Hope this helps,

Clifford Bass
 
D

Daryl S

Nick -

You didn't say what to do with duplicates (that is, if a given record number
has a 1 in Column A in one record and also a 1 in Column A in another record
- would you want that record number to be listed once or twice?

Start with this (but use your table and fieldnames):

SELECT RecordNumber, 1 As Value_Check
From YourTableName
Where (nz([FieldA],0)=1 OR nz([FieldB],0)=1 OR nz([FieldC],0)=1)

If you only want one record returned if there are duplicates, change the
SELECT to SELECT DISTINCT. This should give you all the records with a 1 in
either FieldA, FieldB, or FieldC. Once you get this working, then you will
want to copy/paste the SQL into a new query in SQL View (don't select any
tables in query design view). Paste it in three times, putting a UNION ALL
between each pair of queries, and removing the semicolon from the first two
queries. Change the "1" to "2" and "3" in the other queries. It will look
something like this (but with your table and field names):

SELECT RecordNumber, 1 As Value_Check
From YourTableName
Where (nz([FieldA],0)=1 OR nz([FieldB],0)=1 OR nz([FieldC],0)=1)
UNION ALL
SELECT RecordNumber, 2 As Value_Check
From YourTableName
Where (nz([FieldA],0)=2 OR nz([FieldB],0)=2 OR nz([FieldC],0)=2)
UNION ALL
SELECT RecordNumber, 3 As Value_Check
From YourTableName
Where (nz([FieldA],0)=3 OR nz([FieldB],0)=3 OR nz([FieldC],0)=3)

That should give you what you need. If you have issues, paste your SQL
into your next posting.
 
N

Nick Xylas

Nick -

You didn't say what to do with duplicates (that is, if a given record number
has a 1 in Column A in one record and also a 1 in Column A in another record
- would you want that record number to be listed once or twice?  
I'm not sure I quite understand the question. If one record has a 1 in
column A and another record also has a 1 in column A, I would like the
primary key autonumber for both records to be listed. Is that what you
are asking?

And I should have said that the numbers go all the way up to 147. Is
there a way to automate the process so that I don't have to type all
the numbers from 1 to 147 in manually? The numbers represent people
who can fall into one of three categories, and I am trying to bring up
every record in which each person appears.
 
C

Clifford Bass via AccessMonster.com

Nick,

Did you try my solution?

Clifford Bass

Nick said:
I'm not sure I quite understand the question. If one record has a 1 in
column A and another record also has a 1 in column A, I would like the
primary key autonumber for both records to be listed. Is that what you
are asking?

And I should have said that the numbers go all the way up to 147. Is
there a way to automate the process so that I don't have to type all
the numbers from 1 to 147 in manually? The numbers represent people
who can fall into one of three categories, and I am trying to bring up
every record in which each person appears.
 
N

Nick Xylas

Where (nz([FieldA],0)=3 OR nz([FieldB],0)=3 OR nz([FieldC],0)=3)

That should give you what you need.  If you have issues, paste your SQL
into your next posting.

I got a syntax error. This was my SQL

SELECT RecordNumber, 1 As Value_Check
From Subplots
Where (nz([Character 1],0)=1 OR nz([Character 2],0)=1 OR nz([Character
3,0)=1)
UNION ALL
SELECT RecordNumber, 2 As Value_Check
From Subplots
Where (nz([Character 1],0)=2 OR nz([Character 2],0)=2 OR nz([Character
3,0)=2)
UNION ALL
SELECT RecordNumber, 3 As Value_Check
From Subplots
Where (nz([Character 1],0)3 OR nz([Character 2],0)=3 OR nz([Character
3,0)=3)
 
D

Daryl S

Nick -

If you table only has one record for each person, then you don't need to
worry about duplicates. Since I don't know the data, I was asking in case
you could have one person in the table multiple times.

The reason this is so difficult is your table structure is probably not
correct. Since I don't know what A, B, or C are, but they are used in the
same way for this query or report, then my thought would be instead of a
table that looks like this (I am using Person for the primary key just
because I don't know your data):

Person A B C
Mary 1 1 2
John 12 1 4
Jack 2 5 8


The table would be like this:
Person Type Value
Mary A 1
Mary B 1
Mary C 2
John A 12
John B 1
John C 4
Jack A 2
Jack B 5
Jack C 8


Then your queries would be very simple:

Select Distinct Value, Person from <your table name>
Order by Value, Person;
 
N

Nick Xylas

Nick -

If you table only has one record for each person, then you don't need to
worry about duplicates.  Since I don't know the data, I was asking in case
you could have one person in the table multiple times.

The reason this is so difficult is your table structure is probably not
correct.  Since I don't know what A, B, or C are, but they are used in the
same way for this query or report, then my thought would be instead of a
table that looks like this (I am using Person for the primary key just
because I don't know your data):

OK, let me give you a little background. This database is for an
ongoing comic I have been plotting for some time and which features a
huge cast of characters and many intertwining subplots. In order to
help me keep track, I created a table named Subplots, which links to
another table named Characters. Each record in Subplots features three
fields, named Character 1, Character 2 and Character 3, which use the
primary key from the Characters table as their data. But the character
with the number 47 (to pick a random example) as his or her primary
key in the Characters table (which uses an Autonumber as the primary
key) could be Character 1 in one record and Character 3 in another. I
want to be able to search for the value "47" in the Character 1 OR
Character 2 OR Character 3 field in the Subplots table, but if
possible, I'd like to be able to do the same for all the records in
the Characters table, starting with the first and going onto the last.
Though I'm beginning to wonder whether it might not be simpler to do
it manually,,,.
 
C

Clifford Bass via AccessMonster.com

Hi Nick,

I am rather curious; is there some reason that you have not tried my
solution?

On another note; what happens when you have four or five characters in a
particular subplot? I concurr with Daryl--you need to fix your table
structure. Then the whole problem will go away.

Clifford Bass
 
N

Nick Xylas

Hi Nick,

     I am rather curious; is there some reason that you have not tried my
solution?
Mostly because I wasn't sure how to adapt it from the generic to the
specific (ie what to replace with my actual field and table names).
 
C

Clifford Bass via AccessMonster.com

Hi Nick,

Pretty simple. Create the table as mentioned and the second numbers
query; which you will name ""qryNumbers 0-999". Then your query will be:

SELECT [qryNumbers 0-999].The_Number, Subplots.RecordNumber, Subplots.
[Character 1], Subplots.[Character 2], Subplots.[Character 3]
FROM Subplots, [qryNumbers 0-999]
WHERE (((Subplots.[Character 1])=[The_Number])) OR (((Subplots.[Character 2])
=[The_Number])) OR (((Subplots.[Character 3])=[The_Number]))
ORDER BY [qryNumbers 0-999].The_Number, Subplots.RecordNumber;

For the future, it may be easier to tell us your actual table and field
names instead of trying to be generic.

If you are still confused, post back on where you need further
clarification.

Clifford Bass
 
N

Nick Xylas

Hi Nick,

     Pretty simple.  Create the table as mentioned and the secondnumbers
query; which you will name ""qryNumbers 0-999".  Then your query will be:

SELECT [qryNumbers 0-999].The_Number, Subplots.RecordNumber, Subplots.
[Character 1], Subplots.[Character 2], Subplots.[Character 3]
FROM Subplots, [qryNumbers 0-999]
WHERE (((Subplots.[Character 1])=[The_Number])) OR (((Subplots.[Character 2])
=[The_Number])) OR (((Subplots.[Character 3])=[The_Number]))
ORDER BY [qryNumbers 0-999].The_Number, Subplots.RecordNumber;

     For the future, it may be easier to tell us your actual table and field
names instead of trying to be generic.
I realise that now. I thought the solution would be a lot simpler than
it actually was.
     If you are still confused, post back on where you need further
clarification.

             Clifford Bass
I ran the query and it asked me to input parameters. What am I
supposed to enter into these boxes?
 
C

Clifford Bass via AccessMonster.com

Hi Nick,

The nature of your question associated with the way you set up your
table(s) makes it less than simple.

It should not prompt you for anything unless I or you have mistyped
something. Let's back up a bit. Did you create the "tblNumbers" table? If
not, do so following my instructions in my first reply to you. Next, did you
create the "qryNumbers 0-999" query by copying and pasting my code into the
SQL view of a new query? If not do so, and verify that it works by running
it. If you have all that done and still get prompted for parameters when
running the final query, what are the "parameters" being requested?

Clifford Bass

Nick said:
[quoted text clipped - 10 lines]
     For the future, it may be easier to tell us your actual table and field
names instead of trying to be generic.

I realise that now. I thought the solution would be a lot simpler than
it actually was.
     If you are still confused, post back on where you need further
clarification.

             Clifford Bass

I ran the query and it asked me to input parameters. What am I
supposed to enter into these boxes?
 
K

KenSheridan via AccessMonster.com

Nick:

Your problems stem from a flawed design. Its not difficult to correct this,
however, and things will then become very much simpler.

What you have here are two main entity types, Characters and Subplots, and a
many-to-many relationship type between them, i.e. each character can be in
one or more subplot, and each subplot can involve one or more characters. So
starting with the two maim tables:

Characters
….CharacterID
….Character

Subplots
….SubplotID
….Subplot

The relationship type between them is modelled by a third table:

SubplotCharacters
….SubplotID
….CharacterID

Each of the two columns is a foreign key referencing the primary key of
Subplots and Characters respectively. The primary key of this table is a
composite one made up of both columns. You can of course add other non-key
columns to this table representing other attributes of a character's role in
a subplot if necessary.

Once you've created the SubplotCharacters table filling it is easy with three
'append' queries:

INSERT INTO SubplotCharacters(SubplotID, CharacterID)
SELECT SubplotID, [Character 1]
FROM Subplots
WHERE [Character 1] IS NOT NULL;

INSERT INTO SubplotCharacters(SubplotID, CharacterID)
SELECT SubplotID, [Character 2]
FROM Subplots
WHERE [Character 2] IS NOT NULL;

INSERT INTO SubplotCharacters(SubplotID, CharacterID)
SELECT SubplotID, [Character 3]
FROM Subplots
WHERE [Character 3] IS NOT NULL;

Once you are happy that the table has been filled correctly you can deleted
the redundant Character 1-3 columns from Subplots

You can now have as few or as many characters per subplot simply by inserting
a row for each into SubplotCharacters.

To return which characters are in which subplots you now just need a simple
query:

SELECT Subplot, Character
FROM Characters, SubplotCharacters, Subplots
WHERE Subplots.CharacterID = Characters.CharacterID
AND Subplots.SubplotID = Subplots.SubPlotID
ORDER BY Subplot, Character;

This will group order the results by subplot and then characters in each. To
order it by character, then those subplots in which each is involved just
change the SELECT clause to:

SELECT Character, Subplot

And the ORDER BY clause to:

ORDER BY Character, Subplot

If you want to return the subplots in which a particular character is
involved you can add a parameter:

SELECT Character, Subplot
FROM Characters, SubplotCharacters, Subplots
WHERE Subplots.CharacterID = Characters.CharacterID
AND Subplots.SubplotID = Subplots.SubPlotID
AND (Character = [Enter character:]
OR [Enter character:] IS NULL)
ORDER BY Character, Subplot;

This will return the subplots in which the character entered at the prompt is
involved, or if no character is entered at the prompt, all subplots.

Similarly if you want to return characters is involved in a particular
subplot:

SELECT Subplot, Character
FROM Characters, SubplotCharacters, Subplots
WHERE Subplots.CharacterID = Characters.CharacterID
AND Subplots.SubplotID = Subplots.SubPlotID
AND (Subplot = [Enter subplot:]
OR [Enter subplot:] IS NULL)
ORDER BY Subplot, Character;

In a developed application, rather than simply opening queries, you'd
probably design a dialogue form with combo or list boxes to select characters
or subplots and open a form or report based on one of the queries from the
dialogue form, changing the simple parameters to ones which reference the
combo or list box on the form .

For data entry an appropriate solution would be a form base on Subplots, in
single form view, and within it a subform in continuous forms view, based on
SubplotCharacters. The parent form and subform would be linked on SubplotID
and would include a combo box set up as follows:

ContrlSource: CharacterID

RowSource: SELECT CharacterID, Character FROM Characters ORDER BY
Character;

BoundColumn: 1
ColumnCount: 2
ColumnWidths: 0cm;8cm

If your units of measurement are imperial rather than metric Access will
automatically convert the last one. The important thing is that the first
dimension is zero to hide the first column and that the second is at least as
wide as the combo box.

The combo box would show the character by name, but its underlying value
would be the hidden CharacterID for the selected character. You can of
course include other controls in the subform bound to any other non-key
columns from SubplotCharacters. Assigning a character to a subplot is then
simply a case of inserting a new row in the subform by selecting a character
from the combo box in the empty row at the bottom on the subform.

Ken Sheridan
Stafford, England

Nick said:
[quoted text clipped - 10 lines]
For the future, it may be easier to tell us your actual table and field
names instead of trying to be generic.

I realise that now. I thought the solution would be a lot simpler than
it actually was.
If you are still confused, post back on where you need further
clarification.

Clifford Bass

I ran the query and it asked me to input parameters. What am I
supposed to enter into these boxes?
 
K

KenSheridan via AccessMonster.com

Nick:

Thinking a little more about your requirements, it occurred to me that you
might like to consider using a 'bill of materials' model. In your case each
character would be the equivalent of a 'base part' and each subplot the
equivalent of an 'assembly' made up of base parts and/or other assemblies.
This would mean you could have subplots of subplots right up to the overall
top-level plot, the equivalent of the complete product in a bill of materials.


The classic method of modelling a bill of materials is by means of an
adjacency list, with columns such as MajorPart and MinorPart each of which
reference the key of a Parts table, which includes assemblies as well as base
parts.

You'll find a demo of mine for a bill of materials database in Access at:

http://community.netscape.com/n/pfx/forum.aspx?tsn=1&nav=messages&webtag=ws-msdevapps&tid=23133


The file is attached to the second message in the above thread.

Ken Sheridan
Stafford, England
 

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