How to Filter Records Using Date Range in Visual Basic 6


Searching records using date ranges are oftentimes used in computer programming to filter specific records that fall within the dates specified. These records must at least have one field with date data type. For example, in a reporting module in a Visual Basic program, users can specify the beginning and end dates by clicking a drop-down calendar, then clicking a command button to filter the reports within that particular period. You can filter records using date range in Visual Basic 6 by following these steps.

Things You'll Need

  • Computer
  • Visual Basic 6 software
  • Database file
  • Start Visual Basic from your computer by clicking "Start > Programs > Microsoft Visual Studio 6.0 > Microsoft Visual Basic 6.0."

  • Start a new Visual Basic project by selecting "Standard EXE" from the "New Project" dialog box that pops up. Click "Open" to load the program environment.

  • Add controls by clicking them from the "Toolbox" one at a time and dropping them on the form. For example, if you want to create a program that filters employees who got hired within January 1, 2000 and December 31, 2009, you may need to add two "DTPicker" controls, "ADO" control to set the connection to a database, a command button, and a grid control to display the records. Since the "DTPicker" control is not part of the default controls found in the "Toolbox", you need to add it. To do this, right-click the "Toolbox" and click "Components". Under the "Controls" tab, scroll down and check "Microsoft Windows Common Controls-2 6.0". Click "Close" when done.

  • Continue designing your program interface. Arrange the controls within the form and set individual properties for each control.

  • Click "View Code" button on top of the "Project Explorer" window to start writing codes. At the "Form_Load" event, open your database connection.

  • Click the VB command button that executes the command to filter the records using date range. At the "Click" event, write a code similar to the following:

    Dim rs as New ADODB.Recordset
    Dim dtFrom, dtTo as Date

    If rs.State = adStateOpen Then rs.Close
    rs.CursorLocation = adUseClient
    rs.Open "Select * from [Table_Name] where [Beginning_Date_field] >='" & dtFrom & "' and End_Date_field <='" & dtTo & "'", conn, adOpenDynamic, adLockOptimistic, adCmdText
    If rs.RecordCount > 0 Then
    'display the filtered results in the grid
    End If

  • Run the program by pressing F5 on your keyboard. Click the drop-down calendars and set the beginning and end dates. Click command button to execute.

  • Correct any syntax or logical errors, if any.

  • Save your work by pressing "Ctrl" + "S" on the keyboard.

Related Searches

Promoted By Zergnet


You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!