Page 1 of 2 12 LastLast
Results 1 to 15 of 21
  1. #1
    Susy is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    134

    VBA Date will not be read. Date parameter as Date defined, still no progress!

    Hi there,

    I am having really problems with one piece of code... The other two filters are working fine, but the date filter is not working.



    In the main table, the ActionDate field is defined as Date/Time (Short Date) as well as in the form. Still I am not able to make it run....

    I get the following error message at this line:
    Code:
    Me.subfrmReport.Form.RecordSource = task
    Click image for larger version. 

Name:	error message.JPG 
Views:	16 
Size:	19.3 KB 
ID:	28241


    This is what the form looks like:

    Click image for larger version. 

Name:	ActionDate.JPG 
Views:	15 
Size:	30.2 KB 
ID:	28240
    I used the idea and code from austin72406 https://www.youtube.com/watch?v=choPri7y_o4


    Code:
    Function SearchCriteria()
    
    
    Dim StatusName, TypeID, ActionDate As String
    Dim task, strCriteria As String
    
    
        If IsNull(Me.cboStatus) Then
            StatusName = "[StatusName] like '*'"
        Else
            StatusName = "[StatusName] = '" & Me.cboStatus & "'"
        End If
    
    
        If IsNull(Me.cboType) Then
        TypeID = "[TypeID] like '*'"
            
        Else
            TypeID = "[TypeID] = " & Me.cboType
        End If
        
        
         If IsNull(Me.cboActionDate) Then
            ActionDate = "[ActionDate] like '*'"
        Else
            ActionDate = "[ActionDate] = #" & Me.cboActionDate & "#"
        End If
        
        
        
        
        strCriteria = StatusName & " AND " & TypeID & " AND " & ActionDate
            task = "SELECT * FROM tblReport WHERE " & strCriteria
            Me.subfrmReport.Form.RecordSource = task
            Me.subfrmReport.Form.Requery
        
    
    
    End Function

    Can someone see what I am doing wrong.... I don't know what to do.

    Regards

  2. #2
    ranman256's Avatar
    ranman256 is offline VIP
    Windows Vista Access 2010 32bit
    Join Date
    Apr 2014
    Location
    Kentucky
    Posts
    9,525
    If the field is null,don't put it in the SQL.
    the error box is wrong because the date has periods, and no closing #.

  3. #3
    Susy is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    134
    but... what would be the solution?

    Code:
    ActionDate = "[ActionDate] = #" & Me.cboActionDate & "#"

  4. #4
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    My initial advice would be to change the variables to

    strStatusName
    strTypeID
    strActionDate

    to confirm that variables weren't being mixed with fields
    Last edited by andy49; 04-13-2017 at 06:11 AM. Reason: ignore - missed the more obvious

  5. #5
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    when requiring sql to convert a string to a date using the # chars, the date needs to be presented in US format. So try

    ActionDate = "[ActionDate] = #" & format(Me.cboActionDate,"mm/dd/yyyy") & "#"

    Also generally not a good idea to have variables with the same name as fields/controls -it can confuse access


    And declaring variables like this

    Dim StatusName, TypeID, ActionDate As String
    Dim task, strCriteria As String


    Only ActionDate and strCriteria are typed as string, the others will be typed as variant - and variants can be slow because time is required for the compiler to determine what type the variant is.

    declare as

    Dim StatusName As String, TypeID As String, ActionDate As String
    Dim task As String, strCriteria As String


    or

    Dim StatusName As String
    dim TypeID As String
    Dim ActionDate As String
    Dim task As String
    dim strCriteria As String

  6. #6
    Susy is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    134
    Thanks, I did as recommended but... the same errorClick image for larger version. 

Name:	new_ActionDate.JPG 
Views:	15 
Size:	23.5 KB 
ID:	28242

    the new code:
    Click image for larger version. 

Name:	newCode.JPG 
Views:	15 
Size:	68.0 KB 
ID:	28243

    I also wrote...
    Code:
    strActDate = "[ActionDate] = #" & Format(Me.cboActionDate, "dd/mm/yyyy") & "#"
    but same error but with month and day replaced...

  7. #7
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    is me.cboactiondate returning a date value or a text version of the date?

  8. #8
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    And I'd add the line

    msgbox stractiondate


    after the line it's defined in to check whether the string actually has picked up the final #

  9. #9
    Susy is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    134
    here some snapshots...

    Click image for larger version. 

Name:	expression1.JPG 
Views:	14 
Size:	14.7 KB 
ID:	28244Click image for larger version. 

Name:	expression2.JPG 
Views:	15 
Size:	46.1 KB 
ID:	28245Click image for larger version. 

Name:	cboActionDate Format.JPG 
Views:	14 
Size:	22.1 KB 
ID:	28246


    I renamed the variables... the change the format of the date... ???

    Click image for larger version. 

Name:	task.JPG 
Views:	14 
Size:	32.3 KB 
ID:	28247

  10. #10
    CJ_London is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    Mar 2015
    Posts
    11,430
    what I don't see is you say you have used the format function - so should be 04/12/2017 but it is displaying 04.12.2017

  11. #11
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    try


    ActionDate = "[ActionDate] = #" & format(Me.cboActionDate,"mm\/dd\/yyyy") & "#"

    the v shape is \ followed by /

  12. #12
    Susy is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    134
    Thanks andy49 and everyone....

    It works like a charm.... you made my day!

    In case... Happy Easter!

  13. #13
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    No problem

    The short date separators are defined within local settings on your PC

    Not even the Format (date) function can change them into /

    So the \ forces the computer to use the next value (ie /)

    All hale AllenBrowne!!

  14. #14
    Susy is offline Competent Performer
    Windows 7 64bit Access 2007
    Join Date
    Sep 2014
    Posts
    134
    Thanks again and thanks for the information

  15. #15
    andy49's Avatar
    andy49 is offline VIP
    Windows 10 Access 2007
    Join Date
    Nov 2016
    Location
    London
    Posts
    1,051
    FYI Susy

    That website has a nice function will might prove useful.

    If local settings are indeed set as . for dates then this wont be the last time you hit this issue.

    And Happy Easter

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 4
    Last Post: 11-20-2018, 11:57 PM
  2. Replies: 5
    Last Post: 09-02-2015, 11:39 AM
  3. Replies: 3
    Last Post: 08-21-2012, 03:05 PM
  4. Parameter Date - Force a Monday Date
    By ker9 in forum Queries
    Replies: 3
    Last Post: 07-26-2011, 01:06 PM
  5. Replies: 1
    Last Post: 07-07-2010, 04:22 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums