How to show the records between two dates (Records are stored in Excel)
Hello all
I am beginner in vb. I have below records stored in the excel file(in 3 columns)
01/01/2013 Abc pune
30/01/2013 def mumbai
06/02/2013 ghi delhi
15/02/2013 jkl nagpur
04/03/2013 mno pune
25/05/2013 pqr satara
13/06/2013 stu newyork
05/06/2013 vwx london
17/08/2013 yzg poland
24/09/2013 hjk India
09/11/2013 iop Shrilanka
25/12/2013 plq Kanpur
I have two text box and a button , These textbox will contain the range of dates which user will enter and after clicking button all the records which are between these dates should be displayed (In datagridview).
e.g. If user enter 01/02/2013 and 30/09/2013 in 2 textbox respectively and click on button then following records should be displayed
Can anyone tell me how can we do this ?:cry::confused:
06/02/2013 ghi delhi
15/02/2013 jkl nagpur
04/03/2013 mno pune
25/05/2013 pqr satara
13/06/2013 stu newyork
05/06/2013 vwx london
17/08/2013 yzg poland
24/09/2013 hjk India
Re: How to show the records between two dates (Records are stored in Excel)
Use the BETWEEN keyword.
-tg
Re: How to show the records between two dates (Records are stored in Excel)
Hello,
Best to use a DateTimePicker unless you are using some form of validation on the TextBoxes to make sure they are dates.
The following shows how to get a date range where the sheet's first row is data, not column names. Column 1 is type date, column 2 string. Since when HDR in the connection string is NO column names are Fn so A would be F1, B would be F2 etc so for this demo I alias the columns.
Code:
Module ExcelModule
Public Sub DemoDateRange()
Dim Builder As New OleDb.OleDbConnectionStringBuilder With
{
.Provider = "Microsoft.ACE.OLEDB.12.0",
.DataSource = IO.Path.Combine(Application.StartupPath, "MyFile1.xlsx")
}
Builder.Add("Extended Properties", "Excel 12.0; HDR=No;")
Dim SheetName As String = "Sheet1"
Dim dt As New DataTable
Using cn As New OleDb.OleDbConnection With {.ConnectionString = Builder.ConnectionString}
Using cmd As New OleDb.OleDbCommand With {.Connection = cn}
cmd.CommandText =
<SQL>
SELECT F1 As TheDate, F2 As SomeColumn
FROM [<%= SheetName %>$]
WHERE F1 BETWEEN @StartDate AND @EndDate
</SQL>.Value
cmd.Parameters.Add(
New OleDb.OleDbParameter With
{
.ParameterName = "StartDate", .DbType = DbType.Date,
.Value = "1/30/2013"
}
)
cmd.Parameters.Add(
New OleDb.OleDbParameter With
{
.ParameterName = "EndDate", .DbType = DbType.Date,
.Value = "4/5/2013"
}
)
cn.Open()
dt.Load(cmd.ExecuteReader)
End Using
End Using
End Sub
End Module