Navigation of a Student Registration Form

In my previous tutorial I created a Student's Registration Form in Visual Basic 2008 and MySQL Database. Now, in this tutorial I will teach you how to navigate and check if how many records saved in the MySQL Database. Lets’ begin: 1. Open the Visual Basic 2008. 2. Open the File of a Student's Registration Form. 3. Add the Five Buttons and 3 Labels in a Form. And it will look like this. Registration Form 4. Add a Module named "navigation". Then, declares all the variables that you needed.
  1. Imports MySql.Data.MySqlClient
  2. Module navigation
  3. Public maxrows As Integer 'declare a variable maxrows as integer
  4. Public inc As Integer = 0 'declare a variable inc as integer and store it the value which is one.
  5. End Module
5. After that, create a Sub procedure for retrieving all the records in MySQL Database.
  1. 'create a sub procedure in retieving all the records in the database for the navigation.
  2. Public Sub selectRecords()
  3. With cmd
  4. .Connection = con
  5. .CommandText = "SELECT * FROM student"
  6. End With
  7. da = New MySqlDataAdapter("SELECT * FROM student", con)
  8. dt = New DataTable
  9. da.Fill(dt)
  10.  
  11. 'stores the total number of rows in the table of the database
  12. maxrows = dt.Rows.Count
  13. End Sub
6. And then, create again a Sub procedure to navigate the records in MySQL Database.
  1. 'create a sub procedure for the navigation of records with a parameters type is Form1
  2. Public Sub navigateRecords(ByVal frm As Form1)
  3. With frm 'frm represent as a Form1
  4.  
  5. 'Variable inc represents the value of rows in the table.
  6. 'whatever the value of the inc, this will be the row of records that will retrieve in the database.
  7. .txtid.Text = dt.Rows(inc).Item("s_id")
  8. .txtFname.Text = dt.Rows(inc).Item("s_fname")
  9. .txtLname.Text = dt.Rows(inc).Item("lastname")
  10. .txtMname.Text = dt.Rows(inc).Item("middlename")
  11. .rchAddress.Text = dt.Rows(inc).Item("s_address")
  12. .dtpDbirth.Text = dt.Rows(inc).Item("s_bday")
  13. .rchPbirth.Text = dt.Rows(inc).Item("s_bplace")
  14. .txtAge.Text = dt.Rows(inc).Item("s_age")
  15.  
  16. If dt.Rows(inc).Item("s_gender") = "Female" Then 'check if the gender is male or female.
  17. .rdoFemaleMale.Checked = True
  18. Else
  19. .rdioMale.Checked = True
  20.  
  21. End If
  22. .cboStatus.Text = dt.Rows(inc).Item("s_status")
  23. .cboYear.Text = dt.Rows(inc).Item("yr")
  24. .cbosy.Text = dt.Rows(inc).Item("sy")
  25. .txtGuardian.Text = dt.Rows(inc).Item("s_guardian")
  26. .txtRelation.Text = dt.Rows(inc).Item("s_guardian_relation")
  27. .rchGaddress.Text = dt.Rows(inc).Item("s_guardian_add")
  28. .txtContact.Text = dt.Rows(inc).Item("s_guardian_contact")
  29. End With
  30. End Sub
7. Now, double click the Form and do following codes under the Form_Load.
  1. selectRecords() 'A SubName in rettrieving records in the database.
  2. lbl_Increment.Text = inc 'Set the variable inc is equal to the Label.
  3. lbl_MaxRecord.Text = maxrows - 1 'Set the total numbers of rows in the table - 1 is equal to the Label.
8. Do the following codes for the First, Last, Next, Previous and New Records : This is for the First Records.
  1. Private Sub btn_First_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_First.Click
  2. Try
  3. selectRecords() 'A SubName in retrieving all the records in the database
  4. inc = 0 'store the value in the variable inc which is 0 for the first row.
  5. lbl_Increment.Text = inc 'Pass the value of a variable inc into the Label.
  6. navigateRecords(Me) 'A SubName for setting up the navigation of the records
  7. Catch ex As Exception
  8. MsgBox(ex.Message)
  9. End Try
  10. End Sub
This is for the Last Records.
  1. Private Sub btn_Last_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Last.Click
  2. Try
  3. selectRecords() 'A SubName in retrieving all the records in the database
  4. If inc <> maxrows - 1 Then 'Check if the variable inc is not equal to the total number of rows in the table.
  5. inc = maxrows - 1 'The total number of rows in the table minus one is equal to the variable inc
  6. navigateRecords(Me) 'A SubName for setting up the navigation of the records
  7. lbl_Increment.Text = maxrows - 1 'The total number of rows in the table minus one is equal to the Label.
  8. End If
  9. Catch ex As Exception
  10. MsgBox(ex.Message)
  11. End Try
  12. End Sub
This is for the Next Records.
  1. Private Sub btn_Next_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Next.Click
  2. selectRecords() 'A SubName in retrieving all the records in the database
  3. If inc <> maxrows - 1 Then 'Check if the variable inc is not equal to the total number of rows in the table.
  4. inc = inc + 1 'The variable inc + 1 is equal to variable inc
  5. 'Every click the button the rows will increment by 1
  6.  
  7. lbl_Increment.Text = inc ''Set your variable inc is equal to the Label
  8. navigateRecords(Me) 'A SubName for setting up the navigation of the records
  9. Else
  10. If maxrows + 1 Then 'check if the total numbers of rows is higher of 1.
  11. MsgBox("No more rows", MsgBoxStyle.Information) 'The message box will appear and has a message of "No more rows"
  12. End If
  13. End If
  14. End Sub
This is for the Previous Records.
  1. Private Sub btn_Previous_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Previous.Click
  2. selectRecords() 'A SubName in retrieving all the records in the database
  3. If inc > 0 Then 'Check if the variable inc is greater than 0
  4. inc = inc - 1 'The variable inc - 1 is equal to variable inc
  5. 'Every click the button the rows will decrease by 1
  6.  
  7. lbl_Increment.Text = inc 'Set your variable inc is equal to the Label
  8. navigateRecords(Me) 'A SubName for setting up the navigation of the records
  9. Else
  10. If inc - 1 Then 'check if variable inc is decrease of 1.
  11. MsgBox("First records", MsgBoxStyle.Information) 'The message box will appear and has a message of "No more rows"
  12. End If
  13. End If
  14. End Sub
And this is for the New Records.
  1. Private Sub btn_New_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_New.Click
  2. Try
  3. cleartext(Me) 'call for clearing the TextBox
  4. Call Form1_Load(sender, e) 'call your first load to set again the AutoNumber.
  5.  
  6. Catch ex As Exception
  7. MsgBox(ex.Message)
  8. End Try
  9. End Sub
Download the complete Source Code and run it on your computer.

Add new comment