Results 1 to 6 of 6

Thread: Search a databse using date values

  1. #1

    Thread Starter
    Member shooteronline's Avatar
    Join Date
    Oct 2002
    Location
    Colombo
    Posts
    40

    Question Search a databse using date values

    Hi,

    I have vb form whitch contain two text boxes.

    First one is Begindate
    Second one is Enddate

    If I want search a databse using begin date and end date how do i create sql statment.And also i want to display record in grid.
    How to do that? Pl explain in simple english in details steps.


    Thankx

    shooter

  2. #2
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228
    Hi,
    Use the between clause to search records between two date values.

    For e.g:-
    Code:
    Use Northwind
    go
    SELECT * FROM [ORDERS] WHERE ORDERDATE BETWEEN '1996.07.06' AND '1996.07.10'	
    go
    For populating a grid. Open a recordset with your query. And set the datasource property of the DBGrid to the recordset. That should do the trick.

    Cheers!
    Abhijit
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  3. #3
    Junior Member
    Join Date
    Oct 2002
    Location
    Belgium
    Posts
    26

    Searching between two dates

    You can make a query using a Sql string. You have to use format #Month/Day/year# in the sql String.

    ‘For example how to search between two dates begin an end.

    Dim Sql as string

    ‘Me.txtBeginDate = Textbox with first Date
    ‘Me.txtEndDate = Textbox with end Date


    Sql = “Select * from tbl... where Date(=field from tbl...) BETWEEN #" & Month(Me.txtBeginDate) & "/" & Day(Me.txtBeginDate) & "/" & year(Me.txtBeginDate) & "# And #" & Month(Me.txtEndDate) & "/" & Day(Me.txtEndDate) & "/" & year(Me.txtEndDate) & "#)”


    I hope this will help You.

  4. #4
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951
    or...

    SELECT * FROM <table> where [date field] >= " & "#" & cdate(txtstartdate.text) & "#" & " and [datefield <= " & "#" and cdate(txtenddate.text) & "#"

  5. #5
    PowerPoster abhijit's Avatar
    Join Date
    Jun 1999
    Location
    Chit Chat Forum.
    Posts
    3,228
    Originally posted by Pasvorto
    or...

    SELECT * FROM <table> where [date field] >= " & "#" & cdate(txtstartdate.text) & "#" & " and [datefield <= " & "#" and cdate(txtenddate.text) & "#"
    Pasvorto,
    This appears to be a query written for access. I am using one for SQL Server.

    Cheers!
    Abhijit
    Everything that has a computer in will fail. Everything in your life, from a watch to a car to, you know, a radio, to an iPhone, it will fail if it has a computer in it. They should kill the people who made those things.- 'Woz'
    save a blobFileStreamDataTable To Text Filemy blog

  6. #6
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687
    Here's two ways we do it:

    VB Code:
    1. Dim strSQL As String
    2. strSQL = "SELECT blah FROM tblFoo WHERE DateField BETWEEN '" & Format$(CDate(BeginDate.Text), "mm/dd/yyyy") & "' AND '" & Format$(CDate(EndDate.Text), "mm/dd/yyyy") & "'"
    3.  
    4. Set rs = objCmd.Execute strSQL
    * 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??? *

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