Results 1 to 7 of 7
  1. #1
    killdozer is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jan 2014
    Location
    New Brunswick
    Posts
    9

    Using a Date Filter to ref a field on a sub form

    I have an unbound form "frmFY" with a Startdate and End Date txtboxes, and a command button that opens the form in Data Sheet view and it show the filtered data according to the dates from the txtboxes. So far so good. I want this form to be a subform on an unbound form to allow me a place to put totals and commnad buttons to print or export to excel the data in the subform. As of now my Main form is called "frmFY_Master" and the Sub form is called "frmFYMain".
    I can't figure out how to reference the date field in the subform in the script. I included the script which was originally for a report.



    Code:
    'On Error GoTo Err_Handler      'Remove the single quote from start of this line once you have it working.
        'Purpose:       Filter a report to a date range.
        'Documentation: http://allenbrowne.com/casu-08.html
        'Note:          Filter uses "less than the next day" in case the field has a time component.
        Dim strForm As String
        Dim strDateField As String
        Dim strWhere As String
        Dim lngView As Long
        Const strcJetDate = "\#mm\/dd\/yyyy\#"  'Do NOT change it to match your local settings.
        
        'DO set the values in the next 3 lines.
        strForm = "frmFYMain"      'Put your report name in these quotes.
        strDateField = "[WorkOrderDate]" 'Put your field name in the square brackets in these quotes.
        lngView = acFormDS    'Use acViewNormal to print instead of preview.
        
        'Build the filter string.
        If IsDate(Me.txtStartDate) Then
            strWhere = "(" & strDateField & " >= " & Format(Me.txtStartDate, strcJetDate) & ")"
        End If
        If IsDate(Me.txtEndDate) Then
            If strWhere <> vbNullString Then
                strWhere = strWhere & " AND "
            End If
            strWhere = strWhere & "(" & strDateField & " < " & Format(Me.txtEndDate + 1, strcJetDate) & ")"
        End If
        
        'Close the report if already open: otherwise it won't filter properly.
        If CurrentProject.AllForms(strForm).IsLoaded Then
            DoCmd.Close acForm, strForm
        End If
        
        'Open the report.
        'Debug.Print strWhere        'Remove the single quote from the start of this line for debugging purposes.
        DoCmd.OpenForm strForm, lngView, , strWhere
         
    Exit_Handler:
        Exit Sub
    Err_Handler:
        If Err.Number <> 2501 Then
            MsgBox "Error " & Err.Number & ": " & Err.Description, vbExclamation, "Cannot open Form"
        End If
        Resume Exit_Handler
    FilterScript.txt

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922

  3. #3
    burrina's Avatar
    burrina is offline VIP
    Windows 8 Access 2010 32bit
    Join Date
    Oct 2012
    Location
    Freeport,Texas
    Posts
    1,383
    I am confused by your post. Are you saying that you want a second subform on your main form and it is frmFY ? BY reference what do you mean, refer to a date field on that form?

  4. #4
    killdozer is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jan 2014
    Location
    New Brunswick
    Posts
    9
    The form "frmFY" is an unbound form with the two txt boxes for the start and end date and a command button to open the form "frmFY_Master" and to apply the filter to the sub form"frmFTMain" in datasheet view. Yes I mean to refer to the date field on the subform "frmFYMain".

  5. #5
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2013
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,922
    The link I provided shows you the syntax for such a reference.

  6. #6
    killdozer is offline Novice
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jan 2014
    Location
    New Brunswick
    Posts
    9
    I think I tried every combination from that page. As the code is on a separate form to filter the data of a sub form when opening the main form. I figured that under Not in these forms and the example "Forms!Mainform!Subform1.Form.RecordSource" would work but the data in the subform is still not being filtered.

  7. #7
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,902
    Think I would not use subform. Consider single bound form with the unbound input date controls in form header. Set the form as Continuous view and arrange controls to look like Datasheet.

    However, forms do not print nice. Reports are the intended vehicle for printing.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 05-22-2014, 06:11 PM
  2. VBA on split form for start and end date filter
    By data808 in forum Programming
    Replies: 1
    Last Post: 02-03-2014, 02:42 AM
  3. Replies: 3
    Last Post: 05-02-2013, 01:59 PM
  4. Filter form records by Date with Combo box
    By jhrBanker in forum Forms
    Replies: 2
    Last Post: 07-31-2012, 01:19 PM
  5. Multiple Field & date range filter
    By mrkandohi001 in forum Reports
    Replies: 6
    Last Post: 01-18-2012, 03:11 AM

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