Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 274
  • Last Modified:

MSACCESS SQL Error with ColdFusion

OK

Here are two tables I am trying to join and output in my CF page.

tbldept
DeptID               Primary Key            Autonumber
Dept                  Text

tblCust
CustID               PrimaryKey             Autonumber
Fname               Text
Lname               Text
Phone                Text
Email                 Text
DeptID               Number



I created an SQL query with acess and here is the SQL it uses when I want to query Fname, Lname, Phone and Email.

SELECT tblDept.DeptID, tblCust.Fname, tblCust.Lname, tblCust.Phone, tblCust.Email
FROM tblDept INNER JOIN tblCust ON tblDept.DeptID=tblCust.DeptID
WHERE DeptID=<cfqueryparam cfsqltype="CF_SQL_INTEGER"
       value="#url.DeptID#"

Now on the page before, I have a link that looks like this.
<a href="deptlookup.cfm?DeptID=#DeptID#">Dept</a>
When they click on this it will take them to the page above.

This is the error I am getting.
-------------------------------------------------------------------------------------------------------------------------
Error Diagnostic Information
ODBC Error Code = S1000 (General error)


[Microsoft][ODBC Microsoft Access Driver] The specified field 'DeptID' could refer to more than one table listed in the FROM clause of your SQL statement.


SQL = "SELECT tblDept.DeptID, tblCust.Fname, tblCust.Lname, tblCust.Phone, tblCust.Email FROM tblDept INNER JOIN tblCust ON tblDept.DeptID=tblCust.DeptID WHERE DeptID=?"

Query Parameter Value(s) -

Parameter #1 = 5

Data Source = "SOFTTRACK"
------------------------------------------------------------------------------------------------------------------------

Now usually I just figure out diffent way to query the tables in what order, but this one is stumping me.

Can anyone help, this should not be this hard and for some reason, I feel like I am missing something.



0
fixx17
Asked:
fixx17
  • 3
  • 2
1 Solution
 
CFDevHeadCommented:
You have to tell the database which table to test deptid
example

WHERE tblDept.DeptID=1
0
 
CFDevHeadCommented:
The reason you got that error is that DeptID is not unique to that query meaning DeptID is in both tables.  That is why you need to reference the table before the felid like so

SELECT tblDept.DeptID, tblCust.Fname, tblCust.Lname, tblCust.Phone, tblCust.Email FROM tblDept INNER JOIN tblCust ON tblDept.DeptID=tblCust.DeptID WHERE tblDept.DeptID=1
0
 
fixx17Author Commented:
Actually, how would that work?

Because I am passing a variable from the link.

For instance:<a href="deptlookup.cfm?DeptID=#DeptID#">Dept</a>

then that number is passed over from the index.cfm page to deptlookup.cfm page.

That is why I have it like below.

WHERE DeptID=<cfqueryparam cfsqltype="CF_SQL_INTEGER"
      value="#url.DeptID#"


OR are you saying I should write it like this.
WHERE DeptID=<cfqueryparam cfsqltype="CF_SQL_INTEGER"
      value="#url.tblDept.DeptID#"


0
 
fixx17Author Commented:
Ok, never mind,

I did it like this

WHERE tblDept.DeptID=<cfqueryparam cfsqltype="CF_SQL_INTEGER"
       value="#url.DeptID#


Sorry about that.. thanks for the help.
0
 
CFDevHeadCommented:
The variable is not the problem.  Its the where clasue.

You have
Where DeptID= whatever

it should be
Where  tblDept.DeptID= whatever

or
Where  tblCust.DeptID= whatever
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now