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

Advertise Here

Advertise Here

Advertise Here

Search and Retrieve a Record from Access Database and VB.NET

DoughZo

Waifu Strategist
D Rep
0
0
0
Rep
0
D Vouches
0
0
0
Vouches
0
Posts
47
Likes
164
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 200 XP
In this tutorial, i will teach you on how to create a program that can search and retrieve a record from an access database and vb.net.

Now, let's start this tutorial!

1. Create an access file with the table, entities, and record below. Name your access database as sample.mdb (2003 format).

searchaccess1.png

searchaccess2.png


2. Create a Windows Form Application for this tutorial by following the following steps in Microsoft Visual Studio: Go to File, click New Project, and choose Windows Application.

3. Next, add only one Button named btnFind and labeled it as "Find". This will search the inputted text in the searched textbox. Insert 4 textbox named txtSearch for the searching of student number, txtName for Student Name, txtCourse for Student course, and txtSection for student section. You must design your interface like this:

searchdesign_0.png


4. Create a module in your project and named it, modConnection.

Import Imports

System.

Data

.

OleDb

library. This library package is for ms access database.

In your module connection, initialize the following variables.
  1. Module

    modConnection

  2. Public

    cn As

    New

    OleDb.

    OleDbConnection
  3. Public

    cm As

    New

    OleDb.

    OleDbCommand
  4. Public

    dr As

    OleDbDataReader

Now, create a method named connection to have the connection string. This will locate the sample.mdb access database that we have created earlier.
  1. Public

    Sub

    connection(

    )
  2. cn =

    New

    OleDb.

    OleDbConnection
  3. With

    cn
  4. 'Provider must be Microsoft.Jet.OLEDB.4.0, find the access file, and test the connection
  5. .

    ConnectionString

    =

    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="

    &

    Application.

    StartupPath

    &

    "\sample.mdb"
  6. .

    Open

    (

    )
  7. End

    With
  8. End

    Sub

Full code of the modConnection module.

  1. Imports

    System.

    Data

    .

    OleDb
  2. Module

    modConnection

  3. Public

    cn As

    New

    OleDb.

    OleDbConnection
  4. Public

    cm As

    New

    OleDb.

    OleDbCommand
  5. Public

    dr As

    OleDbDataReader

  6. Public

    Sub

    connection(

    )
  7. cn =

    New

    OleDb.

    OleDbConnection
  8. With

    cn
  9. .

    ConnectionString

    =

    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="

    &

    Application.

    StartupPath

    &

    "\sample.mdb"
  10. .

    Open

    (

    )
  11. End

    With
  12. End

    Sub
  13. End

    Module

5. Back to our form, put this code for the form_load. We will call the connetion() method that we have created in our module. Because we all know that module is access throughout the entire program.

  1. Private

    Sub

    Form1_Load(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    MyBase

    .

    Load
  2. Call

    connection(

    )
  3. End

    Sub

6. For the btnFind as the search button, put this code below.

We will first create a try and catch method to have the exception handling. In the try method, we will create a Boolean variable named found for finding the student number. Here, we will use the OleDbCommand with the method of connection, CommandType.Text, and CommandText, with the OleDbDataReader to execute the CommandText.
  1. Dim

    found As

    Boolean
  2. Try

  3. cm =

    New

    OleDb.

    OleDbCommand
  4. With

    cm
  5. .

    Connection

    =

    cn
  6. .

    CommandType

    =

    CommandType.

    Text
  7. .

    CommandText

    =

    "SELECT * FROM tblStudent WHERE (Snum = '"

    &

    txtSearch.

    Text

    &

    "')"
  8. dr =

    .

    ExecuteReader
  9. End

    With

Next is to create a while statement for dr.Read to retrieve and display the data to the specified textbox, as well as making the Boolean variable found into True. Make cn.Close() in there and exit sub after finding the student number.

  1. While

    dr.

    Read

    (

    )

  2. txtName.

    Text

    =

    dr(

    "Sname"

    )

    .

    ToString
  3. txtCourse.

    Text

    =

    dr(

    "Scourse"

    )

    .

    ToString
  4. txtSection.

    Text

    =

    dr(

    "Ssection"

    )

    .

    ToString
  5. found =

    True

  6. End

    While
  7. cn.

    Close

    (

    )
  8. Exit

    Sub

If the inputted student number is not in the database, put this code below.
  1. If

    found =

    False

    Then

    MsgBox

    (

    "Student ID not found."

    , MsgBoxStyle.

    Critical

    )
  2. dr.

    Close

    (

    )

Here is the full source code of our Form.
  1. Public

    Class

    Form1

  2. Private

    Sub

    btnFind_Click(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    btnFind.

    Click
  3. Dim

    found As

    Boolean
  4. Try

  5. cm =

    New

    OleDb.

    OleDbCommand
  6. With

    cm
  7. .

    Connection

    =

    cn
  8. .

    CommandType

    =

    CommandType.

    Text
  9. .

    CommandText

    =

    "SELECT * FROM tblStudent WHERE (Snum = '"

    &

    txtSearch.

    Text

    &

    "')"
  10. dr =

    .

    ExecuteReader
  11. End

    With
  12. While

    dr.

    Read

    (

    )

  13. txtName.

    Text

    =

    dr(

    "Sname"

    )

    .

    ToString
  14. txtCourse.

    Text

    =

    dr(

    "Scourse"

    )

    .

    ToString
  15. txtSection.

    Text

    =

    dr(

    "Ssection"

    )

    .

    ToString
  16. found =

    True

  17. End

    While
  18. cn.

    Close

    (

    )
  19. Exit

    Sub
  20. If

    found =

    False

    Then

    MsgBox

    (

    "Student ID not found."

    , MsgBoxStyle.

    Critical

    )
  21. dr.

    Close

    (

    )
  22. Catch

    ex As

    Exception

  23. End

    Try
  24. End

    Sub

  25. Private

    Sub

    Form1_Load(

    ByVal

    sender As

    System.

    Object

    , ByVal

    e As

    System.

    EventArgs

    )

    Handles

    MyBase

    .

    Load
  26. Call

    connection(

    )
  27. End

    Sub
  28. End

    Class

After that, Press F5 to run the program.

Output:
searchoutput.png


Hope this helps! :)

Best Regards,

Engr. Lyndon R. Bermoy

IT Instructor/System Developer/Android Developer

Mobile: 09079373999

Telephone: 826-9296

E-mail:[email protected]

Visit and like my page on Facebook at: Bermz ISware Solutions

Subscribe at my YouTube Channel at: SerBermz


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

452,496

342,733

342,741

Top