How to Make a Search Form for a Visual Basic Project

How to Make a Search Form for a Visual Basic Project

Most computer programs connected to a database file allow end users to view and manipulate data without having to interact directly with the database. In a data entry application, a search form allows the user to look for specific records in a table that matches the criteria provided. This limits the amount of data downloaded from the database and displayed on the form. The information retrieved can then be analyzed, edited, deleted or sent to the printer to produce a hard copy. To create a search form for a Visual Basic Project, the programmer must have basic knowledge in Visual Basic form design and coding, as well as database query.

Instructions

    1

    Load the Visual Basic environment from your computer by clicking "Start>Programs>Microsoft Visual Studio 6.0>Microsoft Visual Basic 6.0".

    2

    Select "Standard EXE" from the "New Project" dialog box that pops up. Click "Open" button to proceed.

    3

    Drag and drop controls from the "Toolbox" into the form to design the interface and set the individual properties afterward under the "Properties" window. A search form usually has text boxes where users type words to search against the database, labels, some command buttons, grid to display the result, and database control that links the form into the table within the database. Continue with designing the form as preferred.

    4

    Click "View>Code" from the menu to go to the code section. This is where you will write the source codes for your project.

    5

    Go to "Form_Load" event by clicking the appropriate event name from the drop-down boxes in the codes section. Input a code similar to the one below. This code will set the connection properties of your data control and connects the grid to the database file.

    With Adodc1
    .ConnectionString = "[Type your connection string here]"
    .CursorType = adOpenKeyset
    .CursorLocation = adUseClient
    .LockType = adLockOptimistic
    .CommandType = adCmdTable
    .RecordSource = "[Type the name of a table or query here]"
    .Refresh
    End With

    With Grid1
    Set .DataSource = Adodc1
    .HighLight = flexHighlightWithFocus
    .ColWidth(0) = .ColWidth(0) + 1000 'this is sample column dimension; change as preferred
    .ColWidth(1) = .ColWidth(1) + 1800
    .ColWidth(2) = .ColWidth(2) + 1800
    .ColWidth(3) = .ColWidth(3) + 2300
    .Refresh
    .TabIndex = 0
    End With

    6

    Double-click the text box where the user will enter the search criteria. For example, the user wants to search certain last names from the database and he will type the letters into the search box. When matches are found, the records will be displayed on the grid. In the code section, go to "Text1_Change()" event and input a code similar to the following:

    Dim t as String

    If Text1.Text <> "" Then
    If Adodc1.Recordset.RecordCount <> 0 Then
    t = "*" + Text1.Text+ "*"
    Adodc1.Recordset.Filter = "LastName like '" + t + "'"
    End If
    Else
    Adodc1.Recordset.Filter = adFilterNone
    End If

    7

    Press the "F5" key to run the project. Test the program and check if it's running as it should.

    8

    Save the project by clicking "File>Save Project As" from the menu and providing a descriptive filename.

Blog Archive