Results 1 to 3 of 3

Thread: How to show the records between two dates (Records are stored in Excel)

  1. #1

    Thread Starter
    New Member
    Join Date
    Jun 2013
    Posts
    10

    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 ?

    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

  2. #2
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: How to show the records between two dates (Records are stored in Excel)

    Use the BETWEEN keyword.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  3. #3
    Karen Payne MVP kareninstructor's Avatar
    Join Date
    Jun 2008
    Location
    Oregon
    Posts
    6,713

    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width