VBA example - Search data form

Form to search for data

In this page, you learn how to find student’s data on a form in VBA example - Microsoft Access. You can search student’s data through one of the three fields: Student ID, Student Name, or Sex; select which option you like.

We have a form for searching students’ data as shown below:

ms access search form  

Descriptions:
-When a form loaded, it looks like the figure above; all values of controls are blank (No selection on option buttons and no check on check box). In addition, it doesn’t show list box yet unless you check on check box.
-When you click on StudentID option button, all students’ ID in table student will be populated into the combo box.
-When you click  on StudentName option button, all students’ names in table student will be listed into combo box.
-Sex option button does the same way as studentID and studentName above.
-When you check View Student Info check box, it will show all students’ data in list box as figure below:

form seach 1

-When you select data in combo box after you clicked which option button, the data will show in list box
-When you click on New Student Command Button, Student form will load for students’ new data entry.

form search 2

 

Note: For student form, you have to create it by yourself. You don’t need to create the same form as I showed you above.
To do what I elaborated above, you have to do the following:

Form Design

Drop and drag the Control and rename them:
-One option group
    +OptGroup
-3 option buttons
     +OptStudentID
     +OptStudentName
     +OptSex
-One combo box
     +cboCondition
-One check box
     +ChkViewStuInfo
-One command button
     +cmdOpenNewStudent
-One list box
     +lstViewStuInfo

Coding

-On Form load
Private Sub Form_Load()
ChckViewStuInfo.Value = 0
OptGroup.Value = 0
cboCondition.Value = ""
Me.InsideHeight = Me.InsideHeight - 4000
End Sub

-On OptGroup
Private Sub OptGroup_Click()
If OptGroup.Value = 1 Then
cboCondition.RowSource = ""
cboCondition.RowSource = "Select StudentId from TblStudent"
cboCondition.Value = cboCondition.ItemData(0)
lstViewStudentInfo.RowSource = "Select * from Tblstudent where studentID='" & cboCondition.ItemData(0) & "'"
ElseIf OptGroup.Value = 2 Then
cboCondition.RowSource = ""
cboCondition.RowSource = "Select StuName from TblStudent"
cboCondition.Value = cboCondition.ItemData(0)
lstViewStudentInfo.RowSource = "Select * from Tblstudent where stuName='" & cboCondition.ItemData(0) & "'"
Else
cboCondition.RowSource = ""
cboCondition.RowSource = "Select Sex from TblStudent"
cboCondition.Value = cboCondition.ItemData(0)
lstViewStudentInfo.RowSource = "Select * from Tblstudent where sex='" & cboCondition.ItemData(0) & "'"
End If
End Sub

-On cboCondition
Private Sub cboCondition_Change()
If OptGroup.Value = 1 Then
Me.lstViewStudentInfo.RowSourceType = "Table/Query"
Me.lstViewStudentInfo.RowSource = "Select * from TblStudent where StudentID ='" & cboCondition.Text & "'"
ElseIf OptGroup.Value = 2 Then
Me.lstViewStudentInfo.RowSource = "Select * from TblStudent where StuName='" & cboCondition.Text & "'"
Else
Me.lstViewStudentInfo.RowSource = "Select * from Tblstudent where Sex='" & cboCondition.Text & "'"
End If
End Sub

-On ChkViewStuInfo
Private Sub ChckViewStuInfo_Click()
If ChckViewStuInfo.Value = -1 Then
lstViewStudentInfo.Visible = True
 Me.InsideHeight = Me.InsideHeight + lstViewStudentInfo.Height
lstViewStudentInfo.RowSourceType = "Table/Query"
lstViewStudentInfo.RowSource = "Select * from TblStudent"
Else
 lstViewStudentInfo.Visible = False
 Me.InsideHeight = Me.InsideHeight - lstViewStudentInfo.Height
End If
End Sub

-On cmdOpenNewStudent
Private Sub cmdOpenNewStudent_Click()
DoCmd.OpenForm "frmStudent", acNormal



comment

Posted comments

Patrick:

Good explanations. Thank you :)

03-22-2013

Dara:

Global and local variables declaration example:
http://www.worldbestlearningcenter.com/tips/Global-variables-in-vba.htm

02-23-2013

ann:

thanks for sharing your knowledge it helps me a lot.

02-11-2013

Tamilan:

Pls post some examples for declaring and calling variables (local and global)

02-09-2013

M.somjate:

Thanks for example color code.

02-08-2013

G G Shah:

Heartly Thanks.God bless you.

01-18-2013

sek sam:

I like this website very much.
It has a lot of helpful helpful materials to learn excel programming.

01-01-2013

limocky:

useful ms access examples...
good web site to to learn access from scratch.

01-01-2013

bakery:

Thank for useful VBA example code...

12-27-2012

prasat:

Thank u for useful website..

11-03-2012

zal:

Thank for really helpful posts

10-28-2012

brasha:

Useful VBA examples for Excel. I really need them.

10-25-2012


.........................................................................................................................Home | Forum | About | Contact
This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word.
However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.
Copyright @ 2011-2013 worldbestlearningcenter. All Rights Reserved.
Computer-Wbest
Tips
Download
Related Posts