• We just launched and are currently in beta. Join us as we build and grow the community.

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

Weev

Cyber Threat Analyst
W Rep
0
0
0
Rep
0
W Vouches
0
0
0
Vouches
0
Posts
96
Likes
158
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 300 XP
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

    [url=http://dev.mysql.com/doc/refman/%35%2E%31/en/control-flow-functions.html]IF

    NOT

    EXISTS

    [/url] `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`

    )
  19. )

    ENGINE

    =

    InnoDB

    DEFAULT

    CHARSET

    =

    latin1;

Then, open Visual Basic 2008 , create a Project and a Student's Registration Form.

clearform.jpg


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. 'the set of commands in MySQL
  12. Public

    cmd As

    New

    MySqlCommand
  13. 'brigde between the database and datatable in retrieving and saving the data
  14. Public

    da As

    New

    MySqlDataAdapter
  15. 'represents a specific table in the database
  16. Public

    dt As

    New

    DataTable

  17. 'declare variable result as interger
  18. Public

    result As

    Integer

  19. 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. 'executes the data to save in the database
  14. result =

    cmd.

    ExecuteNonQuery

  15. 'if the data executes less than 0 it will not be saved
  16. 'but if the data executes greater than 0 it will be saved
  17. If

    result =

    0

    Then
  18. MsgBox

    (

    "Error in Registration!"

    , MsgBoxStyle.

    Exclamation

    )
  19. Else
  20. MsgBox

    (

    "You have successfully registered."

    )
  21. End

    If
  22. End

    With
  23. Catch

    ex As

    Exception
  24. MsgBox

    (

    ex.

    Message

    )
  25. End

    Try
  26. 'close the connection
  27. con.

    Close

    (

    )
  28. 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. 'set up your commands
  8. 'it holds the data to be executed
  9. With

    cmd
  10. 'pass on the value of con to the MySQL command which is Connection
  11. .

    Connection

    =

    con
  12. 'role of this is to return text presented by a command object
  13. .

    CommandText

    =

    sql
  14. End

    With

  15. 'for retrieval of data and filling the table
  16. da =

    New

    MySqlDataAdapter(

    sql, con)
  17. da.

    Fill

    (

    dt)

  18. 'to put the maxrow of a table in the textbox
  19. txt.

    text

    =

    "000"

    &

    dt.

    Rows

    .

    Count

  20. Catch

    ex As

    Exception
  21. MsgBox

    (

    ex.

    Message

    )
  22. End

    Try
  23. da.

    Dispose

    (

    )
  24. 'close the connection
  25. con.

    Close

    (

    )
  26. 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. 'conditioning the radiobutton to set the male and female
  7. ' if the radiobutton is checked it is equals to Female and if not it is equals to male
  8. If

    rdoFemaleMale.

    Checked

    =

    True

    Then
  9. radio =

    "Female"
  10. Else
  11. radio =

    "Male"
  12. End

    If

  13. 'put the insert query to variable sql as string
  14. sql =

    "INSERT INTO student (`s_id`, `s_fname`, `lastname`, `middlename`,"

    _
  15. &

    "`s_address`, `s_age`, `s_bday`, `s_bplace`, `s_gender`,"

    _
  16. &

    "`s_status`, `s_guardian`, `s_guardian_relation`,`s_guardian_add`,"

    _
  17. &

    "`s_guardian_contact`,`sy`, `yr`) VALUES ('"

    &

    txtid.

    Text

    &

    "','"

    _
  18. &

    txtFname.

    Text

    &

    "','"

    &

    txtLname.

    Text

    &

    "','"

    &

    txtMname.

    Text

    &

    "','"

    _
  19. &

    rchAddress.

    Text

    &

    "','"

    &

    txtAge.

    Text

    &

    "','"

    &

    dtpDbirth.

    Text

    &

    "','"

    _
  20. &

    rchPbirth.

    Text

    &

    "','"

    &

    radio &

    "','"

    &

    cboStatus.

    Text

    &

    "','"

    _
  21. &

    txtGuardian.

    Text

    &

    "','"

    &

    txtRelation.

    Text

    &

    "','"

    &

    rchGaddress.

    Text

    &

    "','"

    _
  22. &

    txtContact.

    Text

    &

    "' ,'"

    &

    cbosy.

    Text

    &

    "','"

    &

    cboYear.

    Text

    &

    "')"

  23. 'call your public sub name and put the sql in the parameters list
  24. create(

    sql)

  25. 'for clearing the textbox
  26. cleartext(

    Me

    )

  27. 'call your autonumber to set a new value
  28. autoNumber(

    "SELECT * FROM student"

    , txtid)

  29. End

    Sub

The complete Source Code is included. Download and run it on your computer.


Download
You must upgrade your account or reply in the thread to view the hidden content.
 

452,496

330,760

330,768

Top