Public Class Form1
Dim daCustomers As New OleDb.OleDbDataAdapter
Dim dsCustomers As New DataSet
Dim MyConnection As OleDb.OleDbConnection
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim cmdCustomers As New OleDb.OleDbCommand
Dim DatabasePath As String = System.IO.Directory.GetCurrentDirectory & "\Database.mdb"
Try
MyConnection = New System.Data.OleDb.OleDbConnection("Provider=microsoft.jet.oledb.4.0;data source ='" & DatabasePath & "'")
MyConnection.Open()
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Information, "error")
End
End Try
Try
daCustomers = New OleDb.OleDbDataAdapter("Select * from Contacts", MyConnection)
' automatically generate the Update, Insert, and Delete commands
Dim cb As OleDb.OleDbCommandBuilder = New OleDb.OleDbCommandBuilder(daCustomers)
daCustomers.Fill(dsCustomers, "Customers")
txtCompany.DataBindings.Add("Text", dsCustomers.Tables("Customers"), "Company")
Dim MiEnlazador As New BindingSource
MiEnlazador.DataSource = dsCustomers.Tables("Customers")
DataGridView1.DataSource = dsCustomers
DataGridView1.DataMember = ("Customers")
UpdatePosition()
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "frmCustomers_Load")
End Try
End Sub
Private Sub UpdatePosition()
Me.lblPosition.Text = ((Me.BindingContext(dsCustomers.Tables("Customers")).Position + 1).ToString + " of " + Me.BindingContext(dsCustomers.Tables("Customers")).Count.ToString)
End Sub
Private Sub DataGridView1_SelectionChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.SelectionChanged
UpdatePosition()
End Sub
Private Sub btnFirst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFirst.Click
Try
Me.BindingContext(dsCustomers.Tables("Customers")).Position = 0
UpdatePosition()
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "btnNext_Click")
End Try
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
Try
Me.BindingContext(dsCustomers.Tables("Customers")).Position -= 1
UpdatePosition()
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "btnNext_Click")
End Try
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Try
Me.BindingContext(dsCustomers.Tables("Customers")).Position += 1
UpdatePosition()
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "btnNext_Click")
End Try
End Sub
Private Sub btnLast_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLast.Click
Try
Me.BindingContext(dsCustomers.Tables("Customers")).Position = Me.BindingContext(dsCustomers.Tables("Customers")).Count - 1
UpdatePosition()
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "btnNext_Click")
End Try
End Sub
Private Sub btnAddRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddRecord.Click
Try
Me.BindingContext(dsCustomers.Tables("Customers")).EndCurrentEdit()
Me.BindingContext(dsCustomers.Tables("Customers")).AddNew()
UpdatePosition()
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "btnNext_Click")
End Try
End Sub
End Class