Solved

Search Form / Searching Multiple Tables w/ different fields in each

Posted on 2003-11-20
15
238 Views
Last Modified: 2013-12-24
Ok, now I need a search form that can search multiple tables in the same db (Access). The only thing is there are different fields in some of the tables (they exist in the others, but not the same number of fields).

4 tables:
A,B,C,D

Fields in A:
1,2,3,4,5

Fields in B:
1,2,3,4,5

Fields in C:
1,2,3,4,5

Fields in D:
1,2,4

Anyone have some quick code for me???
God forbid I do it myself :)

Thanks,
Michelle
0
Comment
Question by:mflagstad
15 Comments
 
LVL 11

Expert Comment

by:hart
ID: 9795033
could u be more descriptive by giving the field names properly

is there relationshipd between A and B
B and C, C and D ???

then u can write a join get all the corresponding records

say field 1 in A is pk
and field 2 in B is fk

say field1 in B is pk
and field2 in C is fk

and so on...


then joins can help u search records across tables

Regards
Hart
0
 
LVL 10

Expert Comment

by:Mause
ID: 9795356
If the fields are the same in the tables you can also use union like this:

select 1, 2, 3, 4, 5
from A
union
select 1, 2, 3, 4, 5
from B
union
select 1, 2, 3, 4, 5
from C
union
select 1, 2, '' as 3, 4, '' as 5
from D

Mause
0
 
LVL 5

Expert Comment

by:nathans
ID: 9796704
You can give us FAKE names just change the name to make sense so we know what goes together and what are Primary keys etc...
0
 
LVL 1

Author Comment

by:mflagstad
ID: 9796787
here is current search pages:

search.cfm
<form method="GET" action="searchresults.cfm" name="get">
 
  <div align="left">
    <table border="0" cellpadding="0" cellspacing="0">
      <tr>
        <td>Name:</td>
        <td><input type="text" name="name" size="20"></td>
      </tr>
      <tr>
        <td>Lot:</td>
        <td><input type="text" name="lot" size="20"></td>
      </tr>
      <tr>
        <td>Date:</td>
        <td><input type="text" name="date" size="20"></td>
      </tr>
      <tr>
        <td>Many:</td>
        <td><input type="text" name="many" size="20"></td>
      </tr>
      <tr>
        <td>Cremains/Burial:</td>
        <td><input type="text" name="cb" size="20"></td>
      </tr>
    </table>
  </div>
  <p><input type="submit" value="Submit"></p>
</form>


searchresults.cfm

<cfquery name="get" datasource="kidwell" dbtype="ODBC">
SELECT *
from sectionA INNER JOIN peopleA ON sectionA.ID = peopleA.division
union
SELECT *
from sectionBINNER JOIN peopleBON sectionB.ID = peopleB.division
union
SELECT *
from sectionPINNER JOIN peoplePON sectionP.ID= peoplePdivision
union
SELECT *
from sectioncol INNER JOIN peoplecol ON sectioncole.ID = peoplecol.division
union
 </cfquery>

<cfoutput query="get">
<cfif get.recordcount gt 0>
Click on the name below to edit information.<br>
<a href="edit.cfm?id=#id#">
#name##date##id#
</a>
<cfelse>

There were no listings found.  Please try again.
</cfif>
</cfoutput>
</div>

However, this produces an error. How do I do this?
Thanks,
Michelle
0
 
LVL 1

Author Comment

by:mflagstad
ID: 9796848
Sorry, I forgot to include the data names, etc.:

peopleA: name, lot, id, date, many, cb,division (fk)
peopleB: name, lot, id, date, many, cb,division(fk)
peopleP: name, lot, id, date, many, cb,division(fk)
peopleCOL: id, name, date,division(fk)

sectionA:ID (PK)
sectionB: ID(PK)
sectionP:ID(PK)
sectionCOL: ID(PK)

0
 
LVL 10

Expert Comment

by:Mause
ID: 9796877
You dont need the last union (just before </cfquery>) and there need to be a space before INNER
So it would be:
<cfquery name="get" datasource="kidwell" dbtype="ODBC">
SELECT *
from sectionA INNER JOIN peopleA ON sectionA.ID = peopleA.division
union
SELECT *
from sectionB INNER JOIN peopleBON sectionB.ID = peopleB.division
union
SELECT *
from sectionP INNER JOIN peoplePON sectionP.ID= peoplePdivision
union
SELECT *
from sectioncol INNER JOIN peoplecol ON sectioncole.ID = peoplecol.division
</cfquery>

Mause
0
 
LVL 10

Expert Comment

by:Mause
ID: 9796890
and the selected fields need to be the same:

 <cfquery name="get" datasource="kidwell" dbtype="ODBC">
