hi.. Im a beginner in visual basic 2010 im making my first project i created form3 for the search function so i created 2 checkbox to give the user the choice to choose to search by name or by date or by both of them and i created a button search then i created a datagridview to show up the results so im confused now how to start my project. should I start it with if checkbox1 checked then.... or what? some guidance will be appreciated.. thanks in advance

Recommended Answers

All 11 Replies

That depends on what you want to do. Can you do a search without any of the checkbox being checked? If you can then you can enable the button else only when one or more of the checkbox is checked will you be able to click the button.

There are a few ways you can do this. One easy way is to add an if..else in the button_click code so that if checkbox1 is checked, then show the results based on name. If checkbox2 is checked, then show results based on date. Either way, I assume that you are showing data from a database? correct.. so then its a matter of modifying your SQL query with a where clause.. for example... select columns from table where name = parameter, or select columns from table where date = parameter.

Hi , There are many ways to achieve your requirement. First you can decide whether you are going to give the privilege to the user to search based on more filters or filter based on one criteria.
For eg giving the option to search based on empno+name. If search required based on one criteria at a time you need to revisit the checkbox.

wen and gorgem and padagalingmg thanks for the fast replay .. but gorge yes im using database but i did not use sql yet in my project so i think your both ways i can get it better .. can you please explain which way u prefer and easier to do? and do i still can use code of sql with that code? in the other way? thanks a lot guys for all of you

and in case if i use your first way:

(if checkbox1 is checked, then show the results based on name. If checkbox2 is checked, then show results based on date)
so i still need to make my code and work on the datagridview to show the results.. right? so first i need to click on the search button to make my first code with your first way then go to the datgridview to make my other code to show the result or i can just try to use the filtering function and query in the datagridview.. right?

checkbox1 - name
checkbox2 - date

give conditions in such a way that

if checkbox1.checked = true then
'ur sql query which will show the result in datagridview
elseif checkbox2.checked = true then
'ur sql query
elseif checkbox1.checked = true and checkbox2.checked = true then
'ur sql query
else 'means if both are unchecked then display error or if u want to display all details of the table
'whatever u need
End if
commented: very nice one , Pooja , simple and clear. +4
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Collections.Generic
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Drawing.Printing
Imports System.Data.SqlClient
Imports System.Collections
Imports System.Resources
Imports System.Data.OleDb
Imports System.Windows.Forms.Control

Public Class Form1
    Inherits System.Windows.Forms.Form

    Dim connectionString As String

    Dim connection = New System.Data.OleDb.OleDbConnection()

    Dim SqlCommand As OleDb.OleDbCommand = New OleDb.OleDbCommand

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Me.DataGridView1.AllowUserToAddRows = False
End Sub


'create a find button, combobox(put your search items in the collection List), and a textbox to input the name or date you need to search on

Private Sub btnFind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFind.Click

        Dim searchItem As String = ""
        searchItem = txtSearch.Text.Trim

        If cmbSearch.SelectedItem = "" Then
            MessageBox.Show("Select a Search Item! ")
            cmbSearch.Focus()
        Else


            Dim connectionString As String = "Provider =SQLOLEDB.1; Data Source =Servername; Persist Security Info =True; Password=password; User ID=userid; Initial Catalog=databasename; Integrated Security=SSPI"
            Dim connection4 As New OleDbConnection(connectionString)


            If cmbSearch.SelectedItem = "Name" Then

                searchItem = "select  field1, field2, field3 from TABLE where Name = '" & searchItem & "'"

            ElseIf cmbSearch.SelectedItem = "Date" Then

                searchItem = "select  field1,field2,field3 from TABLE where   Date = '" & searchItem & "'"



            End If


            SqlCommand.CommandText = searchItem

            connection.Open()
            Dim ds As New DataSet()
            Dim dataadapter As New OleDbDataAdapter(searchItem, connection)

            SqlCommand.Connection = connection



            Dim sqlReader As OleDb.OleDbDataReader = SqlCommand.ExecuteReader()

            dataadapter.Fill(ds, "TABLE")
            connection.Close()
            DataGridView1.DataSource = ds
            DataGridView1.DataMember = "TABLE"


        End If

    End Sub
    End Class

poojavb thanks a lot really so helpful ..ashley24 thanks too for all the codes i learned from it new ways and new ideas .. thanks all guys for the help :)

ur welcome....if it helped u dont forget to mark the thread as solved...

my advice to beginner20 is that please try to study first , then search some code samples , after knowing all the basic logics and working of components which will be used in your coding then start coding , this will save lot of time of yours , while coding , this will also helps you to improve your self and also to code much complex programs, in this case your have to deal with 4 or five things ,
1- check box and buttons
2- grid
3- sql connection , sqldataadapter or sqldatareader , sqlcommand object , datatable
4- queries
5- simple logics how to get data from db then how to pass it your grid.

if you just spend one or two hours on them , belive me next time you will be here not to ask some thing about all these but to help others .

Best Regards

Muhammad Waqas Aslam

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.