C
cathywoodford
Hi. I'm creating a report based on a company combo box selection and
a date range (2 text box fields). I can't seem to get the report to
do both. I want it so that the combo box selection (company) must be
choosen but the date range is optional. If it's not filled in then it
would return all rows for that company. Here is my code.
Private Sub cmdPrintReport_Click()
On Error GoTo Err_cmdPrintReport_Click
Dim lstrSQL As String
Dim stDocName As String
Dim strDate As String
Dim strField As String 'Name of your date field.
Dim strwhere As String
strField = "tbltransmittal.DateofIssue"
If IsNull(Me.txtStartIssueDate) Then
If Not IsNull(Me.txtEndIssueDate) Then 'End date, but no
start.
strDate = strField & " <= " & Me.txtEndIssueDate
End If
Else
If IsNull(Me.txtEndIssueDate) Then 'Start date, but no
End.
strDate = strField & " >= " & (Me.txtStartIssueDate)
Else 'Both start and end dates.
strDate = strField & " Between " & (Me.txtStartIssueDate)
_
& " And " & (Me.txtEndIssueDate)
End If
End If
If Not IsNull(Me.cboCompanySearch) Or Me.cboCompanySearch <> ""
Then
If lstrSQL <> "" Then
lstrSQL = lstrSQL & " and "
End If
lstrSQL = lstrSQL & " [CompanyName] Like " & """" &
Nz(Me.cboCompanySearch, "*") & strDate & """"
End If
If lstrSQL = "" Then
MsgBox "Please choose a company from the list"
Else
stDocName = "rptReportbyCompany"
DoCmd.OpenReport stDocName, acPreview, , lstrSQL
End If
Exit_cmdPrintReport_Click:
Exit Sub
Err_cmdPrintReport_Click:
MsgBox Err.Description
Resume Exit_cmdPrintReport_Click
End Sub
Hope someone can help.
a date range (2 text box fields). I can't seem to get the report to
do both. I want it so that the combo box selection (company) must be
choosen but the date range is optional. If it's not filled in then it
would return all rows for that company. Here is my code.
Private Sub cmdPrintReport_Click()
On Error GoTo Err_cmdPrintReport_Click
Dim lstrSQL As String
Dim stDocName As String
Dim strDate As String
Dim strField As String 'Name of your date field.
Dim strwhere As String
strField = "tbltransmittal.DateofIssue"
If IsNull(Me.txtStartIssueDate) Then
If Not IsNull(Me.txtEndIssueDate) Then 'End date, but no
start.
strDate = strField & " <= " & Me.txtEndIssueDate
End If
Else
If IsNull(Me.txtEndIssueDate) Then 'Start date, but no
End.
strDate = strField & " >= " & (Me.txtStartIssueDate)
Else 'Both start and end dates.
strDate = strField & " Between " & (Me.txtStartIssueDate)
_
& " And " & (Me.txtEndIssueDate)
End If
End If
If Not IsNull(Me.cboCompanySearch) Or Me.cboCompanySearch <> ""
Then
If lstrSQL <> "" Then
lstrSQL = lstrSQL & " and "
End If
lstrSQL = lstrSQL & " [CompanyName] Like " & """" &
Nz(Me.cboCompanySearch, "*") & strDate & """"
End If
If lstrSQL = "" Then
MsgBox "Please choose a company from the list"
Else
stDocName = "rptReportbyCompany"
DoCmd.OpenReport stDocName, acPreview, , lstrSQL
End If
Exit_cmdPrintReport_Click:
Exit Sub
Err_cmdPrintReport_Click:
MsgBox Err.Description
Resume Exit_cmdPrintReport_Click
End Sub
Hope someone can help.