OleDbCommand syntax error 0x80040E14 in SELECT Statement

2

This query runs perfectly in Access 2010:

SELECT te.[EnterpirseName], COUNT(to.[ID]) AS Orgs, SUM(to.[Members]) AS Mems
FROM tbl_Enterprise AS te, tbl_Organizations AS to
WHERE to.[Enterprise_ID] = te.[ID] AND te.[Status] = 1
GROUP BY te.[EnterpirseName]

However when I try to execute it from my C# winforms application, I get syntax error:

0x80040E14 Syntax error near keyword FROM

EDIT: My C# code:

string strCommand = "SELECT te.[EnterpirseName], COUNT(to.[ID]) AS Orgs, SUM(to.[Members]) AS Mems" +
    " FROM tbl_Enterprise AS te, tbl_Organizations AS to" +
    " WHERE to.[Enterprise_ID] = te.[ID]" +
    " AND te.[Status] = 1" +
    " GROUP BY te.[EnterpirseName]";

DataTable dt = new DataTable();

using (OleDbConnection conn = new OleDbConnection(strConnString))
{
    OleDbDataAdapter da = new OleDbDataAdapter(strCommand, conn);

    using (DataSet ds = new DataSet())
    {
        // THIS IS WHERE I GET THE ERROR
        da.Fill(ds);
        dt = ds.Tables[0];
    }
}

That's how strCommand looks like in Debug:

enter image description here

c#
winforms
oledbdataadapter
asked on Stack Overflow Feb 3, 2017 by firefalcon • edited Feb 3, 2017 by Muhammed Shevil KP

2 Answers

2

TO is a reserved word in Access as well. Try to replace it to something like tbl_Org.

answered on Stack Overflow Feb 3, 2017 by FireFalcon
-2

Are you sure your c# app isn't using another engine, like SQL Server? In SQL Server, TO is a reserved word, so you should use [To] instead.

answered on Stack Overflow Feb 3, 2017 by faibistes

User contributions licensed under CC BY-SA 3.0