Results 1 to 5 of 5

Thread: [RESOLVED] Access Date searching using VB6 & SQL

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    54

    Resolved [RESOLVED] Access Date searching using VB6 & SQL

    I have a list of product serial numbers and the dates the numbers were issued in an access database. I am querying from a VB program which lets the users pick a start and end date to assign the relevant serial numbers within that date range to a recordset.

    I can a list for a specific date using SQL = "SELECT * From Serial WHERE Date LIKE " & "'" & FirstDate & "'"
    Set rsFirst = cnn1.Execute(SQL, , adCmdText)

    But can anyone tell me how to populate a recordsetwith dates in a range of say June 3rd to june 24th?

    Thank you.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Access Date searching using VB6 & SQL

    VB Code:
    1. WHERE datefield => '#" & txtFromDate.Text & "#'"
    2. AND datefield <= '#" & txtToDate.Text & "#'"
    I may have, and probably did, mess up the # signs (I always had trouble with those things), so you may have to tweak it a bit, but the fundamential logic is:

    Greater Than/Equal To [StartDate] AND Less Than/Equal To [End Date]

    Also, if your date field really is called date (and I hope it is not), then you would need to put that in brackets [Date]. Date is a reserved word, and you should always avoid using reserved words as field names.

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Access Date searching using VB6 & SQL

    You can also use a between statement
    VB Code:
    1. WHERE Format(datefield,'Short Date')  Between '#" & format(txtFromDate.Text,'Short Date') & "# AND #" & Format(txtToDate.Text,'Short Date') & "#'"
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Access Date searching using VB6 & SQL

    Quote Originally Posted by GaryMazzone
    You can also use a between statement
    VB Code:
    1. WHERE Format(datefield,'Short Date')  Between '#" & format(txtFromDate.Text,'Short Date') & "# AND #" & Format(txtToDate.Text,'Short Date') & "#'"
    I never get those # signs right.
    Last edited by Hack; Jun 16th, 2006 at 08:07 AM.

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2002
    Posts
    54

    Re: Access Date searching using VB6 & SQL

    That was great help, thank you both.

    Just out of interest, the visual basic code:WHERE datefield => '#" & txtFromDate.Text & "#'" AND datefield <= '#" & txtToDate.Text & "#'"

    only works for me when I leave out the # signs. I HAD named the date field "Date" , so I changed it and made it a text field.

    Thanks to your help I have a working search facility.

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