Skip to main content

Find Records Between two Dates in C# and MS Access Database

In this tutorial, you are going to learn how to find records between two dates using C# and MS Access database. With the use of this method, you will be able to search the records you need in the database between two dates. On the other hand, you can filter the records and display it inside the datagridview. There are times that you might encounter this kind of problem while coding, so I hope this can help you solve it.

Creating Application

Step 1

Open Microsoft Visual Studio 2015 and create a new windows form application for c#.

Step 2

Make the Form just like shown below.

Step 3

Open the code editor and create a connection between c# and ms access database. After that, initialize all the classes that are needed.

  2. OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Application.StartupPath + "\\person_db.accdb");
  3. OleDbCommand cmd;
  4. OleDbDataAdapter da;
  5. DataTable dt;
  6. string sql;

Step 4

Create a method for retrieving data in the database that would be displayed in the datagridview.

  2. private void find_data(string sql, DataGridView dtg)
  3. {
  4. try
  5. {
  6. con.Open();
  7. cmd = new OleDbCommand();
  8. da = new OleDbDataAdapter();
  9. dt = new DataTable();
  11. cmd.Connection = con;
  12. cmd.CommandText = sql;
  14. da.SelectCommand = cmd;
  15. da.Fill(dt);
  17. dtg.DataSource = dt;
  18. dtg.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
  20. }
  21. catch (Exception ex)
  22. {
  23. MessageBox.Show(ex.Message);
  24. }
  25. finally
  26. {
  27. da.Dispose();
  28. con.Close();
  29. }
  30. }

Step 5

Double click the form and call the method that you have created to display the data into the datagridview in the first load of the form.

  2. private void Form1_Load(object sender, EventArgs e)
  3. {
  4. sql = "Select Fullname,u_name as Username, u_type as UserRole,DateJoined From tbluser";
  5. find_data(sql, dataGridView1);
  6. }

Step 6

Double click the button and do the following codes for searching data between two dates.

  2. private void button1_Click(object sender, EventArgs e)
  3. {
  4. sql = "Select Fullname,u_name as Username, u_type as UserRole,DateJoined From tbluser WHERE DateJoined >= #" + dtpFrom.Value.ToString("MM/dd/yyyy") + "# and DateJoined >= #" + dtpFrom.Value.ToString("MM/dd/yyyy") + "#";
  5. find_data(sql, dataGridView1);
  6. }

Note: Add using System.Data.OleDb; above the namespace to access OleDB libraries.

The complete source code is included. You can download it and run it on your computer.
For more question about this article. You can contact me @
Email – [email protected]
Mobile No. – 09305235027 – TNT
FB Account –
Or feel free to comment below.v

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.


1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Add new comment

This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.