System.Data.OleDb.OleDbException: 'Syntax error in FROM clause.' - vb.net

It keeps telling me syntax error in FROM clause. Any thought on fixing this code? I followed the tutorial 100%, somehow this error keeps happen. Also I'm using VS2019 and Microsoft Access 365. This is visual basic .net form.
Public Class Form1
Dim cmdlog As OleDbCommand
Dim drlog As OleDbDataReader
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub button1_Click(sender As Object, e As EventArgs) Handles Clear.Click
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs)
End Sub
Private Sub Btn_Login_Click(sender As Object, e As EventArgs) Handles Btn_Login.Click
Dim conn As New OleDbConnection("Provider= Microsoft.ACE.OLEDB.12.0;Data Source= C:\Users\Jane\Documents\MYSJ GA\MySJ GA\bin\Debug\Mysejahtera1.accdb")
Dim cmdlog As OleDbCommand = New OleDbCommand("Select * from user log in where [user_ID]='" & textBoxuser.Text & "' and [user_pw]='" & textBoxpw.Text & "'", conn)
conn.Open()
Dim drlog As OleDbDataReader = cmdlog.ExecuteReader
If drlog.Read() = True Then
Me.Hide()
Form4.Show()
MsgBox("login successfully")
Else
MsgBox("login failed")
End If
End Sub
End Class

I think you should use square brackets in table name:
"Select * from [user log in] where [user_ID]='"

Related

Combo Box Declaration expected

When I got the value for the combo box from the MySQL database it seems getting error declaration expected in line 4. Can anyone help me? Here I attach my code
Imports MySql.Data.MySqlClient
Public Class Utama
Private Sub LogoutToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles LogoutToolStripMenuItem.Click
Me.Hide()
Dim utama As New Login
utama.Show()
End Sub
Private Sub KeluarToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles KeluarToolStripMenuItem.Click
Me.Close()
End Sub
Private Sub SupplierToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles SupplierToolStripMenuItem.Click
Me.Hide()
Dim supplier As New Supplier
supplier.Show()
End Sub
Private Sub ProdukToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ProdukToolStripMenuItem.Click
Me.Hide()
Dim Produk As New Produk
Produk.Show()
End Sub
Private Sub CetakToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles CetakToolStripMenuItem.Click
Me.Hide()
Dim Cetak As New Cetak
Cetak.Show()
End Sub
Dim connection As New MySqlConnection("Server=127.0.0.1;Database=pembelian;Uid=root;Pwd=;")
Dim da As New MySqlDataAdapter("select * from supplier", connection)
Dim dt As DataTable
da.fill(dt)
ComboBox1.Datasource=dt
ComboBox1.DisplayMember = "npwp"
ComboBox1.ValueMember = "npwp"
End Class
You need to put that code inside a method. You can't just put arbitrary code anywhere in a class. The only thing that can be directly in the class is a declaration. The first three lines are declarations, so they're OK. The last four lines are not. That code should be inside a method somewhere. If you want to execute that code when the form loads then it should be in the Load event handler. This is pretty elementary stuff that any beginners tutorial would cover, so maybe you should work your way through such a tutorial.
First, create a method as shown below and check the exception.
Private Sub Utama_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Try
Dim connection As New MySqlConnection("Server=127.0.0.1;Database=pembelian;Uid=root;Pwd=;")
Dim da As New MySqlDataAdapter("select * from supplier", connection)
Dim dt As new DataTable
da.fill(dt)
ComboBox1.Datasource = dt
ComboBox1.DisplayMember = "npwp"
ComboBox1.ValueMember = "npwp"
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub

ACE.OLEDB.12.O connection not open

Imports System.Data.OleDb
Public Class Form1
Dim Dutycon As OleDbConnection
Dim DutyAdo As OleDbDataAdapter
Dim DutyCmd As OleDbCommandBuilder
Dim DutyI As New Integer
Dim DutyTab As New DataTable
Dim DutyRow As DataRow
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Duty_EmployeeTableAdapter.Fill(Me._Duty_DatabaseDataSet1.Duty_Employee)
Dutycon = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Datasource=Duty-Database.accdb; Presist security info=false;")
DutyAdo = New OleDbDataAdapter("Select * from Duty_Employee", Dutycon)
DutyCmd = New OleDbCommandBuilder(DutyAdo)
Try
Dutycon.Open()
MsgBox("Database opened")
Catch ex As Exception
MsgBox("Database Not connected -- -.. " + ex.Message)
End Try
DutyAdo.Fill(DutyTab)
DataGridView1.DataSource = DutyTab
End Sub
Private Sub Duty_EmployeeBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Duty_EmployeeBindingNavigatorSaveItem.Click
Me.Validate()
Me.Duty_EmployeeBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me._Duty_DatabaseDataSet1)
End Sub
Private Sub Cmd_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmd_Add.Click
Dutycon.Open()
DutyRow = DutyTab.NewRow
DutyRow("Emp_No") = TextBox1.Text
DutyRow("Emp_Name") = TextBox2.Text
DutyRow("Emp_salary") = TextBox3.Text
DutyTab.Rows.Add(DutyRow)
DutyAdo.Update(DutyTab)
MsgBox("New Record added", MsgBoxStyle.Information, "Record Stored")
Dutycon.Close()
End Sub
End Class
Connection () can't open
Could not find installeble ISAM
I have Ms-Office 2007 (Access) and connect to VB.net through "ACE.OLEDB.12.0"

