I have this SQL:
SELECT
dbo.ARTRNTBL.Item_code, dbo.ARTRNTBL.Item_name, dbo.FAFPHTBL.rate_value,
dbo.FAFPHTBL.quantity, dbo.FAFPHTBL.total, dbo.FAFPHTBL.TIN, dbo.EGRDGTBL.return_type
FROM
dbo.FAFPHTBL
INNER JOIN
dbo.ARTRNTBL
INNER JOIN
dbo.EGRDGTBL ON dbo.FAFPHTBL.Item_code = dbo.ARTRNTBL.Item_code
ON dbo.ARTRNTBL.Item_code = dbo.EGRDGTBL.Item_code
WHERE
dbo.FAFPHTBL.invoice_no = '" & TextBox39.Text & "'"
I keep getting this error:
The multi-part identifier "dbo.FAFPHTBL.Item_code" could not be bound.
How can I fix this?
The problem is the way the join expressions are nested. At the time dbo.FAFPHTBL.Item_code is used, it's part of this expression:
dbo.ARTRNTBL INNER JOIN dbo.EGRDGTBL ON dbo.FAFPHTBL.Item_code = dbo.ARTRNTBL.Item_code
Notice the dbo.FAFPHTBL object is not included with this expression.
You probably want this instead:
SELECT dbo.ARTRNTBL.Item_code, dbo.ARTRNTBL.Item_name, dbo.FAFPHTBL.rate_value,
dbo.FAFPHTBL.quantity, dbo.FAFPHTBL.total, dbo.FAFPHTBL.TIN, dbo.EGRDGTBL.return_type
FROM dbo.FAFPHTBL
INNER JOIN dbo.ARTRNTBL ON dbo.FAFPHTBL.Item_code = dbo.ARTRNTBL.Item_code
INNER JOIN dbo.EGRDGTBL ON dbo.ARTRNTBL.Item_code = dbo.EGRDGTBL.Item_code
WHERE dbo.FAFPHTBL.invoice_no = #InvoiceNumber
While I'm here:
DO NOT USE STRING CONCATENATION LIKE THAT TO INCLUDE A TEXTBOX IN THE QUERY!
And yes, I know I'm shouting, because it's that important. If you're not using parameterized queries, you're practically begging for problems with your app, some of them pretty serious security issues. If you set me down in front of your app, I could easily delete all your data or elevate my permission to admin level.
Related
I have been trying to create a form which contains information from 3 tables. On this form I would like to have a filter combo box so that the user may select a member type and it will filter the list to members with that member type.
Members in our database can have more than one type. For this reason I created a memberTable:
MemberID
FName
SName
etc.
A membertype table:
MemberTypeID
MemberTypeName
and a joining table membermembertype:
MemberID
MemberTypeID
I have tried to set my form up with a number of different queries:
SELECT qryMemberType.MemberID, qryMemberType.FName, qryMemberType.SName,
qryMemberType.MemberTypeName, qryMemberType.MemberTypeID FROM qryMemberType;
SELECT [member].[MemberID] AS [member_MemberID], [member].[FName], [member].[SName],
[membertype].[MemberTypeID] AS [membertype_MemberTypeID], [membertype].[MemberTypeName],
[membermembertype].[MemberID] AS [membermembertype_MemberID], [membermembertype].[MemberTypeID]
SELECT [member].[MemberID] AS [member_MemberID], [member].[FName], [member].[SName],
[membermembertype].[MemberID] AS [membermembertype_MemberID],
[membermembertype].[MemberTypeID] AS [membermembertype_MemberTypeID],
[membertype].[MemberTypeID] AS [membertype_MemberTypeID]
The VBA for the combo box that I have been attempting to filter with is:
Dim myMember As String
myMember = "SELECT ... FROM ... WHERE ([MemberTypeID] = " & Me.cboMemberType & ");"
Me.frmFilterTestSub.Form.RecordSource = myMember
Me.frmFilterTestSub.Form.Requery
I have tried numerous different syntaxes for the SELECT statement in the VBA for the combo box including:
"Select * from membermembertype where ([MemberTypeID] = " & Me.cboMemberType & ")"
"SELECT member.MemberID AS member_memberID, member.FName, member.SName,
membertype.MemberTypeID AS membertype_MemberTypeID, membertype.MemberTypeName,
membermembertype.MemberID AS membermembertype_MemberID,
membermembertype.MemberTypeID AS membermembertype_MemberTypeID
FROM member
LEFT JOIN (membertype
RIGHT JOIN membermembertype ON membertype.[MemberTypeID] = membermembertype.[MemberTypeID])
ON member.[MemberID] = membermembertype.[MemberID]
WHERE ([MemberTypeID] = " & Me.cboMemberType & ")"
"SELECT member.*,
(membermembertype.MemberID AS mmt_MemberID),
(membermembertype.MemberTypeID AS mmt_MemberTypeID),
membertype.MemberTypeName
FROM (member
LEFT JOIN membermembertype ON mmt_MemberID = (member.MemberID AS m_MemberID))
RIGHT JOIN membertype ON mmt_MemberTypeID=(membertype.MemberTypeID AS mt_MemberTypeID)
WHERE ([MemberTypeID] = " & Me.cboMemberType & ");"
I'm pretty sure the first example returned the closest result with the list returning the correct number of records for each member type selected, however the fields were filled with the #Name? error value which is why I was trying to do a number of different ways to join the tables. The other SELECT STATEMENTS are returning SQL Syntax errors.
Any advice would be greatly appreciated!
Since you only want to list the members, I guess that you are not really interested with membertype table info, then you can simply do this :
"SELECT member.MemberID AS member_memberID, member.FName, member.SName
FROM member
INNER JOIN membermembertype
ON (membermembertype.[MemberTypeID] = " & Me.cboMemberType & " AND member.[MemberID]=membermembertype.[MemberID]) "
Right outer join is highly frowned on in SQL, when you can use a left outer join in the correct order instead.
In your case, inner joins are better anyway.
You should use a parameter rather than string concatenation if you care about security, but since you're using Access I guess anyone with access to the file can already do what they like.
Please try this one and let me know if you get an error.
SELECT [Member].MemberId
, [Member].FName
, [Member].SName
, [MemberType].MemberTypeId
, [MemberType].MemberTypeName
FROM ((MemberType
INNER JOIN MemberMemberType ON MemberMemberType.MemberTypeId = MemberType.MemberTypeId)
INNER JOIN Member ON Member.MemberId = MemberMemberType.MemberId)
WHERE MemberType.MemberTypeId = 123 --Put your parameter here once you've tested that it works for a specific value
I'm a long time follower of Stack overflow but this is my first post. I'm hoping the community can help.
I have a successful Access Query that returns the required results - Perfect!
HOWEVER, I'm trying to return the same using OLEDB connection to the database within an ASP script. This is all legacy stuff however we are allowing web access to this legacy information.
MS Access (2016) shows Query as this... (works)
SELECT [EventName] & ": " & [RoundCaption] AS RoundTitle, ChunkEntryTable.WinPos
FROM ((EventTable INNER JOIN EventRoundTable ON EventTable.EventId = EventRoundTable.EventId) INNER JOIN ((RoundHeatTable INNER JOIN ChunkTable ON RoundHeatTable.RoundHeatId = ChunkTable.RoundHeatId) INNER JOIN (EventEntryTable INNER JOIN ChunkEntryTable ON EventEntryTable.EventEntryId = ChunkEntryTable.EventEntryId) ON ChunkTable.ChunkId = ChunkEntryTable.ChunkId) ON EventRoundTable.RoundKeyId = RoundHeatTable.RoundKeyId) LEFT JOIN EventEntryMemberTable ON EventEntryTable.EventEntryId = EventEntryMemberTable.EventEntryId
WHERE (((EventEntryTable.Entry1Id)=[EntryId])) OR (((EventEntryTable.Entry2Id)=[EntryId])) OR (((EventEntryTable.Entry3Id)=[EntryId])) OR (((EventEntryMemberTable.MemberId)=[EntryId]))
ORDER BY EventTable.SortIdx, EventRoundTable.RoundId DESC , EventRoundTable.IsRepechage DESC;
Doing this in OLEDB. Connection string as follows...
<%
' FileName="Connection_ado_conn_string.htm"
' Type="ADO"
' DesigntimeType="ADO"
' HTTP="true"
' Catalog=""
' Schema=""
Dim MM_csresultdb_STRING
MM_csresultdb_STRING = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=xyz.mde;Jet OLEDB:Database Password=xxxxxxxxx;"
%>
Connection works perfectly but I can't seem to get the SQL command to work. I get "No value given for one or more required parameters".
NOTE: I have replaced [EntryID] in 4 places with a valid value and it works perfectly in Access just not outside of Access using OLEDB. Here's what the SQL is I'm using...
SELECT EventTable.EventName & ": " & EventRoundTable.RoundCaption AS RoundTitle, ChunkEntryTable.WinPos FROM
((EventTable INNER JOIN EventRoundTable ON EventTable.EventId = EventRoundTable.EventId) INNER JOIN
((RoundHeatTable INNER JOIN ChunkTable ON RoundHeatTable.RoundHeatId = ChunkTable.RoundHeatId) INNER JOIN
(EventEntryTable INNER JOIN ChunkEntryTable ON EventEntryTable.EventEntryId = ChunkEntryTable.EventEntryId) ON ChunkTable.ChunkId = ChunkEntryTable.ChunkId) ON ChunkTable.ChunkId = ChunkEntryTable.ChunkId)
ON EventRoundTable.RoundKeyId = RoundHeatTable.RoundKeyId)
WHERE ((EventEntryTable.Entry1Id)=4741) OR ((EventEntryTable.Entry2Id)=4741) OR ((EventEntryTable.Entry3Id)=4741)
ORDER BY EventTable.SortIdx, EventRoundTable.RoundId DESC , EventRoundTable.IsRepechage DESC;
FOUND PROBLEM ** See answer below
FOUND PROBLEM ** It's to do with this part of the SQL...
[EventName] & ": " & [RoundCaption] AS RoundTitle
Changed to
[EventName], [RoundCaption] AS RoundTitle
and it works but gives me two separate fields rather than the one concatenated field called "RoundTitle". So I'll join the two result fields during the display output rather than at the query stage.
Whew! That many days to figure out. Thanks to the comments that kinda steered me in that direction of the AS part of the statement.
I am not sure why my query is netting me the following error:
The SQL statement could not be executed because it contains ambiguous outer joins. To force one of the joins to be performed first, create a separate query that performs the firm join and then include that query in your SQL statement.
This is my query, I only see one join:
SELECT PC.[Mother_Board_Name] & ',' & PC.[Mother_Board_Manufacturer]
FROM PersonalComputerHardware PC, Registers R
WHERE ',' & R.[Names].Value & ',' LIKE '*,' & PC.[Computer_ID] & ',*';
R.Names.Value is a reference to a multi-value field, by the way. I don't understand this error message because I am only using one Cross JOIN
Try this:
SELECT PC.Mother_Board_Name & ',' & PC.Mother_Board_Manufacturer
FROM PersonalComputerHardware PC
INNER JOIN Registers R ON R.Names LIKE '*,' & PC.Computer_ID & ',*'
Assuming your actual logic is correct, this should work.
However your logic is probably not correct. At best it will only match Computer_ID that is in the middle (not first or last) unless your Names has commas at the beginning and end as well.
The VBA code is:
''sql1 = "select InvoiceCNotes.[docsetamount] ,InvoiceCNotes.[docsetamount] + AllocationsTEMP.[paidamount] from AllocationsTEMP inner join InvoiceCNotes on AllocationsTEMP.Docnumber = InvoiceCNotes.Docnumber where InvoiceCNotes.[docnumber] = AllocationsTEMP.[docnumber] AND AllocationsTEMP.[paidamount] <> 0"
sqlline = sql1
DoCmd.RunSQL sql1
The code as displayed in sqlline above, is as follows:
Update InvoiceCNotes
set InvoiceCNotes.[docsetamount] = InvoiceCNotes.[docsetamount] + AllocationsTEMP.[paidamount]
from AllocationsTEMP inner join InvoiceCNotes
on AllocationsTEMP.Docnumber = InvoiceCNotes.Docnumber
where InvoiceCNotes.[docnumber] = AllocationsTEMP.[docnumber]
AND AllocationsTEMP.[paidamount] <> 0
I have looked at other questions here regarding the same error, but still I am missing something.
From previous questions, I added the table names, and bracketed the field names.
I checked the table specs to see that docsetamount and paidamount are both defined as [NUMBER,double,fixed,2], the two docnumbers are both long integers, and paidamount is also NUMBER,double,fixed,2
Now I am possibly staring into the problem and not noticing my error, as I have developed quite a few apps in Access over the past five years (since retirement I should add) so I must have done something wrong.
Do you notice the mistake?
The UPDATE t1 SET t1.f=foo FROM t1 JOIN t2 syntax with the FROM is from Sql Server.
In Access SQL you put the JOIN in the first clause, like this:
UPDATE InvoiceCNotes INNER JOIN AllocationsTEMP
ON InvoiceCNotes.Docnumber = AllocationsTEMP.Docnumber
SET InvoiceCNotes.[docsetamount] = InvoiceCNotes.[docsetamount] + AllocationsTEMP.[paidamount]
WHERE AllocationsTEMP.[paidamount] <> 0
where InvoiceCNotes.[docnumber] = AllocationsTEMP.[docnumber] is superfluous because it's already in the JOIN condition.
I found a totally different syntax - and it works just fine.
Maybe my original source was wrong, as I did exactly hat it said.
My new syntax for the update is:
UPDATE InvoiceCNotes INNER JOIN AllocationsTEMP
ON InvoiceCNotes.Docnumber = AllocationsTEMP.Docnumber
SET InvoiceCNotes.docsetamount = InvoiceCNotes.[docsetamount]+AllocationsTEMP.[paidamount]
WHERE (((InvoiceCNotes.docnumber)=[AllocationsTEMP].[docnumber])
AND ((AllocationsTEMP.paidamount)<>False))
Maybe this question should just be removed - it will cause confusion rather than to be helpful. I can't do that, or I would.
Apologies to whoever might have tried to help - and thanks for the effort.
This is now SOLVED
I need a little help developing my SQL query. My goal is to remove an entry. My condition is two tables away. I've gotten this far but I can't seem to find where the other mistake
con.Execute "DELETE FROM Expenses INNER JOIN Agreements ON Agreements.AgreementsID =
Expenses.AgreementID AND INNER JOIN Audits
ON Audits.AuditID = Agreements.AuditID WHERE Audits.Share = True"
I'm using access 2007 and my con variable is
con.Open _
"Driver={Microsoft Access Driver (*.mdb, *.accdb)};" & _
"Dbq=" & Loc & ";"
Try This, You are using AND before INNER JOIN, remove that only.
DELETE DISTINCTROW Expenses.*
FROM Expenses
INNER JOIN Agreements ON Agreements.AgreementsID = Expenses.AgreementID
INNER JOIN Audits ON Audits.AuditID = Agreements.AuditID
WHERE Audits.Share = True
[EDIT Query]
DELETE Expenses.*
FROM Expenses
WHERE Expenses.AgreementID IN (
SELECT Agreements.AgreementID FROM Agreements
INNER JOIN Audits ON Audits.AuditID = Agreements.AuditID
WHERE Audits.Share = True
)
OP get error:- Too few parameters Expected 2.
To handle this, This error may be obtained because the any column names being selected have special characters in it. If there are special characters in the column names of the database, the name should be surrounded with brackets in the SQL query. So if you have any column name which have special char value , then try [ColumnName]