What's wrong with this SQL for Date?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MNNovice
    Contributor
    • Aug 2008
    • 418

    What's wrong with this SQL for Date?

    My form has two text boxes, txtDateFrom and txtDateTo. A macro button with the caption Month is to automatically fill in txtDateFrom and txtDateTo with current month date. My code reads as:

    Code:
    Private Sub cmdmonth_Click()
    'Sets the Date From and Date To text boxes
    'to show complete month (from start to end of current month)
    
        Me!txtdatefrom = CDate("01/" & Month(Date) & "/" & Year(Date))
        Me!txtDateTo = DateAdd("d", -1, DateAdd("m", 1, Me!txtdatefrom))
    
    End Sub
    But it's giving me wrong date: 1/5/09 and 2/4/09. I expect it to show 5/1/09 and 5/31/09

    By the way, macros for Today, Week and Year are working just fine. These codes are as follow:

    TODAY
    Code:
    Private Sub cmdtoday_Click()
    'Sets the Date From and Date To text boxes
    'to Today's Date
    
        Me!txtdatefrom = Date
        Me!txtDateTo = Date
    
    End Sub
    THIS WEEK
    Code:
    Private Sub cmdweek_Click()
    'Sets the Date From and Date To text boxes
    'to show complete working week (Mon - Fri)
        
        Dim today
    
        today = Weekday(Date)
        
        Me!txtdatefrom = DateAdd("d", (today * -1) + 2, Date)
        Me!txtDateTo = DateAdd("d", 6 - today, Date)
    
    End Sub
    I need your experties to help me with my code for MONTH. Thanks.
  • MNNovice
    Contributor
    • Aug 2008
    • 418

    #2
    To get current month (05/01/2009 - 05/31/2009) I modified the code to read as:

    Code:
    Private Sub cmdmonth_Click()
    'Sets the Date From and Date To text boxes
    'to show complete month (from start to end of current month)
    
        Me!txtdatefrom = DateAdd("m", (today * -1), Date)
        Me!txtDateTo = DateAdd("d", -1, DateAdd("m", 1, Me!txtdatefrom))
    
    End Sub
    but the end result has been 5/15/2009 - 6/14/2009.

    Is it possible to get This month instead of a month from today?

    Thanks.

    Comment

    • Denburt
      Recognized Expert Top Contributor
      • Mar 2007
      • 1356

      #3
      Is this a double post? If it is do you mind if I remove the other one? If it isn't please let me know so I can adjust the title as needed.



      Code:
         Me!txtdatefrom = Dateserial(Year(Date),month(Date),1)
           Me!txtDateTo =Dateserial(Year(Date),month(Date),1-1)

      Comment

      • IT Couple
        New Member
        • May 2009
        • 36

        #4
        Hi

        Try adding the format function
        format(date, "dd/mm/yyyy") or format(date, "short date")

        Regards
        Emil

        Comment

        • Denburt
          Recognized Expert Top Contributor
          • Mar 2007
          • 1356

          #5
          Originally posted by IT Couple
          Hi

          Try adding the format function
          format(date, "dd/mm/yyyy") or format(date, "short date")

          Regards
          Emil
          The format function would return a Variant (String) not an actual date if you would like to keep it as a date you would need to convert it like so:
          Code:
          CDate(format(date, "dd/mm/yyyy"))
          Just an FYI

          Comment

          • Chinde
            New Member
            • Feb 2009
            • 52

            #6
            The format function would return a Variant (String) not an actual date if you would like to keep it as a date you would need to convert it like so:
            Code:
            CDate(format(date, "dd/mm/yyyy"))
            Just an FYI
            Good tip thank you!!

            Comment

            • MNNovice
              Contributor
              • Aug 2008
              • 418

              #7
              Denburt:

              The following code:
              Code:
                 Me!txtdatefrom = Dateserial(Year(Date),month(Date),1) 
                   Me!txtDateTo =Dateserial(Year(Date),month(Date),1-1)
              gave me this:
              Start Date: 5/1/09
              End Date: 4/30/09

              So I changed it to read as
              Code:
                 Me!txtdatefrom = Dateserial(Year(Date),month(Date),1) 
                   Me!txtDateTo =Dateserial(Year(Date),month(Date),1+30)
              Now I got: Start date: 5/1/09 and End Date 5/31/09

              BUT what happens when the month has 28 days or 30 days? Can I include an IIF statement somewhere to make it dynamic?

              Thanks.

              Comment

              • Denburt
                Recognized Expert Top Contributor
                • Mar 2007
                • 1356

                #8
                I should have added a month then subtracted 1-1in the day field to get the last day of any month. Yes I missed a little something there. The following will suit your needs and will take care of the ending date.
                Code:
                   Me!txtdatefrom = Dateserial(Year(Date),month(Date),1) 
                     Me!txtDateTo =Dateserial(Year(Date),month(Date)+1,1-1)

                Comment

                • Denburt
                  Recognized Expert Top Contributor
                  • Mar 2007
                  • 1356

                  #9
                  Originally posted by Chinde
                  Good tip thank you!!
                  Your quite welcome, glad you found it useful.

                  Comment

                  • MNNovice
                    Contributor
                    • Aug 2008
                    • 418

                    #10
                    Denburt: Many thanks. It worked just fine.

                    How do I close this thread now? Just delete from "my subscription?" Thanks.

                    Comment

                    • Denburt
                      Recognized Expert Top Contributor
                      • Mar 2007
                      • 1356

                      #11
                      i would think that should work I am still getting used to some of the new functionality. So i am hit or miss on a lot of that. Generally I tend to keep myself subscribed (not sure if this is good or not) but if someone else makes a post even a year or so down the road it could be useful information, so I would like a notice but that's me (yeah I am kind of a pack rat).

                      Comment

                      • NeoPa
                        Recognized Expert Moderator MVP
                        • Oct 2006
                        • 32662

                        #12
                        For this I always use something like :
                        Code:
                        Me.txtDateFrom = Format(Date(), "1 mmmm yyyy")
                        Me.txtDateTo = Format(DateAdd("m", 1, Date()) - 1, "d mmmm yyyy")

                        Comment

                        Working...