Moving between DataGridView rows via code and show through textboxes

I'm not an experienced programmer and I must have made some misconceptions.
I have two forms, one for the search (FormSearch) and one to show the results (FormMain).
The question is: how can I populate the textboxes with the previous results (click on the btnPrev button) or the next results (click on the btnNext button)?
FormSearch
Imports System.Data.SqlClient
Public Class FormSearch
Private Sub btnsearch_Click(sender As Object, e As EventArgs) Handles btnsearch.Click
Dim con As New SqlConnection("Data Source=" & Form1.server & "," & Form1.port & "; Initial Catalog=" & Form1.database & "; User Id=" & Form1.txtUsername.Text & "; Password=" & Form1.txtPassword.Text & ";")
Dim cmd As New SqlCommand("select * FROM dbo.customers;", con)
Dim adapter As New SqlDataAdapter(cmd)
Dim table As New DataTable
adapter.Fill(table)
DataGridView1.DataSource = table
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.RowIndex >= 0 Then
Dim row As DataGridViewRow
row = Me.DataGridView1.Rows(e.RowIndex)
FormMain.TextBox1.Text = row.Cells("Name").Value.ToString
FormMain.TextBox2.Text = row.Cells("Surname").Value.ToString
FormMain.TextBox3.Text = row.Cells("Age").Value.ToString
Me.Hide()
FormMain.Show()
End If
End Sub
End Class
FormMain
Public Class FormMain
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
FormSearch.Show()
Me.Hide()
End Sub
Private Sub btnPrev_Click(sender As Object, e As EventArgs) Handles btnPrev.Click
'Show prev result
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
'Show next result
End Sub
End Class
Try this:
1) Apply the code below in FormMain
Public Class FormMain
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
FormSearch.Show()
Me.Hide()
End Sub
Private Sub btnPrev_Click(sender As Object, e As EventArgs) Handles btnPrev.Click
BS.Position = BS.Position - 1
DisplayRecords()
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
BS.Position = BS.Position + 1
DisplayRecords()
End Sub
End Class
2) Apply the code below in FormSearch
Imports System.Data.SqlClient
Public Class FormSearch
Private Sub btnsearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim con As New SqlConnection("Data Source=" & Form1.server & "," & Form1.port & "; Initial Catalog=" & Form1.database & "; User Id=" & Form1.txtUsername.Text & "; Password=" & Form1.txtPassword.Text & ";")
Dim adapter As New SqlDataAdapter("select * FROM [dbo.customers]", con)
adapter.Fill(DS, "myTableNane")
BS.DataSource = DS
BS.DataMember = "myTableNane"
DataGridView1.DataSource = BS
End Sub
Private Sub DataGridView1_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
If e.RowIndex >= 0 Then
BS.Position = e.RowIndex
Me.Hide()
FormMain.Show()
End If
End Sub
End Class
2) Create a Module and place the below code.
Module Module1
Public DS As New DataSet
Public WithEvents BS As New BindingSource
Sub DisplayRecords(Optional ByVal table As String = "myTableNane")
FormMain.TextBox1.Text = DS.Tables(table).Rows(BS.Position)("Name").ToString
FormMain.TextBox2.Text = DS.Tables(table).Rows(BS.Position)("Surename").ToString
FormMain.TextBox3.Text = DS.Tables(table).Rows(BS.Position)("Age").ToString
End Sub
End Module
Hope you find it helpful and solved your problem.
Moiyd

Why is does this error happen? "No value given for one or more required parameters"

