Passing Values from (DataGridView1_Click) in a Form to another sub in another form - vb.net

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

Related

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.

calculation error vb.net textbox

I am working with inventory system, Transaction form I had mainly 2 text box, first 1 is avaqty its from database available qty, other textbox is avqty from user adding qty.
When user add qty avaqty textbox automatically change to
(avaqty) -(avqty) but as an example 10-10 result come in -1
My code is:
Public Class transaction
Dim objcon As New connectionclass
Dim qty As String
Public Function getqty() As String
If itemid.Text = "" Then
Else
Try
objcon.myconnection.Open()
Dim getqty1 As New OleDbCommand("select balance from avaqty where item_id=" & itemid.Text & "", objcon.myconnection)
Dim reader As OleDbDataReader = getqty1.ExecuteReader
While reader.Read
qty = reader("balance")
End While
objcon.myconnection.Close()
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
Return qty
End Function
Private Sub itemid_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles itemid.KeyPress
If Asc(e.KeyChar) = 13 Then
objcon.myconnection.Open()
Try
Dim getdata As New OleDbCommand("select*from itemtbl where item_id=" & itemid.Text & "", objcon.myconnection)
Dim reader As OleDbDataReader = getdata.ExecuteReader
While reader.Read
itemdesc.Text = reader("item_des").ToString
yom.Text = reader("unit_of_mesure").ToString
cost.Text = reader("item_cost").ToString
End While
Catch ex As Exception
MsgBox(ex.Message)
End Try
objcon.myconnection.Close()
End If
If getqty() = 0 Then
avaqty.Text = 0
Else
avaqty.Text = getqty()
End If
End Sub
Private Sub initem_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles initem.CheckedChanged
dataview.DataSource = GETDATA()
End Sub
Private Sub outitem_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles outitem.CheckedChanged
dataview.DataSource = GETDATA2()
End Sub
Private Sub avqty_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles avqty.TextChanged
Try
If avqty.Text = "" Then
avaqty.Text = getqty()
Else
avaqty.Text = CDbl(avaqty.Text - avqty.Text)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub avaqty_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles avaqty.TextChanged
If avaqty.Text < 0 Then
MsgBox("not inough qty")
avqty.Clear()
End If
End Sub
End Class

Visual basic 2010, database – how to fill specific field

