Search function in using an access db

Posted on 2007-04-10
Last Modified: 2008-01-09
I would like to be able to have a text box where the user enters a name (which is also the field name) and upon clicking the find button, the form will find that record. A find next button will take you to the next record. I am using as the front end with Access as the back end. How can I go about doing this.
Question by:carlosm06
  • 3
  • 3
  • 2
LVL 27

Expert Comment

ID: 18885630
A very easy way to do this is create a BindingSource and set it's DataSource to whatever list or source you have, then set the BindingSource the DataSource of a BindingNavigator.

First, Add a BindingNavigator to your form, we can call "bn"
Second, create your BindingSource:
Dim bs As New BindingSource()
bs.DataSource = MyList     'Whatever your list is.
bn.BindingSource = bs

If you have a textbox name "Text1" you can filter your bindingsource as follows:
bs.Filter = "ColumnName='" & Text1.Text & "'"

When you want to remove the filter, do as follows:

LVL 27

Accepted Solution

VBRocks earned 250 total points
ID: 18885772
Here is a little example I put together:

First, I created a form and added a BindingNavigator, a textbox to display the data (tbMyColumn), a textbox for search criteria (tbSearch)
Second, I created a DataSet in the designer and added 1 table to it called "MyTable" with 1 column named "MyColumn.

    Private bs As New BindingSource()

    Private Sub Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer
        Dim ds As New DataSet1()
        Dim row As DataSet1.MyTableRow

        'Load the dataset with data
        For i = 1 To 10
            row = ds.MyTable.NewMyTableRow()
            row.MyColumn = i



        'Set the datasource of the BindingSource
        bs.DataSource = ds.MyTable

        'Set the BindingSource for the BindingNavigator
        Me.BindingNavigator1.BindingSource = bs

        'Bind the Text property to your column
        Me.tbMyColumn.DataBindings.Add(New Binding("Text", bs, "MyColumn"))

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'This is the button that the user clicks to perform the search
        bs.Sort = "MyColumn ASC"
        bs.Filter = "MyColumn='" & Me.tbSearch.Text & "'"

    End Sub

LVL 18

Expert Comment

ID: 18886256
If you dont want to show only filtered records (like I dont), the other way would be to do a loop through all the records and search for criteria in desired column. If you find a match, you remember the position of the record that matches the criteria. If user preses find next button, you ill loop again through records, but starting from a record that matched the criteria.

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.


Author Comment

ID: 18892369
So if I had the textbox on a seperate form how could I call the record on main form.  Right now I have a small form that users can type in a name and click the search button. When the record is found, I would like to bring up another form with the found record. This is the form used to input data...
LVL 18

Assisted Solution

Priest04 earned 250 total points
ID: 18894222
1) You show this separate form modally
2) on this separate form have some public variable, to which you will assign value from textbox.
3) When the user clicks search button, close the separate from
4) the code will be returned to main form, so you can read the search value that is stored in a public variable
5) after that, you can use either VBRocks's suggestion, or mine, it all depends what info do you have on this main form, is data shown only for one record, or data are presented in some tabular form (like in datagridview).

LVL 27

Expert Comment

ID: 19258516
Definitely still interested.  I provided a simple and effective, but detailed example.


Author Comment

ID: 19258626
Where's the example vbrocks?
LVL 18

Expert Comment

ID: 19264787
carlosm06, do you find our answers unhelpful?


Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
Introduction When many people think of the WebBrowser ( control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question