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