Hi all,
Had alot of problems with writing this code to update an access database from vb 2010 - even my computing teacher is at a dead end as what we havent tried. - Da.update(ds, "stafftable")
Basicallly i can navigate records fine - but as soon as i try to update from user inputted data VB gives me olbexceptions. Im at a loss as what to do now. - Code is below - everything declared in a seperate module
Thanks james
Public Class frmMain
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
NavigateRecords("First")
End Sub
Private Sub frmMain_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
con.Close()
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
ConnectToDatabase()
AddRecordsToForm()
intCounter = 0
End Sub
Sub AddRecordsToForm()
txtsurname.Text = ds.Tables("stafftable").Rows(0).Item(1)
txtname.Text = ds.Tables("stafftable").Rows(0).Item(2)
txtemail.Text = ds.Tables("stafftable").Rows(0).Item(3)
txtjobtitle.Text = ds.Tables("stafftable").Rows(0).Item(4)
txthomephone.Text = ds.Tables("stafftable").Rows(0).Item(5)
txtmobilephone.Text = ds.Tables("stafftable").Rows(0).Item(6)
txtaddress.Text = ds.Tables("stafftable").Rows(0).Item(7)
End Sub
Sub NavigateRecords(ByVal MyWay As String)
intMaxRows = ds.Tables("stafftable").Rows.Count
Select Case MyWay
Case "First"
If intCounter <> 0 Then
intCounter = 0
End If
Case "Last"
If intCounter <> intMaxRows Then
intCounter = intMaxRows - 1
End If
Case "Next"
If intCounter <> intMaxRows - 1 Then
intCounter = intCounter + 1
Else
MsgBox("No more records available")
End If
Case "Previous"
If intCounter > 0 Then
intCounter = intCounter - 1
Else
MsgBox("First Record")
End If
End Select
txtsurname.Text = ds.Tables("stafftable").Rows(intCounter).Item(1)
txtname.Text = ds.Tables("stafftable").Rows(intCounter).Item(2)
txtemail.Text = ds.Tables("stafftable").Rows(intCounter).Item(3)
txtjobtitle.Text = ds.Tables("stafftable").Rows(intCounter).Item(4)
txthomephone.Text = ds.Tables("stafftable").Rows(intCounter).Item(5)
txtmobilephone.Text = ds.Tables("stafftable").Rows(intCounter).Item(6)
txtaddress.Text = ds.Tables("stafftable").Rows(intCounter).Item(7)
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
NavigateRecords("Previous")
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
NavigateRecords("Next")
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
UpdateRecord()
End Sub
Sub UpdateRecord()
Dim cb As New OleDb.OleDbCommandBuilder(da) 'The command builder is needed to update the table
'Set the dynaset to the amended text in the text boxes
ds.Tables("stafftable").Rows(intCounter).Item(1) = txtsurname.Text
ds.Tables("stafftable").Rows(intCounter).Item(2) = txtname.Text
ds.Tables("stafftable").Rows(intCounter).Item(3) = txtemail.Text
ds.Tables("stafftable").Rows(intCounter).Item(4) = txtjobtitle.Text
ds.Tables("stafftable").Rows(intCounter).Item(5) = txthomephone.Text
ds.Tables("stafftable").Rows(intCounter).Item(6) = txtmobilephone.Text
ds.Tables("stafftable").Rows(intCounter).Item(7) = txtaddress.Text
'This does not update the actual database
'The dynaset is a copy of the table
'This code will update the table
da.Update(ds, "stafftable")
MsgBox("Record has been updated")
End Sub
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
ClearForm()
End Sub
Sub ClearForm()
'Clear the text boxes in the form
txtsurname.Clear()
txtname.Clear()
txtjobtitle.Clear()
txtemail.Clear()
End Sub
Sub CommitNewRecord()
Dim cb As New OleDb.OleDbCommandBuilder(da)
Dim dsNewRow As DataRow
'Add a new row to your dynaset
dsNewRow = ds.Tables("stafftable").NewRow()
dsNewRow.Item("Surname") = txtsurname.Text
dsNewRow.Item("Forename") = txtname.Text
dsNewRow.Item("email") = txtemail.Text
dsNewRow.Item("Job Title") = txtjobtitle.Text
dsNewRow.Item("Home Phone") = txthomephone.Text
dsNewRow.Item("Mobile Phone") = txtmobilephone.Text
dsNewRow.Item("Address") = txtaddress.Text
ds.Tables("stafftable").Rows.Add(dsNewRow)
da.Update(ds, "stafftable")
End Sub
Private Sub btnCommit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCommit.Click
CommitNewRecord()
'Reconnect to database and fill form
'DisconnectFromDatabase()
'ConnectToDatabase()
'AddRecordsToForm()
MsgBox("Record has been added to the database")
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
NavigateRecords("Last")
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
DeleteCurrentRecord()
'Reconnect to database and fill form
'DisconnectFromDatabase()
'ConnectToDatabase()
'AddRecordsToForm()
MsgBox("Record has been deleted from the database")
End Sub
Sub DeleteCurrentRecord()
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("stafftable").Rows(intCounter).Delete()
da.Update(ds, "stafftable")
End Sub
Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
ClearForm()
End Sub
Sub SearchRecord(ByVal strSurname As String)
'Search by surname
Dim x As Integer
Dim strCurrent As String
Dim bolFound As Boolean
bolFound = False
intMaxRows = ds.Tables("stafftable").Rows.Count
For x = 0 To intMaxRows - 1
strCurrent = ds.Tables("stafftable").Rows(x).Item(2)
If UCase(strCurrent) = UCase(strSurname) Then
txtsurname.Text = ds.Tables("stafftable").Rows(x).Item(1)
txtname.Text = ds.Tables("stafftable").Rows(x).Item(2)
txtemail.Text = ds.Tables("stafftable").Rows(x).Item(3)
txtjobtitle.Text = ds.Tables("stafftable").Rows(x).Item(4)
bolFound = True
End If
Next x
If bolFound = False Then
MsgBox("Sorry, surname not found")
End If
End Sub
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSearch.Click
Dim strSurname As String
strSurname = InputBox("Please enter Surname")
SearchRecord(strSurname)
End Sub
Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Con.Close()
Me.Close()
End Sub
End Class