N
niuginikiwi
I have a combo box and two list boxes with values plus two text boxes for a
Date Range that will supply criteria for a report that is based on a select
query.
It worked fine with the combo and the list boxes until i added in the Date
Range bit. It says Compile Error. Object Required and from there I'm lost.
Can someone look through the code below and let me know where I am goin
wrong? I don't have much programming experience. Thanks.
Private Sub btnEdit_Click()
Dim cbo As ComboBox
Dim lst As ListBox
Dim strFromDate As Date 'Name of criteria start date field.
Dim strToDate As Date 'Name of criteria end date field.
Const conDateFormat = "\#mm\/dd\/yyyy\#"
Dim strWhere As String
Dim iLen As Integer
'CropName Combo
Set cbo = Me.cboCrop
If Not IsNull(cbo) Then
strWhere = strWhere & "(CropName = """ & cbo.Column(1) & """) AND "
End If
'GrowerName List
Set lst = Me.lstGrower
If Not IsNull(lst) Then
strWhere = strWhere & "(GrowerName = """ & lst.Column(1) & """) AND "
End If
'VarietyName List
Set lst = Me.lstVariety
If Not IsNull(lst) Then
strWhere = strWhere & "(VarietyName = """ & lst.Column(1) & """) AND "
End If
Set strFromDate = Me.txtStartDate
Set strToDate = Me.txtEndDate
If IsNull(strFromDate) Then
If Not IsNull(strToDate) Then 'End date, but no start.
strWhere = strWhere & "(DateSown <= " & Format(strToDate,
conDateFormat) & """)OR"
End If
Else
If IsNull(strToDate) Then 'Start date, but no End.
strWhere = strWhere & "(DateSown >= " & Format(strFromDate,
conDateFormat) & """)OR"
Else 'Both start and end dates.
strWhere = strWhere & "(DateSown Is Between " &
Format(strFromDate, conDateFormat) _
& " And " & Format(strToDate, conDateFormat) & """)AND"
End If
End If
'Similar for other list, combo or text boxes here.
'Did we get anything?
iLen = Len(strWhere) - 5 'Without trailing " AND ".
If iLen < 1 Then
MsgBox "Opps! Must supply at least one criteria"
Else
strWhere = Left$(strWhere, iLen)
'Use it for this form.
Me.Filter = strWhere
Me.FilterOn = True
'Open the report and apply filter.
DoCmd.OpenReport "rptSowing", acViewPreview, , strWhere
End If
Set cbo = Nothing
Set lst = Nothing
Me.lstVariety = Null
Me.lstGrower = Null
End Sub
Date Range that will supply criteria for a report that is based on a select
query.
It worked fine with the combo and the list boxes until i added in the Date
Range bit. It says Compile Error. Object Required and from there I'm lost.
Can someone look through the code below and let me know where I am goin
wrong? I don't have much programming experience. Thanks.
Private Sub btnEdit_Click()
Dim cbo As ComboBox
Dim lst As ListBox
Dim strFromDate As Date 'Name of criteria start date field.
Dim strToDate As Date 'Name of criteria end date field.
Const conDateFormat = "\#mm\/dd\/yyyy\#"
Dim strWhere As String
Dim iLen As Integer
'CropName Combo
Set cbo = Me.cboCrop
If Not IsNull(cbo) Then
strWhere = strWhere & "(CropName = """ & cbo.Column(1) & """) AND "
End If
'GrowerName List
Set lst = Me.lstGrower
If Not IsNull(lst) Then
strWhere = strWhere & "(GrowerName = """ & lst.Column(1) & """) AND "
End If
'VarietyName List
Set lst = Me.lstVariety
If Not IsNull(lst) Then
strWhere = strWhere & "(VarietyName = """ & lst.Column(1) & """) AND "
End If
Set strFromDate = Me.txtStartDate
Set strToDate = Me.txtEndDate
If IsNull(strFromDate) Then
If Not IsNull(strToDate) Then 'End date, but no start.
strWhere = strWhere & "(DateSown <= " & Format(strToDate,
conDateFormat) & """)OR"
End If
Else
If IsNull(strToDate) Then 'Start date, but no End.
strWhere = strWhere & "(DateSown >= " & Format(strFromDate,
conDateFormat) & """)OR"
Else 'Both start and end dates.
strWhere = strWhere & "(DateSown Is Between " &
Format(strFromDate, conDateFormat) _
& " And " & Format(strToDate, conDateFormat) & """)AND"
End If
End If
'Similar for other list, combo or text boxes here.
'Did we get anything?
iLen = Len(strWhere) - 5 'Without trailing " AND ".
If iLen < 1 Then
MsgBox "Opps! Must supply at least one criteria"
Else
strWhere = Left$(strWhere, iLen)
'Use it for this form.
Me.Filter = strWhere
Me.FilterOn = True
'Open the report and apply filter.
DoCmd.OpenReport "rptSowing", acViewPreview, , strWhere
End If
Set cbo = Nothing
Set lst = Nothing
Me.lstVariety = Null
Me.lstGrower = Null
End Sub