marvelousme
Registered User.
- Local time
- Today, 13:36
- Joined
- Oct 23, 2014
- Messages
- 25
I have created a search form using VBA that works perfectly. I want to be able to take that same filtered data and use it for a report. I'm having trouble getting my report to open with the filtered results from my form. My search form has a command button containing this code:
Private Sub cmdFilter_Click()
On Error GoTo errr
Me.subProductivtyReport.Form.RecordSource = "Select * From qryProductivty " & BuildFilter
Me.subProductivtyReport.Requery
Exit Sub
errr:
MsgBox Err.Description
End Sub
Private Function BuildFilter() As Variant
Dim varWhere As Variant
Dim tmp As String
tmp = """"
Const conJetDate = "\#mm\/dd\/yyyy\#"
varWhere = Null
If Me.txtFrom > "" Then
varWhere = varWhere & "([date] >= " & Format(Me.txtFrom, conJetDate) & ") AND "
End If
If Me.txtTo > "" Then
varWhere = varWhere & "([date] <= " & Format(Me.txtTo, conJetDate) & ") AND "
End If
If Me.cboStatus > "" Then
varWhere = varWhere & "[EmpType] like '" & Me.cboStatus & "' AND "
End If
If cboReviewer > "" Then
varWhere = varWhere & "[CreatedBy] like '" & Me.cboReviewer & "' AND "
End If
If cboVendor > "" Then
varWhere = varWhere & "[Vendor] like '" & Me.cboVendor & "' AND "
End If
If cboRole > "" Then
varWhere = varWhere & "[role] like '" & Me.cboRole & "' AND "
End If
If IsNull(varWhere) Then
varWhere = ""
Else
varWhere = "WHERE " & varWhere
If Right(varWhere, 5) = " AND " Then
varWhere = Left(varWhere, Len(varWhere) - 5)
End If
End If
BuildFilter = varWhere
End Function
I attempted to add a command button to open the report in print preview:
DoCmd.OpenReport "qryProductivty", acViewPreview",, tmp
But it just opens the report unfiltered. What am I doing wrong?
Private Sub cmdFilter_Click()
On Error GoTo errr
Me.subProductivtyReport.Form.RecordSource = "Select * From qryProductivty " & BuildFilter
Me.subProductivtyReport.Requery
Exit Sub
errr:
MsgBox Err.Description
End Sub
Private Function BuildFilter() As Variant
Dim varWhere As Variant
Dim tmp As String
tmp = """"
Const conJetDate = "\#mm\/dd\/yyyy\#"
varWhere = Null
If Me.txtFrom > "" Then
varWhere = varWhere & "([date] >= " & Format(Me.txtFrom, conJetDate) & ") AND "
End If
If Me.txtTo > "" Then
varWhere = varWhere & "([date] <= " & Format(Me.txtTo, conJetDate) & ") AND "
End If
If Me.cboStatus > "" Then
varWhere = varWhere & "[EmpType] like '" & Me.cboStatus & "' AND "
End If
If cboReviewer > "" Then
varWhere = varWhere & "[CreatedBy] like '" & Me.cboReviewer & "' AND "
End If
If cboVendor > "" Then
varWhere = varWhere & "[Vendor] like '" & Me.cboVendor & "' AND "
End If
If cboRole > "" Then
varWhere = varWhere & "[role] like '" & Me.cboRole & "' AND "
End If
If IsNull(varWhere) Then
varWhere = ""
Else
varWhere = "WHERE " & varWhere
If Right(varWhere, 5) = " AND " Then
varWhere = Left(varWhere, Len(varWhere) - 5)
End If
End If
BuildFilter = varWhere
End Function
I attempted to add a command button to open the report in print preview:
DoCmd.OpenReport "qryProductivty", acViewPreview",, tmp
But it just opens the report unfiltered. What am I doing wrong?