Results 1 to 14 of 14

Thread: [RESOLVED] Search by Month Problem

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    124

    Resolved [RESOLVED] Search by Month Problem

    I have a bit of a problem in date ranges. Im using SQL Server 2k and VB6 frontend.

    I have a form with Msflexgrid populated from database. My problem is searchin by Month.

    Here is my code for the combobox. Please take a look at view by month coz im having error to that.

    vb Code:
    1. Select Case ComboBox.Text
    2.         Case "Today"
    3.        
    4.               'VIEW TODAY'S RECORDS
    5.               strSQL = "SELECT * FROM TABLE WHERE FIELD_DATE ='" & Date & "'"
    6.              
    7.         Case "Yesterday"
    8.               'VIEW YESTERDAY'S RECORDS
    9.               strSQL = "SELECT * FROM TABLE WHERE FIELD_DATE ='" & Date - 1 & "'"
    10.        
    11.         Case "This Week"
    12.               'VIEW THIS WEEK'S RECORDS
    13.               strSQL = "SELECT * FROM TABLE WHERE FIELD_DATE BETWEEN '" & _
    14.                         Date - 7 & " ' AND '" & Date & "'"
    15.              
    16.         Case "This Month"
    17.               'VIEW THIS MONTH'S RECORDS
    18.               'IM GETTING AN ERROR HERE
    19.               strSQL = "SELECT * FROM TABLE WHERE FIELD_DATE BETWEEN '" & _
    20.                        CDate(Month(Date) & "/01/" & Year(Date)) & "' AND '" & _
    21.                        CDate(Month(Date) & "/31/" & Year(Date)) & ""
    22.              
    23.         Case "This Year"
    24.               'VIEW THIS YEAR'S RECORDS
    25.               strSQL = "SELECT * FROM TABLE WHERE FIELD_DATE BETWEEN '" & _
    26.                        CDate("1/01/" & Year(Date)) & "' AND '" & _
    27.                        CDate("12/31/" & Year(Date)) & ""
    28.     End Select

    Is there any other approach to do this. I maybe doing this wrong. Thanks in anticipation.
    Last edited by Erroneous; Apr 4th, 2008 at 04:44 AM.

  2. #2
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Search by Month Problem

    CDate(Month(Date) & "/31/" & Year(Date))

    won't work for april as there isn't 31 days in april

    try

    msgbox CDate(Month(Date) & "/31/" & Year(Date))

    and

    msgbox CDate(Month(Date) & "/30/" & Year(Date))

    to see the error
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  3. #3
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Search by Month Problem

    April only has 30 days. You're assuming that all months have 31 days. CDate will fail to convert "31/04/2008" with a Type Mismatch"

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    124

    Re: Search by Month Problem

    Ok thanks koolsid for pointing out the problem. Is there a general approach to search by month using combobox?

    In the form I have an option to use a datepicker and it works with no problem but I also want to incorporate the combobox coz it's much easier than datepickers.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    124

    Re: Search by Month Problem

    Thanks Doodle for pointing out the problem. My approach was wrong. Please read post #4. thanks!

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    124

    Re: Search by Month Problem

    No can i do it by using "Today", "Yesterday", "This Week", "This Month", "This Year" on a combobox?

    Seems like all of them are working except for MONTH coz date varies from 28,29,30 and 31. Is there any other workaround?

  7. #7
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Search by Month Problem

    Hi it has nothing to do with "Today", "Yesterday", "This Week", "This Month", "This Year" on a combobox. It is ultimately how you are finaly coding your strSQL

    alternatively if you want to use that approach then what i can think of is when the user selects "This Month" then there will a code which will check the system time and then calculate total days in that month (say for example it stores it in "NoofDays") and then use it to finaly code the strSQL. say something like


    CDate(Month(Date) & "/" & NoofDays & "/" & Year(Date))

    Hope this is what you want...


    EDIT : AN EXAMPLE
    For example this function if placed in a module will give the total number of days in a month

    Code:
    Option Explicit 
     
    Public Function NoofDays(myMonth As Long) As Long 
         
        NoofDays = Day(DateSerial(Year(Date), myMonth + 1, 1) - 1) 
         
    End Function
    Where myMonth is the desired month number.

    Example

    NoofDays(4) or NoofDays(2) etc...
    Last edited by Siddharth Rout; Apr 4th, 2008 at 05:16 AM.
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  8. #8
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Search by Month Problem

    Something like this perhaps
    Code:
    Private Function DaysInThisMonth() As Integer
    Dim daNow As Date
    Dim daFirst As Date
    Dim daNext As Date
    Dim intDay As Integer
    Dim intMonth As Integer
    Dim intNextM As Integer
    Dim intDiff As Integer
    Dim intYear As Integer
    Dim intYear1 As Integer
    daNow = Now
    intMonth = Month(daNow)
    intDay = 1
    intYear = Year(daNow)
    If intMonth = 12 Then
        intNextM = 1
        intYear1 = intYear + 1
    Else
        intNextM = intMonth + 1
        intYear1 = intYear
    End If
    daFirst = CDate(intDay & "/" & intMonth & "/" & intYear)
    daNext = CDate(intDay & "/" & intNextM & "/" & intYear1)
    DaysInThisMonth = Abs(DateDiff("d", daNext, daFirst))
    End Function

  9. #9
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Search by Month Problem

    I have just edited my post with an example on how to calculate number of days in a month...

    edit:
    @doogle : you beat me to it

    however the function that i have is a shorter version...
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  10. #10
    PowerPoster
    Join Date
    Jul 2006
    Location
    Maldon, Essex. UK
    Posts
    6,334

    Re: Search by Month Problem

    Certainly simpler than mine !!

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    124

    Re: Search by Month Problem

    Wow great help guys! I'll keep you posted. I don't know the DateSerial Function yet let me check.

  12. #12
    Discovering Life Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    12,001

    Re: Search by Month Problem

    Quote Originally Posted by Erroneous
    I don't know the DateSerial Function yet let me check.
    Does this help...

    http://msdn2.microsoft.com/en-us/lib...0c(VS.80).aspx
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved


    MyGear:
    ★ CPU ★ Ryzen 5 5800X
    ★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
    ★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
    ★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
    ★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
    ★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
    ★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
    ★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
    ★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
    ★ Keyboard ★ TVS Electronics Gold Keyboard
    ★ Mouse ★ Logitech G502 Hero

  13. #13

    Thread Starter
    Lively Member
    Join Date
    Mar 2007
    Posts
    124

    Re: Search by Month Problem

    Ok guys I tested it and it works flawless.

    Here is the code. The DateSerial does the trick.

    NoOfDays(Month(Date))


    vb Code:
    1. strSQL = "SELECT * FROM TABLE WHERE FIELD_DATE BETWEEN '" & _
    2.                        CDate(Month(Date) & "/01/" & Year(Date)) & "' AND '" & _
    3.                        CDate(Month(Date) & "/" & NoOfDays(Month(Date)) & "/" & Year(Date) & ""

    Rep Added. Cheers!

  14. #14
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: [RESOLVED] Search by Month Problem

    This is my version:
    Code:
    Function DaysOfMonth(iMonth As Integer, iYear As Integer) As Integer
       '-- note that Feb may have 28 or 29 days that depends on year
       DaysOfMonth = Day(DateSerial(iYear, iMonth + 1, 0))
    End Function
    
    Function DaysOfThisMonth() As Integer
       DaysOfThisMonth = Day(DateSerial(Year(Date), Month(Date) + 1, 0))
    End Function
    
    Function DaysOfLastMonth() As Integer
       DaysOfLastMonth = Day(Date - Day(Date))
    End Function
    I don't think we need to know number of days in a month here:
    Code:
       strSQL = "SELECT * FROM TABLE WHERE "
       Select Case ComboBox.Text
           Case "Today"
                strSQL = strSQL & "FIELD_DATE ='" & Date & "'"
           Case "Yesterday"
                strSQL = strSQL & "FIELD_DATE ='" & (Date - 1) & "'"
           Case "This Week" '--???
                strSQL = strSQL & "FIELD_DATE BETWEEN '" & (Date - 6) & "' AND '" & Date & "'"
           Case "This Month"
                '-- actually we compare the last days of previous months here
                strSQL = strSQL & "(FIELD_DATE - DAY(FIELD_DATE)) = (DATE - DAY(DATE))"
                '-- or
                'strSQL = strSQL & "YEAR(FIELD_DATE) = YEAR(DATE) AND MONTH(FIELD_DATE) = MONTH(DATE)"
           Case "This Year"
                strSQL = strSQL & "YEAR(FIELD_DATE) = YEAR(DATE)"
                '--or:
                'strSQL = strSQL & "YEAR(FIELD_DATE) = " & Year(Date)
       End Select
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

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