Results 1 to 11 of 11

Thread: Querying Access on Dates

  1. #1
    da_beano
    Guest

    Querying Access on Dates

    I'm having real problems with this, for the last few days. It beat out the general vb forum too.

    Basically I want to enter 2 dates on a form and have the program query a database and show all flights between those 2 dates. The problem is that when the value entered for the Day part of the date is over 12, the date seems to be ignored (as well as the remainder of the SQL statement).

    Code:
    Private Sub cmdSrch_Click()
    
    date1 = txtMonth1 & "/" & txtDate1 & "/" & txtYr1
    date2 = txtMonth2 & "/" & txtDate2 & "/" & txtYr2
    
    MsgBox "Flights between " & Format(date1, "dd/mmm/yy") & " and " & Format(date2, "dd/mmm/yy")
    
    datFlights.RecordSource = "SELECT * FROM tblFlight WHERE tblFlight.DDate BETWEEN #" & date1 & "# AND #" & date2 & "# ORDER BY tblFlight.DDate"
    datFlights.Refresh
    End Sub
    The result is that if the day of date2 is > 12, all the dates greater than date1 are returned. Something else that's confusing the hell out of me: if txtDate1 > 12 then date2 is treated as the low date.

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367
    It looks ok to me, I would double check your string in access.

    Do something like this:

    Code:
    strSQL = "SELECT * FROM ..."
    Debug.Print strSQL
    You can then copy the string into access and play with it in the query builder and figure out where your problem may lie. You may also want to check the format of the dates you are passing to access.

    Just my thoughts and hope they help,

  3. #3
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    Originally posted by da_beano
    I'm having real problems with this, for the last few days. It beat out the general vb forum too.

    Basically I want to enter 2 dates on a form and have the program query a database and show all flights between those 2 dates. The problem is that when the value entered for the Day part of the date is over 12, the date seems to be ignored (as well as the remainder of the SQL statement).

    Code:
    Private Sub cmdSrch_Click()
    
    date1 = txtMonth1 & "/" & txtDate1 & "/" & txtYr1
    date2 = txtMonth2 & "/" & txtDate2 & "/" & txtYr2
    
    MsgBox "Flights between " & Format(date1, "dd/mmm/yy") & " and " & Format(date2, "dd/mmm/yy")
    
    datFlights.RecordSource = "SELECT * FROM tblFlight WHERE tblFlight.DDate BETWEEN #" & date1 & "# AND #" & date2 & "# ORDER BY tblFlight.DDate"
    datFlights.Refresh
    End Sub
    The result is that if the day of date2 is > 12, all the dates greater than date1 are returned. Something else that's confusing the hell out of me: if txtDate1 > 12 then date2 is treated as the low date.
    Try using the Format() function in the query itself. If you are using DAO this may be possible.

    Code:
    "SELECT * FROM <YourTable> WHERE FORMAT(<DateField>, 'dd/mm/yy') BETWEEN '" & _
        Format(Date1, "dd/mm/yy") & "' AND '" & Format(Date2, "dd/mm/yy")"
    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  4. #4
    Frenzied Member PilgrimPete's Avatar
    Join Date
    Feb 2002
    Posts
    1,313
    Sounds like a date settings issue. Access does some pretty odd things with dates.

    Try this:
    VB Code:
    1. Private Sub cmdSrch_Click()
    2.  
    3. date1 = txtMonth1 & "/" & txtDate1 & "/" & txtYr1
    4. date2 = txtMonth2 & "/" & txtDate2 & "/" & txtYr2
    5.  
    6. MsgBox "Flights between " & Format(date1, "dd/mmm/yy") & " and " & Format(date2, "dd/mmm/yy")
    7.  
    8. datFlights.RecordSource = "SELECT * FROM tblFlight WHERE tblFlight.DDate BETWEEN #" & Format(date1, "dd mmm yyyy") & "# AND #" & Format(date2, "dd mmm yyyy") & "# ORDER BY tblFlight.DDate"
    9. datFlights.Refresh
    10. End Sub

  5. #5
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    Nothing wrong with the dates..

    Have you looked at this :
    VB Code:
    1. date1 = txtMonth1 & "/" & txtDate1 & "/" & txtYr1
    2. date2 = txtMonth2 & "/" & txtDate2 & "/" & txtYr2
    These are NOT dates

    They are strings, thus format won't work on them.
    If you wanted dates, you should've defined them as dates and enclosed the whole lot in CDate().

    This means :
    VB Code:
    1. MsgBox "Flights between " & Format([b]CDate(date1)[/b], "dd/mmm/yy") & " and " & Format([b]CDate(date2)[/b], "dd/mmm/yy")
    2.  
    3. datFlights.RecordSource = "SELECT * FROM tblFlight WHERE tblFlight.DDate BETWEEN #" & [b]date1[/b] & "# AND #" & [b]date2[/b] & "# ORDER BY tblFlight.DDate"
    ...would suffice.


    Also, someone else on the forum mentioned this before and I've found it helps clear up confusions; Use dd mmm yyyy formating from the text boxes as you won't get confused (nor will the user or the database) about american or english formatting.

    Regards

    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6
    da_beano
    Guest
    If you mean I should have defined them when I swas declaring the variables, they were declared like
    Code:
    Dim date1 As Date
    Dim date2 As Date
    I don't understand the Cdate function, what's that about?

  7. #7
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343
    CDate(<string version of date>) returns the date specified or an error if its not a date.

    If you open the debug window and do :
    Print CDate("10 Jan 2002") you'll get 10/01/2002 - which is a date.

    How the strings were being stored into the date without erroring I dunno.

    But try putting the CDate around the string dates :
    VB Code:
    1. date1 = CDate(txtMonth1 & "/" & txtDate1 & "/" & txtYr1)
    2. date2 = CDate(txtMonth2 & "/" & txtDate2 & "/" & txtYr2)

    The rest of the (original) code should be ok.

    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  8. #8
    da_beano
    Guest

    Angry

    Was worth a try, but still hasn't worked. I'm thinking of giving up and just storing the dates as numbers (possibly a set of 3 numbers... I hope I dont have to resort to that) because I'm running out of time to get this finished.

    I know I'm only a newbie, but I still cant believe I've tried so much and nothing works. It's not like the code is that comlicated... I'm close to tears LOL

  9. #9
    Hyperactive Member
    Join Date
    Mar 2002
    Posts
    424
    It's nothing silly like the fact that dates are reversed in the East? Like in Canada we handle month day year and over there you handle day month year? Could your system not be taking anything in the day field over 12 because it's really the month field?

    Just a thought.

  10. #10
    da_beano
    Guest
    That was one of the first things I tried. In fact that's why the MsgBox is in there - you'll notice it prints the dates as dd/mmm/yy just to make sure that that ISN'T the problem, it does in fact interpret the dates correctly. Presumably VB and Access and other programs just lookup some system variable that sets what format the dates should default to.

  11. #11
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Querying Access on Dates

    Originally posted by da_beano

    VB Code:
    1. public function Nnz(Byval varA as variant, ByVal varB as variant) as variant
    2.   nnz=iif(isnull(vara),varb,vara)
    3. end function
    4.  
    5. Private Sub cmdSrch_Click()
    6.  
    7. Dim date1 as date, date2 as date
    8. Dim blnValid as boolean
    9.  
    10. 'On error resume next
    11.  
    12. '---- perform validation on data on form
    13. blnvalid=true
    14. if nnz(Len(txtdate1.text),0)=0 then blnvalid=false
    15. if nnz(Len(txtmonth1.text),0)=0 then blnvalid=false
    16. if nnz(Len(txtyr1.text),0)=0 then blnvalid=false
    17. if nnz(Len(txtdate2.text),0)=0 then blnvalid=false
    18. if nnz(Len(txtmonth2.text),0)=0 then blnvalid=false
    19. if nnz(Len(txtyr2.text),0)=0 then blnvalid=false
    20.  
    21. if not blnvalid then
    22.  msgbox "Data invalid!"
    23.  exit sub
    24. Enf if
    25.  
    26. '---- Process
    27. date1 = CDate(txtDate1 & "/" & txtMonth1 & "/" & txtYr1)
    28. date2 = CDate(txtDate2 & "/" & txtMonth2 & "/" & txtYr2)
    29.  
    30. '---- check dates correct
    31. MsgBox "Flights between " & Format(date1, "dd/mmm/yy") & " and " & Format(date2, "dd/mmm/yy")
    32.  
    33. datFlights.RecordSource = "SELECT * FROM tblFlight WHERE tblFlight.DDate BETWEEN #" & format(date1, "dd mmm yyyy")& "# AND #" & format(date2,"dd mmm yyyy") & "# ORDER BY tblFlight.DDate;"
    34. datFlights.Refresh
    35. End Sub
    OK, hmmm your code should look a little like that.

    I've added in a validation check for the data entered and changed bits and pieces around. Can you please test and lemme know what happens.

    Note : I've added my nnz function above the sub - this should ideally go into a module...

    Vince

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

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