OleDbCommandBuilder creates SQL statements that result in "syntax error" - vb.net

Can someone please explain why, when I click the "Commit" button, I get the error
Syntax error in INSERT INTO statement.
Here's the code.
Public Class Form3
Dim inc As Integer
Dim MaxRows As Integer
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = D:\TA_Officers.mdb"
con.Open()
sql = "SELECT * from TA_OFFICER"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "TA_Officers")
con.Close()
MaxRows = ds.Tables("TA_Officers").Rows.Count
inc = -1
End Sub
Private Sub NavigateRecords()
txtFName.Text = ds.Tables("TA_Officers").Rows(inc).Item(1)
txtMInitial.Text = ds.Tables("TA_Officers").Rows(inc).Item(2)
txtLName.Text = ds.Tables("TA_Officers").Rows(inc).Item(3)
txtContact.Text = ds.Tables("TA_Officers").Rows(inc).Item(4)
txtEmail.Text = ds.Tables("TA_Officers").Rows(inc).Item(5)
txtPosition.Text = ds.Tables("TA_Officers").Rows(inc).Item(6)
txtCourse.Text = ds.Tables("TA_Officers").Rows(inc).Item(7)
txtAddress.Text = ds.Tables("TA_Officers").Rows(inc).Item(8)
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
BtnCommit.Enabled = True
BtnAdd.Enabled = False
BtnUpdate.Enabled = False
BtnDel.Enabled = False
txtPosition.Clear()
txtLName.Clear()
txtFName.Clear()
txtMInitial.Clear()
txtAddress.Clear()
txtCourse.Clear()
txtEmail.Clear()
txtContact.Clear()
End Sub
Private Sub RdMember_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RdMember.CheckedChanged
Label2.Visible = False
txtPosition.Visible = False
End Sub
Private Sub RdOfficer_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RdOfficer.CheckedChanged
Label2.Visible = True
txtPosition.Visible = True
End Sub
Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("TA_Officers").Rows(inc).Item(1) = txtFName.Text
ds.Tables("TA_Officers").Rows(inc).Item(2) = txtMInitial.Text
ds.Tables("TA_Officers").Rows(inc).Item(3) = txtLName.Text
ds.Tables("TA_Officers").Rows(inc).Item(4) = txtContact.Text
ds.Tables("TA_Officers").Rows(inc).Item(5) = txtEmail.Text
ds.Tables("TA_Officers").Rows(inc).Item(6) = txtPosition.Text
ds.Tables("TA_Officers").Rows(inc).Item(7) = txtCourse.Text
ds.Tables("TA_Officers").Rows(inc).Item(8) = txtAddress.Text
da.Update(ds, "TA_Officers")
MsgBox("Data Updated!")
End Sub
Private Sub BtnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDel.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("TA_Officers").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
NavigateRecords()
da.Update(ds, "TA_Officers")
End Sub
Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click
BtnCommit.Enabled = False
BtnAdd.Enabled = True
BtnUpdate.Enabled = True
BtnDel.Enabled = True
inc = 0
NavigateRecords()
End Sub
Private Sub BtnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCommit.Click
If inc <> -1 Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("TA_Officers").NewRow()
dsNewRow.Item("Firstname") = txtFName.Text
dsNewRow.Item("Middleinitial") = txtMInitial.Text
dsNewRow.Item("Lastname") = txtLName.Text
dsNewRow.Item("Mobilenumber") = txtContact.Text
dsNewRow.Item("Emailaddress") = txtEmail.Text
dsNewRow.Item("Position") = TxtPosition.Text
dsNewRow.Item("Course") = txtCourse.Text
dsNewRow.Item("Address") = txtAddress.Text
ds.Tables("TA_Officers").Rows.Add(dsNewRow)
**da.Update(ds, "TA_Officers")**
MsgBox("New Record added to the Database")
BtnCommit.Enabled = False
BtnAdd.Enabled = True
BtnUpdate.Enabled = True
BtnDel.Enabled = True
End If
End Sub
Private Sub BtnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBack.Click
Form2.Show()
Me.Close()
End Sub
Private Sub TA_MEMBERSBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.TA_MEMBERSBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.TA_OfficersDataSet)
End Sub
End Class

