Command Button on Form

G

Guest

Hi

I have another question

I have a from which uses a command button to load a report based upon the
catergery selected in a combo box. i want to add two things

1) If no catergory is selected it loads the report showing all catergorys
(Currently shows a message saying you must select)

2) I want to add a 2 text boxs to this form where the use can enter a date
range (Start Date and End Date)

So in all i need the command button to look t the combo box and the other
two text boxes. Is this possible.

I have inserted the current Event procedure for the Command Button as follows:

Private Sub Command7_Click()
If IsNull(Me.Combo8) Then
MsgBox "CHOOSE CATERGORY"
Else: DoCmd.OpenReport "Category", acPreview,
WHERECONDITION:="Catergory= '" & Me.Combo8 & "'"
End If

End Sub

Thanks
 
A

Allen Browne

If the category is null, just don't create a WhereCondition string:

Private Sub Command7_Click()
Dim strWhere As String
If Not IsNull(Me.Combo8) Then
strWhere = "Catergory= """ & Me.Combo8 & """"
End If
DoCmd.OpenReport "Category", acViewPreview, , strWhere
End Sub

Regarding the dates, see:
Limiting a Report to a Date Range
at:
http://allenbrowne.com/casu-08.html

You can combine the 2 sets of conditions (dates and category) by adding an
AND between them.
 
G

Guest

Thanks Allen

great Website steps was easy to follow

Got them both to work off individual command buttons.

Is it possible to combine the code so that they work off one command button.
here is the two sets of codes as it stands at the moment

Private Sub OK_Click()

Dim strReport As String 'Name of report to open.
Dim strField As String 'Name of your date field.
Dim strWhere As String 'Where condition for OpenReport.
Const conDateFormat = "\#dd\/mm\/yyyy\#"

strReport = "category"
strField = "pdate"

If IsNull(Me.txtStartDate) Then
If Not IsNull(Me.txtEndDate) Then 'End date, but no start.
strWhere = strField & " < " & Format(Me.txtEndDate, conDateFormat)
End If
Else
If IsNull(Me.txtEndDate) Then 'Start date, but no End.
strWhere = strField & " > " & Format(Me.txtStartDate,
conDateFormat)
Else 'Both start and end dates.
strWhere = strField & " Between " & Format(Me.txtStartDate,
conDateFormat) _
& " And " & Format(Me.txtEndDate, conDateFormat)
End If

End If

' Debug.Print strWhere 'For debugging purposes only.
DoCmd.OpenReport strReport, acViewPreview, , strWhere
End Sub
Private Sub Command7_Click()
Dim strWhere As String
If Not IsNull(Me.Combo8) Then
strWhere = "Catergory= """ & Me.Combo8 & """"
End If
DoCmd.OpenReport "Category", acViewPreview, , strWhere
End Sub

thanks
 
A

Allen Browne

Put this into the Ok button's Click event procedure, after the code that
handles the dates, and before the DoCmd.OpenReport line:

If Not IsNull(Me.Combo8) Then
If strWhere <> "" Then
strWhere = "(" & strWhere & ") AND (Catergory= """ & Me.Combo8 &
""")"
Else
strWhere = "Catergory= """ & Me.Combo8 & """"
End If
End If
 
G

Guest

THANKS ALOT

WORKS GREAT

Allen Browne said:
Put this into the Ok button's Click event procedure, after the code that
handles the dates, and before the DoCmd.OpenReport line:

If Not IsNull(Me.Combo8) Then
If strWhere <> "" Then
strWhere = "(" & strWhere & ") AND (Catergory= """ & Me.Combo8 &
""")"
Else
strWhere = "Catergory= """ & Me.Combo8 & """"
End If
End If
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top