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();
            }
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. Commented Oct 30, 2019 at 10:34
  • Does this answer your question? How to deal with SQL column names that look like SQL keywords? Commented Oct 30, 2019 at 10:34
  • Whenever you have a question about SQL, add the relevant rdbms in the tags. Commented Oct 30, 2019 at 10:34

2 Answers 2

3

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

 string sqlString = "SELECT CorrDetails FROM [Action] WHERE ProbID=137";
Sign up to request clarification or add additional context in comments.

2 Comments

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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.