1

I'm using MS Access 2003 to generate the DataSource of a DataGridView, when I launch the program, it throws :

    "Syntax error (no operator) in expression " k.Ka = p1.Id                 
       INNER JOIN Person AS p2 ON k.Kc1 = p2.Id
       INNER JOIN Person AS p3 ON k.Kc3 = p3.Id "

My code :

     try
            {

                using (OleDbConnection conn = new OleDbConnection(connecString))
                {

                    conn.Open();

       OleDbCommand cmd = new OleDbCommand(@"Select k.[Desc],k.Family,k.Num,p1.Name 
            AS KeyAdmin, p2.Name AS KeyCustodian1, p3.Name AS KeyCustodian3,p4.Name
            AS SecurityOfficer,p5.Name AS ServiceIT
            FROM KC AS k
            INNER JOIN Person AS p1 ON k.Ka = p1.Id                 
            INNER JOIN Person AS p2 ON k.Kc1 = p2.Id
            INNER JOIN Person AS p3 ON k.Kc3 = p3.Id       
            INNER JOIN Person AS p4 ON k.So = p4.Id  
            INNER JOIN Person AS p5 ON k.It = p5.Id
            WHERE k.Num = @Num;", conn);

               OleDbDataAdapter adapt = new OleDbDataAdapter(cmd);

               cmd.Parameters.AddWithValue("@num", form.comboKC.SelectedValue);


               DataTable dt = new DataTable();

               adapt.Fill(dt);
               form.dataGridView1.DataSource = dt;
               conn.Close();

           }

I'm a student and I can't see my error and that's I use it most of the time, neither can my supervisor so... Here I am ! Thanks for your time,

3
  • 3
    Does the query work when running directly in Access? Commented May 16, 2018 at 14:21
  • 3
    It's been forever since I've last worked with ms-access but I remember it loves parenthesis. try ON (k.Ka = p1.Id) for all your joins. Commented May 16, 2018 at 14:21
  • Is form.comboKC.SelectedValue returning a valid value for you to select on? Commented May 16, 2018 at 14:23

1 Answer 1

1

MS Access' SQL dialect requires parentheses pairings around FROM and JOIN clauses. Consider below adjustment. Be sure to also escape Name, a reserved word.

SELECT k.[Desc], k.Family, k.Num, p1.[Name] AS KeyAdmin, p2.[Name] AS KeyCustodian1, 
       p3.[Name] AS KeyCustodian3, p4.[Name] AS SecurityOfficer, p5.[Name] AS ServiceIT
FROM ((((KC AS k
INNER JOIN Person AS p1 ON k.Ka = p1.Id)                
INNER JOIN Person AS p2 ON k.Kc1 = p2.Id)
INNER JOIN Person AS p3 ON k.Kc3 = p3.Id)       
INNER JOIN Person AS p4 ON k.So = p4.Id)  
INNER JOIN Person AS p5 ON k.It = p5.Id
WHERE k.Num = @Num;
Sign up to request clarification or add additional context in comments.

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.