I'm creating database connection in Visual Basic. When user click on "borrow book" it transfer chosen data into table used for lent books. It all works alright, but I need last thing. In my database in lent table I have attribute "End date of lent" and I want fill it everytime when user borrow a book with date today + 1 month (for instance, if now is 19/04/2016, end date will be 19/05/2016). I created method "BorrowTime" for this, but I don't know what I should type into and how can I get value of inserted row into "row". And sorry for horrible look of code and form...
Public Class frm_UserView
Dim BooksSource As New BindingSource
Dim BorrowSource As New BindingSource
Dim BooksView As New DataView
Dim BorrowView As New DataView
Dim ds As New DataSet
Dim Borrow As Byte
Private Sub frm_UserView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
tmr_Timer.Start()
Me.LoanerBooksTableAdapter.Fill(Me.GMITLibraryDataSet1.LoanerBooks)
Me.BooksTableAdapter.Fill(Me.GMITLibraryDataSet.Books)
BooksSource = dgd_UserBookView.DataSource
BooksView = CType(BooksSource.List, DataView)
ds = BooksView.DataViewManager.DataSet.Clone
BorrowSource.DataSource = ds
BorrowSource.DataMember = "Books"
BorrowView = CType(BorrowSource.List, DataView)
dgd_User_Borrow_View.DataSource = BorrowSource
End Sub
Private Sub btn_Borrow_Click(sender As System.Object, e As System.EventArgs) Handles btn_BorrowBook.Click
Borrow = 1
MoveBooks(dgd_UserBookView, BorrowView, Borrow)
dgd_User_Borrow_View.Sort(dgd_User_Borrow_View.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BorrowSource.MoveLast()
BorrowSource.MoveLast()
End Sub
Private Sub MoveBooks(ByRef source As DataGridView, ByRef target As DataView, ByRef borrow As Byte)
For i = 0 To source.SelectedRows.Count - 1
Dim numberOfRow As Integer
Dim row As DataRowView
row = target.AddNew()
Dim col As Int16 = 0
For Each cell As DataGridViewCell In source.SelectedRows(i).Cells
row.Item(col) = cell.Value
If borrow = 1 Then
numberOfRow = 0
BorrowTime(numberOfRow, dgd_User_Borrow_View)
End If
col = col + 1
Next
Next
Dim count As Int16 = source.SelectedRows.Count
For i = 0 To count - 1
source.Rows.RemoveAt(source.SelectedRows(0).Index)
Next
End Sub
Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
'Code for adding date
End Sub
Private Sub btn_ReturnBook_Click(sender As System.Object, e As System.EventArgs) Handles btn_ReturnBook.Click
Borrow = 0
MoveBooks(dgd_User_Borrow_View, BooksView, Borrow)
dgd_UserBookView.Sort(dgd_UserBookView.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BooksSource.MoveLast()
BooksSource.MoveLast()
End Sub
Private Sub btn_UserLogOut_Click(sender As System.Object, e As System.EventArgs) Handles btn_UserLogOut.Click
frm_Login.Show()
Me.Hide()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles tmr_Timer.Tick
Dim countBooks As Integer
Dim countLent As Integer
countBooks = BooksSource.Count
countLent = BorrowSource.Count
lbl_BookCounter.Text = "There are " + countBooks.ToString + " books"
lbl_BorrowCounter.Text = "You have lent " + countLent.ToString + " books"
End Sub
End Class
I need to put code here:
Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
'Code for adding date
End Sub
My form
Public Class frm_UserView
Dim BooksSource As New BindingSource
Dim BorrowSource As New BindingSource
Dim BooksView As New DataView
Dim BorrowView As New DataView
Dim ds As New DataSet
Dim Borrow As Byte
Private Sub frm_UserView_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
tmr_Timer.Start()
Me.LoanerBooksTableAdapter.Fill(Me.GMITLibraryDataSet1.LoanerBooks)
Me.BooksTableAdapter.Fill(Me.GMITLibraryDataSet.Books)
BooksSource = dgd_UserBookView.DataSource
BooksView = CType(BooksSource.List, DataView)
ds = BooksView.DataViewManager.DataSet.Clone
BorrowSource.DataSource = ds
BorrowSource.DataMember = "Books"
BorrowView = CType(BorrowSource.List, DataView)
dgd_User_Borrow_View.DataSource = BorrowSource
End Sub
Private Sub btn_Borrow_Click(sender As System.Object, e As System.EventArgs) Handles btn_BorrowBook.Click
Borrow = 1
MoveBooks(dgd_UserBookView, BorrowView, Borrow)
dgd_User_Borrow_View.Sort(dgd_User_Borrow_View.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BorrowSource.MoveLast()
BorrowSource.MoveLast()
End Sub
Private Sub MoveBooks(ByRef source As DataGridView, ByRef target As DataView, ByRef borrow As Byte)
For i = 0 To source.SelectedRows.Count - 1
Dim numberOfRow As Integer
Dim row As DataRowView
row = target.AddNew()
Dim col As Int16 = 0
For Each cell As DataGridViewCell In source.SelectedRows(i).Cells
row.Item(col) = cell.Value
If borrow = 1 Then
numberOfRow = 0
BorrowTime(numberOfRow, dgd_User_Borrow_View)
End If
col = col + 1
Next
Next
Dim count As Int16 = source.SelectedRows.Count
For i = 0 To count - 1
source.Rows.RemoveAt(source.SelectedRows(0).Index)
Next
End Sub
Private Sub BorrowTime(ByRef row As Integer, ByRef dgd_Table As DataGridView)
'Code for adding date
End Sub
Private Sub btn_ReturnBook_Click(sender As System.Object, e As System.EventArgs) Handles btn_ReturnBook.Click
Borrow = 0
MoveBooks(dgd_User_Borrow_View, BooksView, Borrow)
dgd_UserBookView.Sort(dgd_UserBookView.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
BooksSource.MoveLast()
BooksSource.MoveLast()
End Sub
Private Sub btn_UserLogOut_Click(sender As System.Object, e As System.EventArgs) Handles btn_UserLogOut.Click
frm_Login.Show()
Me.Hide()
End Sub
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles tmr_Timer.Tick
Dim countBooks As Integer
Dim countLent As Integer
countBooks = BooksSource.Count
countLent = BorrowSource.Count
lbl_BookCounter.Text = "There are " + countBooks.ToString + " books"
lbl_BorrowCounter.Text = "You have lent " + countLent.ToString + " books"
End Sub
End Class

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

OleDbCommandBuilder creates SQL statements that result in "syntax error"

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.