Trying to connect my vb.net 2019 to access2010

0

I keep geeting an error at da.Fill(ds, "Users")

Private Sub Login_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Dim ctr, i As Integer
    ds.Clear()
    If cn.State = ConnectionState.Open Then
        cn.Close()
    End If
    Module1.Conn()
    cn.Open()
    str = "select * from User"
    cmd = New OleDbCommand(str, cn)
    da.SelectCommand = cmd
    da.Fill(ds, "Users")
    ctr = ds.Tables("Users").Rows.Count - 1
    For i = 0 To ctr
        Dim unused1 = ComboBox1.Items.Add(ds.Tables("Users").Rows(i)(1).ToString)
    Next
End Sub

This the error I keep getting:

System.Data.OleDb.OleDbException HResult=0x80040E14 Message=Syntax error in FROM clause. Source=System.Data StackTrace: at 
System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at 
System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at 
System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at 
System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult) at 
System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior, String method) at 
System.Data.OleDb.OleDbCommand.ExecuteReader(CommandBehavior behavior) at 
System.Data.OleDb.OleDbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) at 
System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at 
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) at 
System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, String srcTable) at 
Praise_Baptist_Int.School.Login.Login_Load(Object sender, EventArgs e) in C:\Users\PEPRAH\source\repos\Praise Baptist Int. School\Form1.vb:line 36 at 
System.EventHandler.Invoke(Object sender, EventArgs e) at 
System.Windows.Forms.Form.OnLoad(EventArgs e) at 
System.Windows.Forms.Form.OnCreateControl() at 
System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) at 
System.Windows.Forms.Control.CreateControl() at 
System.Windows.Forms.Control.WmShowWindow(Message& m) at 
System.Windows.Forms.Control.WndProc(Message& m) at 
System.Windows.Forms.ScrollableControl.WndProc(Message& m) at 
System.Windows.Forms.ContainerControl.WndProc(Message& m) at 
System.Windows.Forms.Form.WmShowWindow(Message& m) at 
System.Windows.Forms.Form.WndProc(Message& m) at 
System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m) at 
System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) at 
System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
vb.net
asked on Stack Overflow Sep 17, 2019 by Peprah Obed Adjei • edited Sep 17, 2019 by jmcilhinney

1 Answer

0

The issue is presumably that "User" is a reserved word in the flavour of SQL you're using. If you get a syntax error in seemingly valid SQL, that is the most likely explanation. You need to escape it to force it to be interpreted as an identifier. In Jet/Access SQL, you do that by wrapping it in brackets, i.e.

SELECT * FROM [User]

It's worth noting that that's exactly what you do in VB in order to use a reserved word as an identifier, e.g.

Public Class [Event]
    '...
End Class
answered on Stack Overflow Sep 17, 2019 by jmcilhinney

User contributions licensed under CC BY-SA 3.0