EDN Admin
Well-known member
<div style="color:Black;background-color:White; <pre>
ublic <span style="color:Blue; Class Form1
<span style="color:Blue; Dim con <span style="color:Blue; As <span style="color:Blue; New OleDb.OleDbConnection
<span style="color:Blue; Dim currentrec, maxrows <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Dim ds <span style="color:Blue; As <span style="color:Blue; New DataSet
<span style="color:Blue; Dim da <span style="color:Blue; As OleDb.OleDbDataAdapter
<span style="color:Blue; Dim sql <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Private <span style="color:Blue; Sub OpenToolStripMenuItem_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles OpenToolStripMenuItem.Click
<span style="color:Green; Use Try..Catch to prevent runtime error if the file do not exist.
<span style="color:Blue; Try
con.ConnectionString = <span style="color:#A31515; "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=C:UsersuserDesktopPractice1Practice1.accdb"
con.Open()
con.Close()
MessageBox.Show(<span style="color:#A31515; "File Name: Practice1.accdb" & vbNewLine & <span style="color:#A31515; "Format: Microsoft Access 2007" & vbNewLine & <span style="color:#A31515; "Status: Loaded Successfully")
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Green; Error message to be displayed
MessageBox.Show(<span style="color:#A31515; "The file is not available. Please check the path again." & vbNewLine & <span style="color:#A31515; "Set file as:C:UsersuserDesktopPractice1.accdb")
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub CloseToolStripMenuItem_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles CloseToolStripMenuItem.Click
sql = <span style="color:#A31515; "SELECT * FROM Practice1"
da = <span style="color:Blue; New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, <span style="color:#A31515; "BookList")
con.Close()
<span style="color:Green; Initialise the total number of rows from the database table.
maxrows = ds.Tables(<span style="color:#A31515; "BookList").Rows.<span style="color:Blue; Count
txtRecNo.Text = currentrec + 1
txtBookCode.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(0).Item(0)
txtBookTitle.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(0).Item(1)
txtAuthor.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(0).Item(2)
txtYearReleased.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(0).Item(3)
txtPrice.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(0).Item(4)
currentrec = 0
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub ExitToolStripMenuItem_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles ExitToolStripMenuItem.Click
<span style="color:Green; This option allows the current database to be duplicated to a new file
FileCopy(<span style="color:#A31515; "C:UsersuserDesktopPractice1.accdb", <span style="color:#A31515; "C:UsersuserDesktopPractice1.accdb")
MessageBox.Show(<span style="color:#A31515; "Backup created successfully")
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub btnPrevious_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles btnPrevious.Click
<span style="color:Green; This button allows user to view the previous record
currentrec = currentrec - 1
<span style="color:Green; Determine if the record is already the first record in the dataset.
<span style="color:Green; If yes, prevent user from going backward
<span style="color:Blue; If currentrec < 0 <span style="color:Blue; Then
MessageBox.Show(<span style="color:#A31515; "No previous records")
currentrec = 0
<span style="color:Blue; Else
txtRecNo.Text = Val(txtRecNo.Text) - 1
txtBookCode.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(0)
txtBookTitle.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(1)
txtAuthor.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(2)
txtYearReleased.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(3)
txtPrice.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(4)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub btnNext_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles btnNext.Click
<span style="color:Green; This button allows user to view the next record
currentrec = currentrec + 1
<span style="color:Green; Determine if the record is already the last record in the dataset.
<span style="color:Green; If yes, prevent user from going forward
<span style="color:Blue; If currentrec > maxrows - 1 <span style="color:Blue; Then
MessageBox.Show(<span style="color:#A31515; "End of file reached")
currentrec = maxrows - 1
<span style="color:Blue; Else
txtRecNo.Text = Val(txtRecNo.Text) + 1
txtBookCode.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(0)
txtBookTitle.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(1)
txtAuthor.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(2)
txtYearReleased.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(3)
txtPrice.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(4)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub txtRecNo_GotFocus(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles txtRecNo.GotFocus
<span style="color:Green; Ensure user do not put control into the record number field
btnNext.Focus()
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub ExitToolStripMenuItem1_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles ExitToolStripMenuItem1.Click
<span style="color:Green; This menu option allows the form to be closed
Close()
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class
[/code]
<br/>
View the full article
ublic <span style="color:Blue; Class Form1
<span style="color:Blue; Dim con <span style="color:Blue; As <span style="color:Blue; New OleDb.OleDbConnection
<span style="color:Blue; Dim currentrec, maxrows <span style="color:Blue; As <span style="color:Blue; Integer
<span style="color:Blue; Dim ds <span style="color:Blue; As <span style="color:Blue; New DataSet
<span style="color:Blue; Dim da <span style="color:Blue; As OleDb.OleDbDataAdapter
<span style="color:Blue; Dim sql <span style="color:Blue; As <span style="color:Blue; String
<span style="color:Blue; Private <span style="color:Blue; Sub OpenToolStripMenuItem_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles OpenToolStripMenuItem.Click
<span style="color:Green; Use Try..Catch to prevent runtime error if the file do not exist.
<span style="color:Blue; Try
con.ConnectionString = <span style="color:#A31515; "PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source=C:UsersuserDesktopPractice1Practice1.accdb"
con.Open()
con.Close()
MessageBox.Show(<span style="color:#A31515; "File Name: Practice1.accdb" & vbNewLine & <span style="color:#A31515; "Format: Microsoft Access 2007" & vbNewLine & <span style="color:#A31515; "Status: Loaded Successfully")
<span style="color:Blue; Catch ex <span style="color:Blue; As Exception
<span style="color:Green; Error message to be displayed
MessageBox.Show(<span style="color:#A31515; "The file is not available. Please check the path again." & vbNewLine & <span style="color:#A31515; "Set file as:C:UsersuserDesktopPractice1.accdb")
<span style="color:Blue; End <span style="color:Blue; Try
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub CloseToolStripMenuItem_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles CloseToolStripMenuItem.Click
sql = <span style="color:#A31515; "SELECT * FROM Practice1"
da = <span style="color:Blue; New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, <span style="color:#A31515; "BookList")
con.Close()
<span style="color:Green; Initialise the total number of rows from the database table.
maxrows = ds.Tables(<span style="color:#A31515; "BookList").Rows.<span style="color:Blue; Count
txtRecNo.Text = currentrec + 1
txtBookCode.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(0).Item(0)
txtBookTitle.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(0).Item(1)
txtAuthor.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(0).Item(2)
txtYearReleased.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(0).Item(3)
txtPrice.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(0).Item(4)
currentrec = 0
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub ExitToolStripMenuItem_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles ExitToolStripMenuItem.Click
<span style="color:Green; This option allows the current database to be duplicated to a new file
FileCopy(<span style="color:#A31515; "C:UsersuserDesktopPractice1.accdb", <span style="color:#A31515; "C:UsersuserDesktopPractice1.accdb")
MessageBox.Show(<span style="color:#A31515; "Backup created successfully")
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub btnPrevious_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles btnPrevious.Click
<span style="color:Green; This button allows user to view the previous record
currentrec = currentrec - 1
<span style="color:Green; Determine if the record is already the first record in the dataset.
<span style="color:Green; If yes, prevent user from going backward
<span style="color:Blue; If currentrec < 0 <span style="color:Blue; Then
MessageBox.Show(<span style="color:#A31515; "No previous records")
currentrec = 0
<span style="color:Blue; Else
txtRecNo.Text = Val(txtRecNo.Text) - 1
txtBookCode.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(0)
txtBookTitle.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(1)
txtAuthor.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(2)
txtYearReleased.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(3)
txtPrice.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(4)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub btnNext_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles btnNext.Click
<span style="color:Green; This button allows user to view the next record
currentrec = currentrec + 1
<span style="color:Green; Determine if the record is already the last record in the dataset.
<span style="color:Green; If yes, prevent user from going forward
<span style="color:Blue; If currentrec > maxrows - 1 <span style="color:Blue; Then
MessageBox.Show(<span style="color:#A31515; "End of file reached")
currentrec = maxrows - 1
<span style="color:Blue; Else
txtRecNo.Text = Val(txtRecNo.Text) + 1
txtBookCode.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(0)
txtBookTitle.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(1)
txtAuthor.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(2)
txtYearReleased.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(3)
txtPrice.Text = ds.Tables(<span style="color:#A31515; "BookList").Rows(currentrec).Item(4)
<span style="color:Blue; End <span style="color:Blue; If
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub txtRecNo_GotFocus(<span style="color:Blue; ByVal sender <span style="color:Blue; As <span style="color:Blue; Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles txtRecNo.GotFocus
<span style="color:Green; Ensure user do not put control into the record number field
btnNext.Focus()
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; Private <span style="color:Blue; Sub ExitToolStripMenuItem1_Click(<span style="color:Blue; ByVal sender <span style="color:Blue; As System.Object, <span style="color:Blue; ByVal e <span style="color:Blue; As System.EventArgs) <span style="color:Blue; Handles ExitToolStripMenuItem1.Click
<span style="color:Green; This menu option allows the form to be closed
Close()
<span style="color:Blue; End <span style="color:Blue; Sub
<span style="color:Blue; End <span style="color:Blue; Class
[/code]
<br/>
View the full article