2

I am trying to get a specific single value from an Access Database using C#.

For some reason what I am asking for is giving me an exception of

Syntax error in FROM clause

and I cant work out why.

I have tried running the SQL directly in Access itself and it works fine and gives me back the results I want, but I have no idea why its not working in my program.

ProbID is a number field as far as Access describes it and CorrDetails is a memo field.

For simplicity i have set the SQL to look for a specific value (137) but once i have the code working i will make it paramiterised.

Any ideas?

string corrAct;
            OleDbConnection dbConnection;
            dbConnection = new OleDbConnection(vDbString);
            string sqlString = "SELECT CorrDetails FROM Action WHERE ProbID=137";
            OleDbCommand command = new OleDbCommand(sqlString, dbConnection);

            using (dbConnection)
            {

            MessageBox.Show(sqlString);
            dbConnection.Open();
            corrAct = (String)command.ExecuteScalar();
            rtfCorrectiveAction.Text = Convert.ToString(corrAct);
            dbConnection.Close();
            }
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
Peter
  • 29
  • 3
  • 1
    In the future, you could try to put your SQL query into a Query in Access itself. There you'll find about such errors easily. – Christian.K Oct 30 '19 at 10:34
  • Does this answer your question? [How to deal with SQL column names that look like SQL keywords?](https://stackoverflow.com/questions/285775/how-to-deal-with-sql-column-names-that-look-like-sql-keywords) – BugFinder Oct 30 '19 at 10:34
  • Whenever you have a question about SQL, add the relevant rdbms in the tags. – Zohar Peled Oct 30 '19 at 10:34

2 Answers2

3

Action is a reserved word in MS Access. Wrap it with []:

 string sqlString = "SELECT CorrDetails FROM [Action] WHERE ProbID=137";
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
  • This worked perfectly! Thank you, i didnt know about the Access reserved words and this is a database that i did not have any involvement in creating. Thanks for all your help. – Peter Oct 30 '19 at 11:35
  • [Glad to help :-)](http://meta.stackoverflow.com/questions/291325/how-to-show-appreciation-to-a-user-on-stackoverflow/291327#291327) – Zohar Peled Oct 30 '19 at 11:36
1

The problem is you havent taken into account keywords in SQL. https://learn.microsoft.com/en-us/sql/odbc/reference/appendixes/reserved-keywords?view=sql-server-ver15

Action is a keyword so should not be used really in another context, to use them put [] round them some it becomes

select stuff from [Action] where stuff=true

BugFinder
  • 17,474
  • 4
  • 36
  • 51