2

I am getting following error:

Syntax error (missing operator) in query expression 'tblUnit.Unit_DescFROM tblUnit INNER JOIN tblStocks ON tblUnit.[Unit_Control] = tblStocks.[Stk_LinkUnit]'. andthis one Syntax error in FROM clause. 

Part of code that is failing:

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim connetionString As String
    Dim connection As OleDb.OleDbConnection
    Dim oledbAdapter As New OleDb.OleDbDataAdapter
    Dim ds As New DataSet
    Dim i As Integer
    Dim sql As String
    connetionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=dbInventor.mdb"
    connection = New OleDb.OleDbConnection(connetionString)

    connection.Open()
    sql = "SELECT tblStocks.Stk_LinkUnit, tblUnit.Unit_Desc" _
        & "FROM tblUnit INNER JOIN tblStocks ON tblUnit.[Unit_Control] = tblStocks.[Stk_LinkUnit];"

    oledbAdapter.SelectCommand = New OleDb.OleDbCommand(sql, connection)
    oledbAdapter.Fill(ds)
        oledbAdapter.Dispose()
        connection.Close()
        For i = 0 To ds.Tables(0).Rows.Count - 1
        MsgBox(ds.Tables(0).Rows(i).Item(1))
        Next
End Sub

End Class
5
  • Whet do you mean by break my code ??
    – Maraboc
    Sep 14, 2015 at 15:41
  • 1
    change this "SELECT tblStocks.Stk_LinkUnit, tblUnit.Unit_Desc" to this "SELECT tblStocks.Stk_LinkUnit, tblUnit.Unit_Desc " /// missing space before FROM Sep 14, 2015 at 15:44
  • you need a space after "Unit_Desc" Sep 14, 2015 at 15:45
  • I know the error is due to missing space. Just thought I would provide additional information of the error. I've seen this error in EXCEL VBA which is very similar to Access VBA. There are two different mode for a table. One is CommandText and the other CommandTable. To perform a query the table must be set to CommandText. Apparently the missing space is assuming the table is set to CommandTable. It took me a long time in excel to figure out this confusing error message.
    – jdweng
    Sep 14, 2015 at 16:02
  • thank, i will try suggestion. Sep 15, 2015 at 19:24

3 Answers 3

2

You should add a space before FROM like this :

sql = "SELECT tblStocks.Stk_LinkUnit, tblUnit.Unit_Desc" _
    & " FROM tblUnit INNER JOIN tblStocks ON tblUnit.[Unit_Control] = tblStocks.[Stk_LinkUnit];"
2
sql = "SELECT tblStocks.Stk_LinkUnit, tblUnit.Unit_Desc " _
    & "FROM tblUnit INNER JOIN tblStocks ON tblUnit.[Unit_Control] = tblStocks.[Stk_LinkUnit];"

or

sql = "SELECT tblStocks.Stk_LinkUnit, tblUnit.Unit_Desc" _
    & " FROM tblUnit INNER JOIN tblStocks ON tblUnit.[Unit_Control] = tblStocks.[Stk_LinkUnit];"
0

You can also use an xml literal to format long or complex SQL however you like:

Dim sql = <sql>
        SELECT tblStocks.Stk_LinkUnit, tblUnit.Unit_Desc 
        FROM tblUnit INNER JOIN tblStocks ON 
                        tblUnit.[Unit_Control] = tblStocks.[Stk_LinkUnit]
        WHERE Foo=@p1 
            AND Bar=@p2
        </sql>.Value

Console.WriteLine(sql)

Be sure to include the .Value

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.