When I try to click the Button1 ,2 and 3. The error that is stated below happens. This error always occur when I have two tables on my database.
No value given for one or more required parameters
This is my code
Imports System.Data.OleDb
Module MSAccessConnection
Public Function OpenConnection() As String
Dim connString As String = "Provider=Microsoft.ACE.OLEDB.12.0; Data Source=G:\MerdoresNew\OrderData.accdb"
Return connString
End Function
End Module
Public Class Order
Public total As Integer
Public ordered As Integer
Public ordered2 As Integer
Public ordered3 As Integer
Public price As Integer
Dim myConnection As OleDbConnection = New OleDbConnection
Private Sub Order_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'OrderDatabaseDataSet.Items' table. You can move, or remove it, as needed.
End Sub
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myConnection.ConnectionString = OpenConnection()
myConnection.Open()
'the query:
Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Price] FROM [Items] where Item='Cheese Burger'", myConnection)
Dim dr As OleDbDataReader = cmd.ExecuteReader
' the following variable is hold true if user is found, and false if user is not found
Dim Found As Boolean = False
While dr.Read
Found = True
price = dr("Price")
End While
If Found = True Then
ordered = TextBox1.Text * price
Else
MsgBox("Saved!")
Me.Close()
End If
myConnection.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
myConnection.ConnectionString = OpenConnection()
myConnection.Open()
'the query:
Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Price] FROM [Items] where Item='Ham Burger'", myConnection)
Dim dr As OleDbDataReader = cmd.ExecuteReader
' the following variable is hold true if user is found, and false if user is not found
Dim Found As Boolean = False
While dr.Read
Found = True
price = dr("Price")
End While
If Found = True Then
ordered2 = TextBox2.Text * price
Else
MsgBox("Saved!")
End If
myConnection.Close()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
total = ordered + ordered2 + ordered3
Form1.TextBox1.Text = total
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
myConnection.ConnectionString = OpenConnection()
myConnection.Open()
'the query:
Dim cmd As OleDbCommand = New OleDbCommand("SELECT [Price] FROM [Items] where Item='Chicken Sandwich'", myConnection)
Dim dr As OleDbDataReader = cmd.ExecuteReader
' the following variable is hold true if user is found, and false if user is not found
Dim Found As Boolean = False
While dr.Read
Found = True
price = dr("Price")
End While
If Found = True Then
ordered3 = TextBox3.Text * price
Else
MsgBox("Saved!")
End If
myConnection.Close()
End Sub
End Class
I don't see where you are passing your connection string to the button click handler. You DIM the connection string as connString, but you never call it in the handler.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
myConnection.ConnectionString = OpenConnection() <----- Change ConnectionString to connString
myConnection.Open()
I think that's the variable that you're missing. I would have commented but I don't have enough rep to comment yet.

Throw New NotImplementedException - VB

I'm making an application that manages the inputs and outputs of a company's customers as well as the creation of the same, however when I "submit" the form of the creation of a new customer to me the following error:
"Throw New NotImplementedException"
I leave here my code, so they can help me.
FORM1.vb
Public Class Form1
Private Property cnt As Object
<SerializableAttribute> _
'<ComVisibleAttribute(True)> _
Public Class NotImplementedException _
'Inherits SystemException
End Class
Private Sub TabPage1_Click(sender As Object, e As EventArgs)
Dim cnt As New OleDb.OleDbConnection
End Sub
Private Sub Label1_Click(sender As Object, e As EventArgs)
End Sub
Private Sub GroupBox1_Enter(sender As Object, e As EventArgs)
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
TabControl1.SelectedTab = TabPage2
End Sub
Private Sub TabPage2_Click(sender As Object, e As EventArgs) Handles TabPage2.Click
End Sub
Private Sub PictureBox1_Click(sender As Object, e As EventArgs) Handles PictureBox1.Click
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'cnt.ConnectionString = "Provinder=Microsoft.Jet.Oledb.40; Data Source=" & Application.StartupPath & "\bdtesteentradas.mdb"
'TODO: This line of code loads data into the 'BdtesteentradasDataSet.empresa' table. You can move, or remove it, as needed.
Me.EmpresaTableAdapter.Fill(Me.BdtesteentradasDataSet.empresa)
End Sub
Private Sub FillByToolStripButton_Click(sender As Object, e As EventArgs) Handles FillByToolStripButton.Click
Try
Me.EmpresaTableAdapter.FillBy(Me.BdtesteentradasDataSet.empresa)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
Me.EmpresaBindingSource.Filter = "nif ='" & TextBox1.Text & "'"
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
TextBox2.Text = ""
TextBox3.Text = ""
TextBox6.Text = ""
TextBox7.Text = ""
TextBox8.Text = ""
TextBox5.Text = ""
TextBox4.Text = ""
End Sub
Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'Dim cnt As New OleDb.OleDbCommand
If Not ConnectionState.Open Then
End If
Cmd.Connection = cnt()
Cmd.CommandText = "INSERT INTO empresa (NIF, Empresa, nCliente, Distrito, NomeContacto, ApelidoContacto, Funcao" &
"VALUES(" & Me.TextBox6.Text & ")"
'Cmd.ExecuteNonQuery()
MsgBox("Dados inseridos.")
End Sub
Cmd.vb
Class Cmd
Shared Property CommandText As String
Shared Property Connection As Object
Shared Sub ExecuteNonQuery()
Throw New NotImplementedException
End Sub
End Class
Error message
Already a big thank you
You got error on your "cmd" variable and "fixed it" this way:
Didn't you? This generated the file Cmd.vb for you.
That is not what you want. Remove Cmd.vb from you project an try this:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
'You want to check, what the State of *your* connection is
If Not cnt.State = ConnectionState.Open Then
End If
'Create a Command-Object from the Connection
Using cmd = cnt.CreateCommand()
cmd.CommandText = "INSERT INTO empresa (NIF, Empresa, nCliente, Distrito, NomeContacto, ApelidoContacto, Funcao" &
"VALUES(" & Me.TextBox6.Text & ")"
cmd.ExecuteNonQuery()
MsgBox("Dados inseridos.")
End Using
End Sub