1
votes

In my program I am connecting to a Access 2010 database, from there I am using a SQL statement to read the top 30 records in that query. The problem I have when I am reading is that I get the error message: "Syntax error (missing operator) in query expression '* b_forte ORDER BY ProdDate DESC'". I am not 100% sure what I am missing inside my statement, and I could not find a solution online. The code that I am using is as follows:

    Public Sub CheckForRepeat()

    Dim mydb As String
    Dim mystr As String

    Dim connection As OleDb.OleDbConnection
    'Connection string to connect to database.
    mystr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Forte\Fortedb.accdb"
    connection = New OleDb.OleDbConnection(mystr)

    'Opening database.
    connection.Open()

    'Select top 30 records from database organizing by the date, and line ID.
    mydb = "SELECT TOP 30 * b_forte ORDER BY ProdDate DESC, BaleLine DESC"

    'Will display the SQL statement if DisplayCode is checked off.
    If GlobalVariables.DisplayCode = True Then
        MainTextBox.AppendText(Environment.NewLine & mydb)
    End If

    Dim run = New OleDb.OleDbCommand

    run = New OleDbCommand(mydb, connection)
    'Executing the SQL statement.
    run.ExecuteNonQuery()
    connection.Close()

End Sub

The error message is firing at run.ExecuteNonQuery(), I think that I must be missing something inside my SQL statement.b_forte, ProdDate, and BaleLine are all valid inside my Access 2010 database, and the connection string is correct, as I use it earlier in my code to connect to the same database.

2

2 Answers

3
votes

You need to use the FROM keyword.

SELECT TOP 30 * FROM b_forte ORDER BY ProdDate DESC, BaleLine DESC
2
votes

You are not including the table name in your query (and therefore no FROM clause)...

Your query should look like this :

SELECT TOP 30 b_forte FROM TableName ORDER BY ProdDate DESC, BaleLine DESC