Results 1 to 7 of 7

Thread: Access Database Date Filter not working..

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    6

    Access Database Date Filter not working..

    Hi guyz im a newbie to VB programming hope u can help me with this.

    My problem is that i want to filter my database by date but i guess there is something wrong with my coding. It is not Filtering my database according to the range I set by using 2 DTPicker.

    Im using Access as database
    Provider = Microsoft.JET.OLEDB.4.0
    Operating Sys = XP

    'My code:

    Option Explicit

    Public cn As ADODB.Connection
    Public rs As ADODB.Recordset
    Dim SQL As String
    -------------------------------------------------------------------------
    Private Sub cmdExit_Click()
    Unload Me
    End Sub
    -------------------------------------------------------------------------
    Private Sub cmdSearch_Click()

    Set cn = New ADODB.Connection
    With cn
    .Provider = "Microsoft.Jet.OLEDB.4.0"
    .ConnectionString = "Data Source = " _
    & App.Path & "\database\log.mdb ;Persist Security Info=False"
    .CursorLocation = adUseClient
    .Open
    End With


    Set rs = New ADODB.Recordset

    DataGrid.Refresh

    SQL = "SELECT EM_ID, DATE_LOG FROM TIMELOG WHERE DATE_LOG BETWEEN ' " & DTPicker1 & " ' and ' " & DTPicker2 & " ' "

    rs.CursorLocation = adUseClient
    rs.Open SQL, cn, adOpenDynamic, adLockBatchOptimistic

    Set DataGrid.DataSource = rs

    End Sub
    -------------------------------------------------------------------------

  2. #2
    Frenzied Member
    Join Date
    Aug 2006
    Location
    India, Punjab, Bhatinda
    Posts
    1,689

    Red face Re: Access Database Date Filter not working..

    Code:
    SQL = "SELECT EM_ID, DATE_LOG FROM TIMELOG WHERE DATE_LOG BETWEEN # " & DTPicker1.Value & " # and #" & DTPicker2.Value & " # "
    pls note DTP has .Hour, .Minute, .Second, and more. If you are intrested you may use them.
    pls read the VB FAQ's/DB FAQ's as to how to pass diff datatype values.

    pls use Code Tags while posting code snipptes.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Access Database Date Filter not working..

    It isn't just the delimiters that matter, you also need to set an appropriate format - as explained in the article How do I use values (numbers, strings, dates) in SQL statements? from our Database Development FAQs/Tutorials (at the top of this forum)

  4. #4
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Access Database Date Filter not working..

    If you are using Microsoft Access, this will not work.

    Code:
    SQL = "SELECT EM_ID, DATE_LOG FROM TIMELOG WHERE DATE_LOG BETWEEN ' " & DTPicker1 & " ' and ' " & DTPicker2 & " ' "
    Replace the ( ' ) with ( # ) when using BETWEEN clause in Microsoft Access.

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2007
    Posts
    6

    Re: Access Database Date Filter not working..

    thanks

  6. #6
    Frenzied Member zynder's Avatar
    Join Date
    Nov 2006
    Location
    localhost
    Posts
    1,434

    Re: Access Database Date Filter not working..

    you are welcome. and also take note that when working with dates and datepicker, you need to make sure that the datatype in the dbase is also date. Don't make it the datatype "text" otherwise you will encounter a problem in the future.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Access Database Date Filter not working..

    Quote Originally Posted by zynder
    Replace the ( ' ) with ( # ) when using BETWEEN clause in Microsoft Access.
    That change is not good enough - the format also needs to be set appropriately, otherwise the value will be interpreted incorrectly at times (especially true if the Regional Settings date format is not US or ISO).

    For more information, see the article How can I work with dates correctly? from our Database Development FAQs/Tutorials (at the top of the Database Development forum)

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