- Public Class frmVisitor
- Private dbConnection As New OleDb.OleDbConnection()
- 'To add a DataAdapter
- Private daVisitor As OleDb.OleDbDataAdapter
- 'To add a CommandBuilder
- Private cmdbVisitor As OleDb.OleDbCommandBuilder Private dtVisitor As New DataTable
- Private rpVisitor As Integer = 0
- Dim intNewVisitorID As Integer
- Private Sub frmVisitor_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs Handles Me.FormClosed
- 'Closing a Connection to a Data Source ta the time when Main Form Closed
- 'this is accomplished by calling the Close() method of the connection object
- dbConnection.Dispose()
- End Sub
- Private Sub frmVisitor_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
- dbConnection.ConnectionString = _
- "Provider=Microsoft.Jet.OLEDB.4.0;Data Source =SNGPLDbase.mdb"
- daVisitor = New OleDb.OleDbDataAdapter("Select * From Visitor", dbConnection)
- 'To initialize the CommandBuilder object
- cmdbVisitor = New OleDb.OleDbCommandBuilder(daVisitor)
- daVisitor.Fill(dtVisitor) 'Because the DataTable doesn't hold a connection to the data source, you don't need to close it when you're finished.
- 'Used to display the current record in the data table
- 'Me.ShowVisitorRecord()
- End Sub
- Private Sub ShowVisitorRecord()
- If dtVisitor.Rows.Count = 0 Then 'When there is no record
- Me.cboVisitorCode.Text = ""
- Me.txtVisitorName.Text = ""
- Me.txtVehicleNo.Text = ""
- Exit Sub
- End If 'When there is record
- cboVisitorCode.Text = dtVisitor.Rows(rpVisitor)("VisitorCode")
- txtVisitorName.Text = dtVisitor.Rows(rpVisitor)("VisitorName")
- txtVehicleNo.Text = dtVisitor.Rows(rpVisitor)("VehicleNo")
- End Sub
- Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
- If dtVisitor.Rows.Count = 0 Then 'When there is no record
- Dim intNewVisitorID = 10 'Initial value of VisitorCode
- Dim drNewRow As DataRow = dtVisitor.NewRow()
- drNewRow.Item("VisitorCode") = intNewVisitorID
- Me.cboVisitorCode.Items.Add(drNewRow.Item("VisitorCode"))
- Me.cboVisitorCode.Text = drNewRow.Item("VisitorCode")
- ' cboVisitorCode.Text = dtVisitor.Rows(rpVisitor)("VisitorCode")
- dtVisitor.Rows.Add(drNewRow)
- Exit Sub
- Else
- ' If there are any rows in the data table,
- ' move to the last and show the record.
- If dtVisitor.Rows.Count > 0 Then
- rpVisitor = dtVisitor.Rows.Count - 1
- ' Me.ShowVisitorRecord()
- End If
- Dim drNewRow As DataRow = dtVisitor.NewRow()
- intNewVisitorID = drNewRow.Item("VisitorCode") + 10 'VisitorCode increase by 10
- drNewRow.Item("VisitorCode") = intNewVisitorID
- 'To Insert new VisitorCode to Combobox In case of New Entry.
- Me.cboVisitorCode.Items.Add(drNewRow.Item("VisitorCode"))
- Me.cboVisitorCode.Text = drNewRow.Item("VisitorCode")
- dtVisitor.Rows.Add(drNewRow)
- End If
- End Sub
- End Class