Hi guys,
Having a bit of a problem here, mainly as I'm trying to keep the code simple as I'm teaching it to kids.. I've linked an Access database and a VB form, and I'm editing the data in the dataset via code. Only thing is, I can't get the data to write back to the table in access...
It may be cause it's late, or I'm just stupid! But I hope someone would be able to take a look at the code, and suggest how to write back to the DB. Also posted it on YouTube (without the writing back to db bit, but if you look at the bottom of the code, you can see my pathetic effort to write it back to the data adapter in the vain hope it'll update the database!)
Here's the link to the vid: YouTube - 5.10 Adding and Deleting Data in Access through Visual Basic (1)
And here's my code
Any help would be greatly appreciated, but as I stated, I'm trying to keep it simple.. (i.e. using adapters rather than SQL)
Having a bit of a problem here, mainly as I'm trying to keep the code simple as I'm teaching it to kids.. I've linked an Access database and a VB form, and I'm editing the data in the dataset via code. Only thing is, I can't get the data to write back to the table in access...
Here's the link to the vid: YouTube - 5.10 Adding and Deleting Data in Access through Visual Basic (1)
And here's my code
VB.NET:
Imports System.Data.OleDb
Public Class Form1
Dim connectString As String = "Provider = Microsoft.JetOLEDB.4.0;" & "Data Source = HospitalDetails.mdb"
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
OleDbDataAdapter1.Fill(DsPatientsAndWards1)
txtWardName.DataBindings.Add("Text", DsPatientsAndWards1, "Patients.Ward Name")
txtWardClass.DataBindings.Add("Text", DsPatientsAndWards1, "Patients.Ward Class")
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
BindingContext(DsPatientsAndWards1, "Patients").Position = BindingContext(DsPatientsAndWards1, "Patients").Position - 1
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
BindingContext(DsPatientsAndWards1, "Patients").Position = BindingContext(DsPatientsAndWards1, "Patients").Position + 1
End Sub
Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNew.Click
BindingContext(DsPatientsAndWards1, "Patients").AddNew()
End Sub
Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
BindingContext(DsPatientsAndWards1, "Patients").RemoveAt(BindingContext(DsPatientsAndWards1, "Patients").Position)
MsgBox("Message Deleted")
End Sub
Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click
OleDbDataAdapter1.Update(DsPatientsAndWards1)
MsgBox("Record Updated")
End Sub
End Class
Any help would be greatly appreciated, but as I stated, I'm trying to keep it simple.. (i.e. using adapters rather than SQL)