VB.net exception error

Hello!

I am creating a form with a database connection to MS Access and I keep getting this error message :

"Syntax error in query. Incomplete query clause"

Here is my code :

Try
            Dim f3DbConn As OleDb.OleDbConnection
            Dim f3DbCommand As OleDb.OleDbDataAdapter
            Dim DSf3 As New DataSet
            Dim f3sql As String
            f3DbConn = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source= Printer_DB.mdb")
            f3DbConn.Open()
            f3sql = "SELECT * FROM '" & CBF3Device.SelectedText & "'"
            f3DbCommand = New OleDb.OleDbDataAdapter(f3sql, f3DbConn)
            f3DbCommand.Fill(DSf3, "f3TableFill")
            f3DbConn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try

What am I doing wrong?

Thanks in advance
abennett10Asked:
Who is Participating?
 
LacutahCommented:
Oh, it's a Combobox.

Instead of using .SelectedText (misleading)
     f3sql = "SELECT * FROM  " & CBF3Device.SelectedText
Use SelectedItem:
     f3sql = "SELECT * FROM [" & CBF3Device.SelectedItem.tostring & "]"
0
 
LacutahCommented:
Remove the quotes from your select command:
     For your SQL, Instead of:
            Select * from 'tablename'
     Use:
            Select * from tablename
     Or (for access):
           Select * from [tablename]
So your code could read:
       f3sql = "SELECT * FROM " & CBF3Device.SelectedText
0
 
abennett10Author Commented:
Now I receive Syntax error in FROM clause:

Try
            Dim f3DbConn As OleDb.OleDbConnection
            Dim f3DbCommand As OleDb.OleDbDataAdapter
            Dim DSf3 As New DataSet
            Dim f3sql As String
            f3DbConn = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & _
            "Data Source= Printer_DB.mdb")
            f3DbConn.Open()
            f3sql = "SELECT * FROM  " & CBF3Device.SelectedText
            f3DbCommand = New OleDb.OleDbDataAdapter(f3sql, f3DbConn)
            f3DbCommand.Fill(DSf3, "f3TableFill")
            f3DbConn.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
LacutahCommented:
Is the tablename valid?  Does it have spaces, or is it a JET reserved word?  That is when you want to use the brackets ([]) around the table name.
0
 
abennett10Author Commented:
The table name should pull what ever is the Text value from the Combobox CBF3Device is the Combobox.
0
 
abennett10Author Commented:
That worked!!!!

Thanks alot! that was great
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.