SELECT name, lot, id, date, many, cb,division
from sectionA INNER JOIN peopleA ON sectionA.ID = peopleA.division
union
SELECT name, lot, id, date, many, cb,division
from sectionB INNER JOIN peopleBON sectionB.ID = peopleB.division
union
SELECT name, lot, id, date, many, cb,division
from sectionP INNER JOIN peoplePON sectionP.ID= peoplePdivision
union
SELECT name, '' as lot, id, date, '' as many, '' as cb,division
from sectioncol INNER JOIN peoplecol ON sectioncole.ID = peoplecol.division
</cfquery>
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 10

Expert Comment

by:Mause
ID: 9796891
and the selected fields need to be the same:

 <cfquery name="get" datasource="kidwell" dbtype="ODBC">
SELECT name, lot, id, date, many, cb,division
from sectionA INNER JOIN peopleA ON sectionA.ID = peopleA.division
union
SELECT name, lot, id, date, many, cb,division
from sectionB INNER JOIN peopleBON sectionB.ID = peopleB.division
union
SELECT name, lot, id, date, many, cb,division
from sectionP INNER JOIN peoplePON sectionP.ID= peoplePdivision
union
SELECT name, '' as lot, id, date, '' as many, '' as cb,division
from sectioncol INNER JOIN peoplecol ON sectioncole.ID = peoplecol.division
</cfquery>
0
 
LVL 1

Author Comment

by:mflagstad
ID: 9796917
here's the error message:

Error Diagnostic Information
ODBC Error Code = 37000 (Syntax error or access violation)


[Microsoft][ODBC Microsoft Access Driver] Syntax error in FROM clause.



The error occurred while processing an element with a general identifier of (CFQUERY), occupying document position (3:1) to (3:55).
0
 
LVL 1

Author Comment

by:mflagstad
ID: 9797000
Is there maybe a problem with the actual form that is being subitted?
0
 
LVL 10

Expert Comment

by:Mause
ID: 9797106
No its the FROM line in your query
did you pace a [SPACE] between 'sectionB' AND 'INNER' ???

And also between 'sectionP' and 'INNER'

Mause
0
 
LVL 1

Author Comment

by:mflagstad
ID: 9797140
yes... this is exactly what I have:
<cfquery name="get" datasource="kidwell" dbtype="ODBC">
SELECT name, lot, id, date, many, cb,division
from sectionA INNER JOIN peopleA ON sectionA.ID = peopleA.division
union
SELECT name, lot, id, date, many, cb,division
from sectionB INNER JOIN peopleBON sectionB.ID = peopleB.division
union
SELECT name, lot, id, date, many, cb,division
from sectionP INNER JOIN peoplePON sectionP.ID= peoplePdivision
union
SELECT name, '' as lot, id, date, '' as many, '' as cb,division
from sectioncol INNER JOIN peoplecol ON sectioncol.ID = peoplecol.division

</cfquery>

<cfoutput query="get">
<cfif get.recordcount gt 0>
Click on the name below to edit information.<br>
<a href="edit.cfm?id=#id#">
#name##date##id#
</a>
<cfelse>

There were no listings found.  Please try again.
</cfif>
</cfoutput>
</div>

Any clue what the problem is here?
0
 
LVL 10

Expert Comment

by:Mause
ID: 9797198
try this (you needed some more [SPACE]:

<cfquery name="get" datasource="kidwell" dbtype="ODBC">
SELECT name, lot, id, date, many, cb,division
from sectionA INNER JOIN peopleA ON sectionA.ID = peopleA.division
union
SELECT name, lot, id, date, many, cb,division
from sectionB INNER JOIN peopleB ON sectionB.ID = peopleB.division
union
SELECT name, lot, id, date, many, cb,division
from sectionP INNER JOIN peopleP ON sectionP.ID= peopleP.division
union
SELECT name, '' as lot, id, date, '' as many, '' as cb,division
from sectioncol INNER JOIN peoplecol ON sectioncol.ID = peoplecol.division
</cfquery>

0
 
LVL 1

Author Comment

by:mflagstad
ID: 9797633
OK it returned something, just not the right things.
It returned all the names, dates, and id's from ALL the tables. Not just the one I was searching for. How can I make it return just the one, or list of ones that match (like the date for example) or ones that "sound" like the one I was searching for?
Thanks for your help!
0
 
LVL 10

Accepted Solution

by:
Mause earned 100 total points
ID: 9797709
then you also need a WHERE in your query

like this:

SELECT name, lot, id, date, many, cb,division
from sectionA INNER JOIN peopleA ON sectionA.ID = peopleA.division
where date = #form.date#

or something like

SELECT name, lot, id, date, many, cb,division
from sectionA INNER JOIN peopleA ON sectionA.ID = peopleA.division
where name like '%#form.name#%'

0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

In our day to day coding, how many times have we come across a necessity to check whether a URL is a broken link or not? For those of you that answered countless and are using ColdFusion like myself, then this article is for you.  It will show yo…
Article by: kevp75
Hey folks, 'bout time for me to come around with a little tip. Thanks to IIS 7.5 Extensions and Microsoft (well... really Windows 8, and IIS 8 I guess...), we can now prime our Application Pools, when IIS starts. Now, though it would be nice t…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now