Position is a reserved word for JET 4.0 - try to rename the column something else such as prog_position. Access will often let you create the column with a reserved name, but when you go to access it in code JET blocks it and gives you a generic Syntax error in INSERT INTO Statement error without being specific. Check out http://support.microsoft.com/kb/248738 for more information on reserved words. It is goods practice to always prefix your column names with something short so that you never run into conflicts or reserved word issues.

As Makita mentions, your problem stems from the fact that Position is a reserved word in Jet/ACE SQL. The solution is to add the following two lines of code after you create the OleDbCommandBuilder object:
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
That will tell the OleDbCommandBuilder to generate SQL statements like this
INSERT INTO [TA_OFFICER] ([FirstName], ...
...instead of
INSERT INTO TA_OFFICER (FirstName, ...
Wrapping the Position column name in those square brackets will tell the Jet database engine that it is a column name, not a keyword.

This kind of error can happen when you have different data type in the code and in the DB.
Checking the log can give some light on this.

Related

Passing Values from (DataGridView1_Click) in a Form to another sub in another form

I want to pass value from (DataGridView1_Click) to another sub which is in another form
How To Achieve that?
Is there any way to do that, Please help me
Public Class SearchCustomers
Private Sub SearchCustomers_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
txtCustomerSearchBox.Text = ""
CBCustomerSearch.SelectedIndex = -1
txtCustomerSearchBox_TextChanged(Nothing, Nothing)
End Sub
This the click Event
' Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
'FrmCustomers mycustomers = New FrmCustomers()
' mycustomers.show_data(DataGridView1.CurrentRow.Cells(1).Value.ToString)
' End Sub
Private Sub DataGridView1_RowsAdded(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewRowsAddedEventArgs) Handles DataGridView1.RowsAdded
For I = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(I).Cells(0).Value = "Go"
Dim row As DataGridViewRow = DataGridView1.Rows(I)
row.Height = 25
Next
End Sub
Private Sub DataGridView1_CellContentClick( ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub DataGridView1_Click( ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
Dim oForm As New FrmCustomers()
Dim CustomerCode As String
CustomerCode = (DataGridView1.CurrentRow.Cells(1).Value.ToString)
oForm.show_data(CustomerCode)
MsgBox(DataGridView1.CurrentRow.Cells(1).Value.ToString, MsgBoxStyle.Exclamation, "Warning Message")
End Sub
End Class
this is the sub method in form 2
I want from this method to show data from DB to TextBox as seen in the code below
Sub show_data(CustomerCod)
OpenFileDialog1.FileName = ""
Dim sqls = "SELECT * FROM Customers WHERE CustomerCode=N'" & (CustomerCod) & "'"
Dim adp As New SqlClient.SqlDataAdapter(sqls, SQLconn)
Dim ds As New DataSet
adp.Fill(ds)
Dim dt = ds.Tables(0)
If dt.Rows.Count = 0 Then
MsgBox("no record found", MsgBoxStyle.Exclamation, "warning message")
Else
Dim dr = dt.Rows(0)
On Error Resume Next
CustomerCode.Text = dr!CustomerCode
CustomerName.Text = dr!CustomerName
Address.Text = dr!Address
Country.Text = dr!Country
City.Text = dr!City
Fax.Text = dr!Fax
Mobile.Text = dr!Mobile
Email.Text = dr!Email
Facebook.Text = dr!Facebook
Note.Text = dr!Note
'====================== Image Reincyrpation
If IsDBNull(dr!Cust_image) = False Then
Dim imgByteArray() As Byte
imgByteArray = CType(dr!Cust_image, Byte())
Dim stream As New MemoryStream(imgByteArray)
Dim bmp As New Bitmap(stream)
Cust_image.Image = Image.FromStream(stream)
stream.Close()
Label16.Visible = False
'================================================
End If
BtnEdit.Enabled = False
BtnDelete.Enabled = False
BtnSave.BackColor = Color.Red
CustomerName.Focus()
End If
End Sub
You can do like this (just to Call):
Private Sub DataGridView1_Click(sender As Object, e As EventArgs) Handles DataGridView1.Click
FrmCustomers.Show()
FrmCustomers.Hide()
FrmCustomers.show_data(CustomerCod)
FrmCustomers.Dispose()
End Sub

Copy System.Data.Datatable to Access DB Programmatically Vb.net [duplicate]

Can someone please explain why, when I click the "Commit" button, I get the error
Syntax error in INSERT INTO statement.
Here's the code.
Public Class Form3
Dim inc As Integer
Dim MaxRows As Integer
Dim con As New OleDb.OleDbConnection
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = D:\TA_Officers.mdb"
con.Open()
sql = "SELECT * from TA_OFFICER"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "TA_Officers")
con.Close()
MaxRows = ds.Tables("TA_Officers").Rows.Count
inc = -1
End Sub
Private Sub NavigateRecords()
txtFName.Text = ds.Tables("TA_Officers").Rows(inc).Item(1)
txtMInitial.Text = ds.Tables("TA_Officers").Rows(inc).Item(2)
txtLName.Text = ds.Tables("TA_Officers").Rows(inc).Item(3)
txtContact.Text = ds.Tables("TA_Officers").Rows(inc).Item(4)
txtEmail.Text = ds.Tables("TA_Officers").Rows(inc).Item(5)
txtPosition.Text = ds.Tables("TA_Officers").Rows(inc).Item(6)
txtCourse.Text = ds.Tables("TA_Officers").Rows(inc).Item(7)
txtAddress.Text = ds.Tables("TA_Officers").Rows(inc).Item(8)
End Sub
Private Sub BtnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnAdd.Click
BtnCommit.Enabled = True
BtnAdd.Enabled = False
BtnUpdate.Enabled = False
BtnDel.Enabled = False
txtPosition.Clear()
txtLName.Clear()
txtFName.Clear()
txtMInitial.Clear()
txtAddress.Clear()
txtCourse.Clear()
txtEmail.Clear()
txtContact.Clear()
End Sub
Private Sub RdMember_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RdMember.CheckedChanged
Label2.Visible = False
txtPosition.Visible = False
End Sub
Private Sub RdOfficer_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles RdOfficer.CheckedChanged
Label2.Visible = True
txtPosition.Visible = True
End Sub
Private Sub BtnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnUpdate.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("TA_Officers").Rows(inc).Item(1) = txtFName.Text
ds.Tables("TA_Officers").Rows(inc).Item(2) = txtMInitial.Text
ds.Tables("TA_Officers").Rows(inc).Item(3) = txtLName.Text
ds.Tables("TA_Officers").Rows(inc).Item(4) = txtContact.Text
ds.Tables("TA_Officers").Rows(inc).Item(5) = txtEmail.Text
ds.Tables("TA_Officers").Rows(inc).Item(6) = txtPosition.Text
ds.Tables("TA_Officers").Rows(inc).Item(7) = txtCourse.Text
ds.Tables("TA_Officers").Rows(inc).Item(8) = txtAddress.Text
da.Update(ds, "TA_Officers")
MsgBox("Data Updated!")
End Sub
Private Sub BtnDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnDel.Click
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("TA_Officers").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
NavigateRecords()
da.Update(ds, "TA_Officers")
End Sub
Private Sub BtnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnClear.Click
BtnCommit.Enabled = False
BtnAdd.Enabled = True
BtnUpdate.Enabled = True
BtnDel.Enabled = True
inc = 0
NavigateRecords()
End Sub
Private Sub BtnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnCommit.Click
If inc <> -1 Then
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("TA_Officers").NewRow()
dsNewRow.Item("Firstname") = txtFName.Text
dsNewRow.Item("Middleinitial") = txtMInitial.Text
dsNewRow.Item("Lastname") = txtLName.Text
dsNewRow.Item("Mobilenumber") = txtContact.Text
dsNewRow.Item("Emailaddress") = txtEmail.Text
dsNewRow.Item("Position") = TxtPosition.Text
dsNewRow.Item("Course") = txtCourse.Text
dsNewRow.Item("Address") = txtAddress.Text
ds.Tables("TA_Officers").Rows.Add(dsNewRow)
**da.Update(ds, "TA_Officers")**
MsgBox("New Record added to the Database")
BtnCommit.Enabled = False
BtnAdd.Enabled = True
BtnUpdate.Enabled = True
BtnDel.Enabled = True
End If
End Sub
Private Sub BtnBack_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnBack.Click
Form2.Show()
Me.Close()
End Sub
Private Sub TA_MEMBERSBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Me.Validate()
Me.TA_MEMBERSBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.TA_OfficersDataSet)
End Sub
End Class
Position is a reserved word for JET 4.0 - try to rename the column something else such as prog_position. Access will often let you create the column with a reserved name, but when you go to access it in code JET blocks it and gives you a generic Syntax error in INSERT INTO Statement error without being specific. Check out http://support.microsoft.com/kb/248738 for more information on reserved words. It is goods practice to always prefix your column names with something short so that you never run into conflicts or reserved word issues.
As Makita mentions, your problem stems from the fact that Position is a reserved word in Jet/ACE SQL. The solution is to add the following two lines of code after you create the OleDbCommandBuilder object:
cb.QuotePrefix = "["
cb.QuoteSuffix = "]"
That will tell the OleDbCommandBuilder to generate SQL statements like this
INSERT INTO [TA_OFFICER] ([FirstName], ...
...instead of
INSERT INTO TA_OFFICER (FirstName, ...
Wrapping the Position column name in those square brackets will tell the Jet database engine that it is a column name, not a keyword.
This kind of error can happen when you have different data type in the code and in the DB.
Checking the log can give some light on this.

syntax error in insert into statement in vb.net back end of access

i have created a mail tracker having a backend on ms-access 97-2003 format and all the code is fine except getting error on the said above please help on resolving the issue please refer to the screen shot and i'll provide the code below it
Imports System.Data.OleDb
Public Class Form1
Dim dbprovider As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " & Application.StartupPath & "\fsl.mdb;Persist Security Info=False;"
Dim cnn As OleDb.OleDbConnection
Dim now As Date = System.DateTime.Now()
Dim mydate As Date = now
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles quit.Click
Me.Close()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.cnn = New OleDb.OleDbConnection
rdate.Clear()
sdate.Clear()
orgi.Clear()
esub.Clear()
mno.Clear()
comap.Clear()
srno.Clear()
actby.Clear()
remark.Clear()
tim.Clear()
Label11.Visible = False
srno.Visible = False
End Sub
Private Sub save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles save.Click
cnn.ConnectionString = dbprovider
cnn.Open()
Me.sdate.Text = now
Me.tim.Text = now
Dim sqlquery As String = "Insert into fsl([rdate],[sdate],[Originator],[Circ],[Subject],[Mno],[Comp],[Type],[subtype],[actby],[srno],[Status],[Agent],[remarks],[Tim])values (#rdate,#sdate,#Originator,#Circ,#Subject,#Mno,#Comp,#Type,#subtype,#actby,#srno,#Status,#Agent,#remarks,#Tim)"
Dim cmd As OleDbCommand = New OleDbCommand(sqlquery, cnn)
cmd.Parameters.AddWithValue("#rdate", Me.rdate.Text)
cmd.Parameters.AddWithValue("#sdate", Me.sdate.Text)
cmd.Parameters.AddWithValue("#originator", Me.orgi.Text)
cmd.Parameters.AddWithValue("#circ", Me.circ.Text)
cmd.Parameters.AddWithValue("#subject", Me.esub.Text)
cmd.Parameters.AddWithValue("#mno", Me.mno.Text)
cmd.Parameters.AddWithValue("#comp", Me.comap.Text)
cmd.Parameters.AddWithValue("#type", Me.type.Text)
cmd.Parameters.AddWithValue("#subtype", Me.subty.Text)
cmd.Parameters.AddWithValue("#actby", Me.actby.Text)
cmd.Parameters.AddWithValue("#srno", Me.srno.Text)
cmd.Parameters.AddWithValue("#status", Me.status.Text)
cmd.Parameters.AddWithValue("#agent", Me.agent.Text)
cmd.Parameters.AddWithValue("#remarks", Me.remark.Text)
cmd.Parameters.AddWithValue("#tim", Me.tim.Text)
cmd.ExecuteNonQuery()
cnn.Close()
MsgBox("Record Added Successfully")
rdate.Clear()
sdate.Clear()
orgi.Clear()
esub.Clear()
mno.Clear()
comap.Clear()
srno.Clear()
actby.Clear()
remark.Clear()
tim.Clear()
End Sub
Private Sub type_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles type.LostFocus
If type.SelectedItem = "E-Bridge" Then
Label11.Visible = True
srno.Visible = True
Else
Label11.Visible = False
srno.Visible = False
End If
End Sub
Private Sub subty_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles subty.LostFocus
If subty.SelectedItem = "E-Bridge" Then
Label11.Visible = True
srno.Visible = True
ElseIf subty.SelectedItem = "Churn" Then
Label11.Visible = True
srno.Visible = True
Else
Label11.Visible = False
srno.Visible = False
End If
End Sub
End Class
You are missing the "VALUES" keyword.
Dim sqlquery As String = "Insert into fsl Values ([rdate],[sdate], ...
Take a look at this website:
http://www.w3schools.com/sql/sql_insert.asp

My connection string is not quite right

Hi can someone help me with my connection string, I can navigate around my database but I can not add data to it. Thanks in advance my code is below, if someone an shred some light on it for me that would be great. So I can navigate but I can not add data or update data. I did have this working at one point but have since screed t up. If only I had done a backup of it :(
Imports System.Data.OleDb
Public Class adminPanel
Dim con As New OleDb.OleDbConnection
Dim dbProvider As String
Dim dbSource As String
Dim ds As New DataSet
Dim sql As String
Dim da As New OleDb.OleDbDataAdapter
Dim inc As Integer
Dim MaxRows As Integer
Private Sub adminPanel(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim Builder As New OleDb.OleDbConnectionStringBuilder With
{
.ConnectionString = My.Settings.Database1
}
' Change it
Builder.DataSource = IO.Path.Combine(Application.StartupPath, "Database1.mdb")
lblName.Text = LoginForm.txtUserName.Text
If lblName.Text = LoginForm.txtUserName.Text Then
Builder.Add("Jet OLEDB:Database Password", "password")
Using con As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString}
sql = "SELECT * FROM tblContacts"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "Database1")
'MsgBox("Database is now open")
MaxRows = ds.Tables("Database1").Rows.Count
inc = -1
End Using
End If
End Sub
Private Sub NavigateRecords()
UserName.Text = CStr(ds.Tables("Database1").Rows(inc).Item(1))
UserPassword.Text = CStr(ds.Tables("Database1").Rows(inc).Item(2))
UserTimer.Text = ds.Tables("Database1").Rows(inc).Item(3).ToString
End Sub
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
UserName.Text = CStr(ds.Tables("Database1").Rows(inc).Item(1))
UserPassword.Text = CStr(ds.Tables("Database1").Rows(inc).Item(2))
UserTimer.Text = CStr(ds.Tables("Database1").Rows(inc).Item(3))
da.Update(ds, "Database1")
MsgBox("Data updated")
End Sub
Private Sub btnNext_Click(sender As Object, e As EventArgs) Handles btnNext.Click
If inc <> MaxRows - 1 Then
inc = inc + 1
NavigateRecords()
Else
MsgBox("No More Rows")
End If
End Sub
Private Sub btnPrevious_Click(sender As Object, e As EventArgs) Handles btnPrevious.Click
If inc > 0 Then
inc = inc - 1
NavigateRecords()
Else
MsgBox("First Record")
End If
End Sub
Private Sub btnLast_Click(sender As Object, e As EventArgs) Handles btnLast.Click
If inc <> MaxRows - 1 Then
inc = MaxRows - 1
NavigateRecords()
End If
End Sub
Private Sub btnFirst_Click(sender As Object, e As EventArgs) Handles btnFirst.Click
If inc <> 0 Then
inc = 0
NavigateRecords()
End If
End Sub
Private Sub adminPanel_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
MsgBox("Data cleared")
btnCommit.Enabled = False
btnAddNew.Enabled = True
btnUpdate.Enabled = True
btnDelete.Enabled = True
inc = 0
NavigateRecords()
End Sub
Private Sub btnAddNew_Click(sender As Object, e As EventArgs) Handles btnAddNew.Click
btnCommit.Enabled = True
btnUpdate.Enabled = False
btnDelete.Enabled = False
btnAddNew.Enabled = False
UserName.Clear()
UserPassword.Clear()
UserTimer.Clear()
End Sub
Private Sub btnCommit_Click(sender As Object, e As EventArgs) Handles btnCommit.Click
If inc <> -1 Then
Dim con As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
dsNewRow = ds.Tables("Database1").NewRow()
dsNewRow.Item("UserName") = UserName.Text
dsNewRow.Item("UserPassword") = UserPassword.Text
dsNewRow.Item("UserTimer") = UserTimer.Text
ds.Tables("Database1").Rows.Add(dsNewRow)
da.Update(ds, "Database1")
MsgBox("New Record added to the Database")
btnCommit.Enabled = False
btnAddNew.Enabled = True
btnUpdate.Enabled = True >
btnDelete.Enabled = True
End If
End Sub
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then
MsgBox("Operation Cancelled")
Exit Sub
End If
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("Database1").Rows(inc).Delete()
MaxRows = MaxRows - 1
inc = 0
da.Update(ds, "Database1")
NavigateRecords()
End Sub
End Class
It has nothing to do with your connection string. The code in btnUpdate_Click is not updating the database, it is updating the textboxes.
edit
btnUpdate_Click should read something like (untested code)
Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
ds.Tables("Database1").Rows(inc).Item("UserName") = UserName.Text
ds.Tables("Database1").Rows(inc).Item("Password") = UserPassword.Text
ds.Tables("Database1").Rows(inc).Item("UserTimer") = UserTimer.Text
da.Update(ds, "Database1")
MsgBox("Data updated")
End Sub

"Object reference not set" error when creating a form

I got this error when running my program. I wasn't like this before when I'm creating this program.
An error occurred creating the form.
See Exception.InnerException for details.
The error is:
Object reference not set to an instance of an object.
Here is the code in the form :
Imports System.Data
Imports System.Data.OleDb
Public Class IndexFrm
#Region "Connection"
Dim con As OleDbConnection
Dim scmd As OleDbCommand
Dim conreader As OleDbDataReader
Dim dbcon As String = Me.OpenFileDialog1.FileName & ";Jet Oledb:Database Password=*****"
#End Region
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
End
End Sub
Private Sub ToolStripButton1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
FrmSynoyms.Show()
FrmAntonyms.Hide()
FrmAnalogy.Hide()
FrmMath.Hide()
FrmAbstract.Hide()
FrmAbstract2.Hide()
FrmAbstract3.Hide()
FrmAbstract4.Hide()
StdntsFrm.Hide()
End Sub
Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
FrmAntonyms.Show()
FrmAnalogy.Hide()
FrmSynoyms.Hide()
FrmMath.Hide()
FrmAbstract.Hide()
FrmAbstract2.Hide()
FrmAbstract3.Hide()
FrmAbstract4.Hide()
StdntsFrm.Hide()
End Sub
Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
FrmAnalogy.Show()
FrmAntonyms.Hide()
FrmSynoyms.Hide()
FrmMath.Hide()
FrmAbstract.Hide()
FrmAbstract2.Hide()
FrmAbstract3.Hide()
FrmAbstract4.Hide()
StdntsFrm.Hide()
End Sub
Private Sub StudentsFormToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StudentsFormToolStripMenuItem1.Click
StdntsFrm.Show()
FrmAnalogy.Hide()
FrmAntonyms.Hide()
FrmSynoyms.Hide()
FrmMath.Hide()
FrmAbstract.Hide()
FrmAbstract2.Hide()
FrmAbstract3.Hide()
FrmAbstract4.Hide()
End Sub
Private Sub ToolStripButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton4.Click
FrmMath.Show()
FrmAnalogy.Hide()
FrmAntonyms.Hide()
FrmSynoyms.Hide()
FrmAbstract.Hide()
FrmAbstract2.Hide()
FrmAbstract3.Hide()
FrmAbstract4.Hide()
StdntsFrm.Hide()
End Sub
Private Sub ToolStripButton5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton5.Click
FrmAbstract.Show()
FrmAntonyms.Hide()
FrmAnalogy.Hide()
FrmMath.Hide()
StdntsFrm.Hide()
End Sub
'Private Sub IndexFrm_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
' If e.KeyCode = Keys.Escape Then
' Try
' If Me.WindowState = FormWindowState.Minimized Then
' Me.WindowState = FormWindowState.Minimized
' NotifyIcon1.Visible = True
' Me.Hide()
' End If
' Catch ex As Exception
' MsgBox(ex.Message)
' End Try
' End If
'End Sub
Private Sub IndexFrm_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.ToolStripButton1.Enabled = False
Me.ToolStripButton2.Enabled = False
Me.ToolStripButton3.Enabled = False
Me.ToolStripButton4.Enabled = False
Me.ToolStripButton5.Enabled = False
Me.StudentsFormToolStripMenuItem1.Enabled = False
End Sub
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Me.Label3.Text = Val(Me.Label3.Text) - 1
If Me.Label1.Text = "0" And Me.Label2.Text = "0" And Me.Label3.Text = "0" Then
Me.Timer1.Stop()
Me.Timer1.Enabled = False
MsgBox("TIME IS UP")
SynonymsSave()
AntonymsSave()
AnalogySave()
MathSave()
AbstractSave()
StdntsFrm.TxtStdntName.Clear()
StdntsFrm.TxtStdntsMI.Clear()
StdntsFrm.TxtStdntsLast.Clear()
StdntsFrm.TxtStdntAdd.Clear()
StdntsFrm.TxtStdntSchool.Clear()
StdntsFrm.TxtSchoolAdd.Clear()
StdntsFrm.TxtStdntAdv.Clear()
StdntsFrm.StdntTel.Clear()
StdntsFrm.Show()
Else
If Me.Label3.Text = "0" And Me.Label2.Text <> "0" Then
Me.Label3.Text = "59"
Me.Label2.Text = Val(Me.Label2.Text) - 1
ElseIf Me.Label2.Text = "0" And Me.Label1.Text <> "0" Then
Me.Label2.Text = "2"
Me.Label1.Text = Val(Me.Label1.Text) - 1
ElseIf Me.Label1.Text = "0" Then
Me.Label1.Text = "0"
ElseIf Me.Label2.Text = "0" And Me.Label1.Text = "0" Then
Me.Label2.Text = "0"
End If
End If
End Sub
Private Sub StudentsFormToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
FrmDBPath.Show()
End Sub
Private Sub IndexFrm_Resize(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Resize
Try
If Me.WindowState = FormWindowState.Minimized Then
Me.WindowState = FormWindowState.Minimized
NotifyIcon1.Visible = True
Me.Hide()
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub NotifyIcon1_MouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles NotifyIcon1.MouseDoubleClick
Try
Me.Show()
Me.WindowState = FormWindowState.Normal
NotifyIcon1.Visible = False
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub OpenDatabaseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenDatabaseToolStripMenuItem.Click
Dim DBpath As String
With OpenFileDialog1
.AddExtension = True
.CheckPathExists = True
.DefaultExt = ".mdb"
.DereferenceLinks = True
.Filter = "Access File (*.mdb)|*.mdb|All files|*.*"
.Multiselect = False
.RestoreDirectory = True
.ShowHelp = True
.ShowReadOnly = True
.Title = "Select file to open"
.ValidateNames = True
If .ShowDialog = Windows.Forms.DialogResult.OK Then
Try
DBpath = My.Computer.FileSystem.ReadAllText(.FileName)
Me.StudentsFormToolStripMenuItem1.Enabled = True
Catch ex As Exception
MsgBox("THE DATABASE IS ALREADY BEING USED", MsgBoxStyle.Exclamation)
End Try
End If
End With
End Sub
Sub SynonymsSave()
con = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;Data source=" & dbcon & ";Jet Oledb:Database Password=*****")
con.Open()
Dim qstring As String = "Insert into tblStdntsScores (StdntName,Synonyms,ScoreDate) values ('" & FrmSynoyms.Label59.Text & "','" & FrmSynoyms.Label58.Text & "','" & StdntsFrm.Label13.Text & "')"
scmd = New OleDbCommand(qstring, con)
scmd.ExecuteReader()
scmd.Dispose()
con.Close()
FrmSynoyms.HistorySave()
End Sub
Sub AntonymsSave()
con = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;Data source=" & dbcon & ";Jet Oledb:Database Password=*****")
con.Open()
Dim qstring As String = "Update tblStdntsScores set Antonyms='" & FrmAntonyms.Label28.Text & "' where StdntName='" & FrmAntonyms.Label29.Text & "'"
scmd = New OleDbCommand(qstring, con)
scmd.ExecuteReader()
scmd.Dispose()
con.Close()
FrmAntonyms.HistorySave()
End Sub
Sub AnalogySave()
con = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;Data source=" & dbcon & ";Jet Oledb:Database Password=*****")
con.Open()
Dim qstring As String = "Update tblStdntsScores set Analogy='" & FrmAnalogy.Label308.Text & "' where StdntName='" & FrmAnalogy.Label368.Text & "'"
scmd = New OleDbCommand(qstring, con)
scmd.ExecuteReader()
scmd.Dispose()
con.Close()
FrmAnalogy.HistorySave()
End Sub
Sub MathSave()
con = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;Data source=" & dbcon & ";Jet Oledb:Database Password=*****")
con.Open()
Dim qstring As String = "Update tblStdntsScores set Math='" & FrmMath.Label67.Text & "' where StdntName='" & FrmMath.Label68.Text & "'"
scmd = New OleDbCommand(qstring, con)
scmd.ExecuteReader()
scmd.Dispose()
con.Close()
FrmMath.HistorySave()
End Sub
Sub AbstractSave()
con = New OleDbConnection("Provider=Microsoft.jet.oledb.4.0;Data source=" & dbcon & ";Jet Oledb:Database Password=*****")
con.Open()
Dim qstring As String = "Update tblStdntsScores set Abstract='" & FrmAbstract4.Label36.Text & "' where StdntName='" & FrmAbstract.Label36.Text & "'"
scmd = New OleDbCommand(qstring, con)
scmd.ExecuteReader()
scmd.Dispose()
con.Close()
FrmAbstract4.HistorySave()
End Sub
End Class
Please help, guys.
Dim dbcon As String = Me.OpenFileDialog1.FileName & "...etc"
Here you are trying to initialize a string with a control property value. Class scoped variables (fields) are initialized before any other constructor code (ie: InitializeComponent()) is called, so here OpenFileDialog1 does not exist yet.