Whats wrong in the syntax of my SELECT VBA Statement? - vb.net

What's wrong in the syntax of
"SELECT * FROM Tbl_Appointments WHERE Tbl_Appointments.[MRN] = '" & Me.MRN.Value & "' ORDERBY Tbl_Appointments.[Patient_Name];"
I keep getting a syntax error.
Btw I am not a professional programmer and I just create little databases for my own use in my clinic, and I didn't get this error earlier.

ORDERBY is incorrect, should be ORDER BY

Related

MS Access sql - Update query syntax

Seems a small issue with an sql update query. But I can not get my head around this issue. Not very much acquainted with sql.
Based on a selection, selected rows from a table (7DARphases) are copied and inserted to another table (7tblDAR). And the Project ID (PRJID column) for all the inserted rows should be updated with a fixed value (PID) taken from the form.
Issue is: I am facing syntax issue in the part where the fixed value needs to be inserted in the PRJID column of added rows.
Code is:
Set dbs = CurrentDb
PID = Me.PRJID.Value
sqlstr = "INSERT INTO 7tblDAR (Phase, Deliverable, Link_PIM_temp, Link_PIM_WB, Description, Accept_criteria, Deliv_type, TollgateID, Workstream, Accountable, ClientRACI) SELECT [7DARphases].Phase, [7DARphases].Deliverable, [7DARphases].Link_PIM_temp, [7DARphases].Link_PIM_WB, [7DARphases].Description, [7DARphases].Accept_criteria, [7DARphases].Deliv_type, [7DARphases].TollgateID, [7DARphases].Workstream, [7DARphases].Accountable, [7DARphases].ClientRACI FROM 7DARphases WHERE ((([7DARphases].SolType) Like '*2PP*')) ORDER BY [7DARphases].Phase, [7DARphases].Deliverable;"
sqlUpdt = "update 7tblDAR set PRJID = '" & Me.PRJID.Value & "' from 7tblDAR where tblDAR.PRJID = """""
dbs.Execute sqlstr, dbFailOnError
dbs.Execute sqlUpdt, dbFailOnError
The 'sqlstr' works fine and inserts rows.
But 'sqlUpdt' gives error:
"Run-time error 3075: Syntax error (missing operator) in query expression ..."
Can you please help me out with this.
Plus, if possible, can you suggest to perform this action in one query itself.
Why not put the value in when you insert the values?
sqlstr = "INSERT INTO 7tblDAR (Phase, Deliverable, Link_PIM_temp, Link_PIM_WB, Description, Accept_criteria, Deliv_type, TollgateID, Workstream, Accountable, ClientRACI, PRJID)
SELECT . . .,
'" & Me.PRJID.Value & "'
WHERE [7DARphases].SolType Like '*2PP*')
ORDER BY [7DARphases].Phase, [7DARphases].Deliverable;"
This saves the trouble of having to update it later.

SQL Server error '80040e14' with syntax error nearby "ORDER"

I have been getting the SQL Server error '80040e14' saying Incorrect syntax near the keyword 'ORDER'. However, the line it is pointing is the next line to the line where there is the word "ORDER" Can someone point out the syntax mistake I did in the following sql code? ASP is the language I have been using for this project.
SQL = " SELECT TS.ID, types"&_
" FROM tblTickets TS"&_
" WHERE TS.ID = "& Request("ticketid") &" ORDER BY dateof DESC"
SET RSticket = objConn.Execute(SQL)
So,it points error in the last line whereas "ORDER" is in different line.
If Request("ticketid") returns a string value then you need to add single quotes around it:
SQL = " SELECT TS.ID, types"&_
" FROM tblTickets TS"&_
" WHERE TS.ID = '"& Request("ticketid") &"' ORDER BY dateof DESC"
the ORDER BY will fail, because dateof isnt a selected field
try " WHERE TS.ID = "& Replace(Request("ticketid"),"'","''") &" ORDER BY 1 DESC"
Thank you all, ya I was just missing single quote wrap for & Request("ticketid").Now it works fine.

SQL sort order using 'ORDER BY CASE...'

I'm trying to create a sorting function in SQL but I cannot make this thing work. I have really tried to find some similar problem on Stackoverflow but non that seems to solve my problem. I really not an expert on this things so hope that my explanation is understandable.
So case is as follows: I will list out products an on the website I have links for the user to click that decides what will be sorted, ProductName, ProductID and so on. Chosen sort type is collected from a request.querystring and I use CreateParameter to avoid the risk of SQL injections.
Here is the (simplified) SQL:
"SELECT * FROM Product" & _
"ORDER BY CASE ? " & _
"WHEN 'ID' THEN ProductID " & _
"WHEN 'IDART' THEN ArticleNumber " & _
"ELSE ProductName " & _
"END DESC"
objConnProdList.CommandType = 1
SET objParam = objConnProdList.CreateParameter("#strOrder", adVarchar, adParamInput, 10, "%" & request.querystring("OrderBy") & "%")
Using ELSE I will get the error message:
Conversion failed when converting the nvarchar value 'DiningChair' to data type int.
The problem is that you can't mix different data types in a single case. The syntax would be ok if you convert the values to nvarchars, but that doesn't sort them properly.
If you're using dynamic SQL like in the example, just add the lines to order by you actually need. If you have a stored procedure or something like that, it works with separate order by clauses like this:
SELECT * FROM Product
ORDER BY
CASE #xxx WHEN 'ID' then ProductID end,
case #xxx WHEN 'IDART' THEN ArticleNumber end,
ProductName DESC
i think you make it overly complicated.
var sql = "SELECT * FROM Product";
switch (strOrder)
case when "ID":
sql = sql & " ORDER BY ProductID"
break;
case when "IDART":
sql = sql & " ORDER BY ArticleID"
break;
end;
and run it. there is no place for injection as there is no variable you inject.
This is a variation on JamesZ's answer:
with const as (
select ? as xxx
)
SELECT *
FROM const cross join Product
ORDER BY (CASE xxx WHEN 'ID' then ProductID end),
(case xxx WHEN 'IDART' THEN ArticleNumber end),
ProductName DESC

Convert SQL Server Query to MS Access to Update Table

Could anyone help with this. I don't use Access often, but this process I'm building needs to utilize Access for the business to use. I have the following code, which will not work in Access. I keep getting the error 'Syntax Error (missing operator) in query expression'. Would anyone be able to convert this query into something Access will accept.
UPDATE Auto_DailyDiary_PrimaryTbl a
SET a.TotalDiary = sub.TotalDiary
FROM
(
SELECT CaseEEID, Count(CaseID) as TotalDiary
FROM dbo_Case
WHERE CaseStatus <> 6
GROUP BY CaseEEID
) sub
WHERE sub.EEID = a.EEID AND a.DiaryDt = Date()
Pretty sure Access doesn't like having a joined group by subquery in an update query.
Maybe try something like this?
UPDATE Auto_DailyDiary_PrimaryTbl
SET TotalDiary =
DCOUNT('CaseID', 'dbo_Case',
"CaseStatus <> 6 AND " &
"CaseEEID = '" & Auto_DailyDiary_PrimaryTbl.EEID & "'")
WHERE DiaryDt = Date()

Error in SQL query expression

I´m trying to run the following SQL code on access 2010, but it returns syntax error in query expression. Any ideas what could be causing it?
Thanks in advance.
DELETE FROM Trn_done
WHERE (Trn_done.training =
SELECT Training
FROM Trainings
WHERE (Trainings.Area = '" & Area & "'))
The bracket should be before the nested SQL as mentioned below
DELETE FROM Trn_done
WHERE Trn_done.training IN (
SELECT Training
FROM Trainings
WHERE (Trainings.Area = '" & Area & "')
)
You shouldn't EQUAL a select statement, lest multiple rows in your select cause your code to break. Try this instead:
DELETE td
from Trn_done td
inner join Trainings t
on td.training = t.training
WHERE t.Area = '" & Area & "'