Student's Registration Form with AutoIncrement/AutoNumber in a TextBox

In this tutorial I will create a Student's Registration Form. I will show you how to use a Module in setting up your codes and minimize bunch of codes in your Student's Registration Form. And at the same time, it AutoIncrement a TextBox and generates the AutoNumber of the Student's Id.
First create a Table in the MySQL Database named “studentdb”.
  1. CREATE TABLE IF NOT EXISTS `student` (
  2.   `s_id` int(11) NOT NULL,
  3.   `s_name` varchar(40) NOT NULL,
  4.   `lastname` varchar(40) NOT NULL,
  5.   `middlename` varchar(40) NOT NULL,
  6.   `s_address` varchar(30) NOT NULL,
  7.   `s_age` int(11) NOT NULL,
  8.   `s_bday` varchar(30) NOT NULL,
  9.   `s_bplace` varchar(30) NOT NULL,
  10.   `s_gender` varchar(30) NOT NULL,
  11.   `s_status` varchar(30) NOT NULL,
  12.   `s_contact` varchar(30) NOT NULL,
  13.   `s_guardian` varchar(30) NOT NULL,
  14.   `s_guardian_add` varchar(40) NOT NULL,
  15.   `s_guardian_contact` varchar(30) NOT NULL,
  16.   `sy` varchar(20) NOT NULL,
  17.   `yr` int(5) NOT NULL,
  18.   PRIMARY KEY (`s_id`)
Then, open Visual Basic 2008 , create a Project and a Student's Registration Form. Resgistration Form Description: I set the properties of theTextBox(txtid) into disabled so that you cannot set or change the id and the program will set it automatically. Create a Module named “studReg” .Set up your connection in MySQL Database and Visual Basic 2008 then declare the variables and all the classes that you needed.
  1. 'add reference
  2. Imports MySql.Data.MySqlClient
  3. Module studReg
  4.     'create a connection in public function
  5.     Public Function mysqldb() As MySqlConnection
  6.         'to return new connection
  7.         Return New MySqlConnection("server=localhost;user id=root;database=studentdb")
  8.     End Function
  9.     'to pass on the mysql connection in the con  
  10.     Public con As MySqlConnection = mysqldb()
  11.  
  12.     'the set of commands in MySQL
  13.     Public cmd As New MySqlCommand
  14.     'brigde between the database and datatable in retrieving and saving the data
  15.     Public da As New MySqlDataAdapter
  16.     'represents a specific table in the database
  17.     Public dt As New DataTable
  18.  
  19.     'declare variable result as interger
  20.     Public result As Integer
  21.  
  22. End Class
Create your Sub Procedure for saving Student's record.
  1. 'create a sub procedure with parameters type of string
  2.     Public Sub create(ByVal sql As String)
  3.         Try
  4.             'open the connection
  5.             con.Open()
  6.             'set up your commands
  7.             'it holds the data to be executed
  8.             With cmd
  9.                 'pass on the value of con to the MySQL command which is Connection
  10.                 .Connection = con
  11.                 'role of this is to return text presented by a command object
  12.                 .CommandText = sql
  13.  
  14.                 'executes the data to save in the database
  15.                 result = cmd.ExecuteNonQuery
  16.  
  17.                 'if the data executes less than 0 it will not be saved
  18.                 'but if the data executes greater than 0 it will be saved
  19.                 If result = 0 Then
  20.                     MsgBox("Error in Registration!", MsgBoxStyle.Exclamation)
  21.                 Else
  22.                     MsgBox("You have successfully registered.")
  23.                 End If
  24.             End With
  25.         Catch ex As Exception
  26.             MsgBox(ex.Message)
  27.         End Try
  28.         'close the connection
  29.         con.Close()
  30.     End Sub
Then, create a Sub Procedure for retrieving the total number of rows in the table of your Database, which will appear in the TextBox. I know you are confused of how this procedure AutoIncrement/AutoNumber the value in the TextBox. The technique is, set this procedure (you can see it below) in the first load and put this after saving your data. You will know and see what I’m talking about when you came up in the next step.
  1.     Public Sub autoNumber(ByVal sql As String, ByRef txt As Object)
  2.         Try
  3.             'open the connection
  4.             con.Open()
  5.             'to set a new specific table in the database
  6.             dt = New DataTable
  7.  
  8.             'set up your commands
  9.             'it holds the data to be executed
  10.             With cmd
  11.                 'pass on the value of con to the MySQL command which is Connection
  12.                 .Connection = con
  13.                 'role of this is to return text presented by a command object
  14.                 .CommandText = sql
  15.             End With
  16.  
  17.             'for retrieval of data and filling the table
  18.             da = New MySqlDataAdapter(sql, con)
  19.             da.Fill(dt)
  20.  
  21.             'to put the maxrow of a table in the textbox
  22.             txt.text = "000" & dt.Rows.Count
  23.  
  24.         Catch ex As Exception
  25.             MsgBox(ex.Message)
  26.         End Try
  27.         da.Dispose()
  28.         'close the connection
  29.         con.Close()
  30.     End Sub
Go back to the Design Views , double click the Form and call your Sub name for your AutoIncrement/AutoNumber.
  1.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  2.         ''set your autonumber on the first load
  3.         autoNumber("SELECT * FROM student", txtid)
  4.     End Sub
Go back to the Design Views again, double click the Save Button and call the Sub name for saving the data. Then, call again the Sub name of your AutoIncrement/AutoNumber procedure to increment the value in the TextBox.
  1.  Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
  2.         'declaring variable sql as string
  3.         Dim sql As String
  4.         'declaring variable radio as string
  5.         Dim radio As String
  6.  
  7.         'conditioning the radiobutton to set the male and female
  8.         ' if the radiobutton is checked it is equals to Female and if not it is equals to male
  9.         If rdoFemaleMale.Checked = True Then
  10.             radio = "Female"
  11.         Else
  12.             radio = "Male"
  13.         End If
  14.  
  15.         'put the insert query to variable sql as string
  16.         sql = "INSERT INTO student (`s_id`, `s_fname`, `lastname`, `middlename`," _
  17.                         & "`s_address`, `s_age`, `s_bday`, `s_bplace`, `s_gender`," _
  18.                         & "`s_status`, `s_guardian`, `s_guardian_relation`,`s_guardian_add`," _
  19.                         & "`s_guardian_contact`,`sy`, `yr`) VALUES ('" & txtid.Text & "','" _
  20.                         & txtFname.Text & "','" & txtLname.Text & "','" & txtMname.Text & "','" _
  21.                         & rchAddress.Text & "','" & txtAge.Text & "','" & dtpDbirth.Text & "','" _
  22.                         & rchPbirth.Text & "','" & radio & "','" & cboStatus.Text & "','" _
  23.                         & txtGuardian.Text & "','" & txtRelation.Text & "','" & rchGaddress.Text & "','" _
  24.                         & txtContact.Text & "' ,'" & cbosy.Text & "','" & cboYear.Text & "')"
  25.  
  26.         'call your public sub name and put the sql in the parameters list
  27.         create(sql)
  28.  
  29.         'for clearing the textbox
  30.         cleartext(Me)
  31.  
  32.         'call your autonumber to set a new value
  33.         autoNumber("SELECT * FROM student", txtid)
  34.  
  35.     End Sub
The complete Source Code is included. Download and run it on your computer.

Add new comment