Mohjo
Mohjo

Reputation: 37

Syntax error in FROM clause vb.net

Just to let you know this is my first question im posting on this website, so please if I've missed anything just let me know. The problem im having is this, when the program runs im getting the following error message

Syntax error in FROM clause

The code is here

    Dim dbProvider As String
    Dim dbSource As String
    Dim con As New OleDb.OleDbConnection
    Dim ds As New DataSet
    Dim MaxRows As Integer
    Dim sql As String
    Dim TableName As String
    Dim TableCreate As New OleDb.OleDbCommand("CREATE TABLE [" & TableName & "](" & "ID INTEGER NOT NULL," & ")", con)
    Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM  [" & TableName & "]", con)
    Dim cb As New OleDb.OleDbCommandBuilder(da)

    TableName = TbTableName.Text

    dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
    dbSource = "Data Source = E:\A2 Computing\Project\PasswordDatabase.mdb"

    con.ConnectionString = dbProvider & dbSource

    con.Open()

    da.Fill(ds, "NewTable")

    MaxRows = ds.Tables("NewTable").Rows.Count

The Problem im having is at this line

    da.Fill(ds, "NewTable")

Im not sure what I've missed out, Im guessing its something to do with the brackets when I declare tablecreate or da? =S Any help would be greatly appreciated. =)

Upvotes: 0

Views: 4202

Answers (1)

Ken White
Ken White

Reputation: 125689

You need to assign something to TableName before you use it, not after. Your current SQL is SELECT * FROM [], which is why you're getting a syntax error there.

Change the order of your code:

Dim TableName As String
TableName = TbTableName.Text

Dim TableCreate As New OleDb.OleDbCommand("CREATE TABLE [" & TableName & "](" & _
   "ID INTEGER NOT NULL" & ")", con)
Dim da As New OleDb.OleDbDataAdapter("SELECT * FROM  [" & TableName & "]", con)
Dim cb As New OleDb.OleDbCommandBuilder(da)

You also need to execute the TableCreate statement. IIRC, you use ExecuteNonQuery to do so.

TableCreate.ExecuteNonQuery;

Your table create SQL could be simplified, BTW:

' Remove unnecessary concatenation for column definition, as it's always
' the same
Dim TableCreate As New OleDb.OleDbCommand("CREATE TABLE [" & TableName & _
  "](ID INTEGER NOT NULL)", con)

Where are you actually connecting to the database? I don't see an OleDBConnection anywhere in your code?

Here's a full example from the MSDN documentation for OleDBCommand.ExecuteNonScalar:

Private Sub CreateOleDbCommand( _
      ByVal queryString As String, ByVal connectionString As String)
  Using connection As New OleDbConnection(connectionString)
    connection.Open()
    Dim command As New OleDbCommand(queryString, connection)
    command.ExecuteNonQuery()
  End Using
End Sub

Also, you are aware that doing a CREATE TABLE creates a new, empty table, and therefore your SELECT * FROM it won't return anything but a single NULL row because you're not inserting any data?

Upvotes: 1

Related Questions