I am trying to update / save a new record in an access database file through VB.
When I run the application and press the save or update button I am receiving the 2 errors:
Additional information: Syntax error in INSERT INTO statement.
Additional information: Syntax error in UPDATE statement.
Can anyone see the problem with my syntax?
I will attach the code and a screenshot of GUI
Imports System.Data.OleDb
Public Class Form1
Dim dbconn As New OleDbConnection
Dim adt As New OleDbDataAdapter
Dim ds As New DataSet
Dim datatable As New DataTable
Dim cmd As New OleDbCommand
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
dbconn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0; data source = CUBSDatabase.accdb"
showData()
End Sub
Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
adt = New OleDbDataAdapter("insert into Student (FName, SName, Attendance, CA1, CA2, FinalExam) values ( '" & txtFName.Text & "','" & txtSName.Text & "', '" & txtAttendance.Text & "', '" & txtCA1.Text & "', '" & txtCA2.Text & "', '" & txtFinalExam.Text & "', )", dbconn)
adt.Fill(ds)
ds = New DataSet
showData()
MsgBox("Saved")
End Sub
Private Sub showData()
Dim dbcommand As String
dbcommand = "SELECT * FROM Student"
adt = New OleDbDataAdapter(dbcommand, dbconn)
datatable = New DataTable
adt.Fill(datatable)
DataGridView1.DataSource = datatable
End Sub
Private Sub btnFind_Click(sender As Object, e As EventArgs) Handles btnFind.Click
Dim sql = "select * from Student where ID =" & txtID.Text & " "
adt = New OleDbDataAdapter(sql, dbconn)
cmd = New OleDbCommand(sql)
adt.Fill(ds, "Student")
txtFName.Text = ds.Tables("Student").Rows(0)(1).ToString
txtSName.Text = ds.Tables("Student").Rows(0)(2).ToString
txtAttendance.Text = ds.Tables("Student").Rows(0)(3).ToString
txtCA1.Text = ds.Tables("Student").Rows(0)(4).ToString
txtCA2.Text = ds.Tables("Student").Rows(0)(5).ToString
txtFinalExam.Text = ds.Tables("Student").Rows(0)(6).ToString
ds = New DataSet
End Sub
Private Sub TabPage1_Click(sender As Object, e As EventArgs) Handles TabPage1.Click
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
adt = New OleDbDataAdapter("update Student set FName='" & txtFName.Text & "', SName='" & txtSName.Text & "', Attendance='" & txtAttendance.Text & "', CA1'" & txtCA1.Text & "', CA2'" & txtCA2.Text & "', FinalExam'" & txtFinalExam.Text & "'where ID=" & txtID.Text & "", dbconn)
adt.Fill(ds)
ds = New DataSet
showData() ' refresh data in datagridview
MsgBox("Updated")
End Sub
End Class