VBA Incorrect DatePart

VSolano

Registered User.
Local time
Today, 12:32
Joined
Feb 21, 2017
Messages
89
I have the following code which I am trying to pull out the quarter for the current date. I have a variable for the current date and it is storing the date correctly, the issue is when I got to the select case for the quarter today's date turns in 01/03/1900.

Any guide will be greatly appreciate because I am going clueless on this.

Code:
Public Sub FRFilter()
Dim TodayDate As Date
TodayDate = DateValue(Date)

STFilter = ""
If Me!cboassignedto <> "" Then
    STFilter = " TaskAssignedTo = " & Me!cboassignedto & ""
   Debug.Print STFilter
   
End If
    
If Me!cbostatus <> "" Then
  
    If STFilter = "" Then
        STFilter = " taskstatus = " & Me!cbostatus & ""
        
    Else
         STFilter = STFilter & " AND  taskstatus = " & Me!cbostatus & ""
        
    End If
 End If
 
 If Me!cbodatefilter <> "" Then
    
    If STFilter = "" Then
        Select Case Me!cbodatefilter
            Case 1 ' This Month
               STFilter = " month(taskduedate) = " & Month(TodayDate) & ""
              
            Case 2 ' This Quarter
                
                TodayDate = DatePart("q", Now)
                Debug.Print TodayDate
                STFilter = DatePart("q", (TaskDueDate)) = " & TodayDate & "
                Debug.Print STFilter
            Case 3 ' This Year
            Case 4 ' Last Year
            Case 5 ' Date Range
            
        End Select
       
    Else
    'nothing yet
    
    End If
    
 End If
 
Is your date format mm/dd/yyyy? If not, you need to convert it using Format(DateField, "mm/dd/yyyy")
 
Is your date format mm/dd/yyyy? If not, you need to convert it using Format(DateField, "mm/dd/yyyy")

Works fine for me in UK format? :confused:
 
Re: VBA Incorrect DatePart Result

it does not work. I am still getting the same result
 
Inspect the variables in debug.

This does not look correct either?

Code:
STFilter = DatePart("q", (TaskDueDate)) = " & TodayDate & "

evaluates to False?
 
Last edited:
When I am evaluating that part, it return a false result

Code:
If STFilter = "" Then
        Select Case Me!cbodatefilter
            Case 1 ' This Month
               STFilter = " month(taskduedate) = " & Month(TodayDate) & ""
               Debug.Print STFilter
            Case 2 ' This Quarter
                
                MyDatePart = DatePart("q", TodayDate)
                DBDatePart = DatePart("q", TaskDueDate)
                Debug.Print MyDatePart
                STFilter = DatePart("q", TaskDueDate) = " & mydatepart & "
                Debug.Print STFilter
 
I am just trying to pull all the value from TaskDueDate base on the current quarter
 
I am just trying to pull all the value from TaskDueDate base on the current quarter

I realise that, but that code is not doing that.
Can you upload your DB? as from what you have shown, I am having a hard time seeing how you can get 01/03/1900 ?
 
I fixed the issue with the 01/03/1900. It was the variable type.

The issue is the SQL on the quarter part that is not pulling the any information

Code:
Public Sub FRFilter()
Dim TodayDate As Date
Dim MyDatePart As Integer
Dim DBDatePart As Integer
TodayDate = Format(Date, "mm,dd,yyyy")
 

STFilter = ""
If Me!cboassignedto <> "" Then
    STFilter = " TaskAssignedTo = " & Me!cboassignedto & ""
   Debug.Print STFilter
   
End If
    
If Me!cbostatus <> "" Then
  
    If STFilter = "" Then
        STFilter = " taskstatus = " & Me!cbostatus & ""
        
    Else
         STFilter = STFilter & " AND  taskstatus = " & Me!cbostatus & ""
        
    End If
 End If
 
 If Me!cbodatefilter <> "" Then
    
    If STFilter = "" Then
        Select Case Me!cbodatefilter
            Case 1 ' This Month
               STFilter = " month(taskduedate) = " & Month(TodayDate) & ""
               Debug.Print STFilter
            Case 2 ' This Quarter
                
                MyDatePart = DatePart("q", TodayDate)
                DBDatePart = DatePart("q", TaskDueDate)
                Debug.Print MyDatePart
                STFilter = DatePart("q", TaskDueDate) = " & mydatepart & "
                Debug.Print STFilter
            Case 3 ' This Year
            Case 4 ' Last Year
            Case 5 ' Date Range
            
        End Select
       
    Else
    'nothing yet
    
    End If
    
 End If
 
Try comparing your STFilters ?

Code:
STFilter = "DatePart('q', TaskDueDate) = " & mydatepart
 
I'm not sure the format for todaydate will work...but do you need that variable at all?

I would format the date in MyDatePart and DbDatePart

However, you have a variable DBDatePart that isn't being used.
Suggest you debug every variable to determine exactly what is going on
 
I know that I am not using this variable. I was just doing a test.
I am clueless
 
As previously suggested add debug.print for each line of code and make sure you understand what each line does
 

Users who are viewing this thread

Back
Top Bottom