Forms are not optimized for printing. That means that subforms are truncated and form fields don't expand as they do with reports. On top of that, forms are generally colored and that wastes ink when you print.
Create a report and use the:
Docmd.OpenReport method to open the report. One of the arguments is a filter so you can pass the form's filter into the report to filter it.
Private Sub Command954_Click()
Me.Filter = buildWhereClause
Me.FilterOn = True
End Sub
Private Function buildWhereClause()
buildWhereClause = ""
' Has the field got something in it?
If Len(Me.Tcode & vbNullString) > 0 Then
' If there is already something in the WHERE clause then add an AND
If Len(buildWhereClause & vbNullString) > 0 Then buildWhereClause = buildWhereClause & " AND "
' Using single quotes rather than double quotes can make life easier
' when dealing with strings in SQL
buildWhereClause = buildWhereClause & "([Transport code] = '" & Me.Tcode & "')"
' buildWhereClause = buildWhereClause & "([Transport code] = """ & Me.Tcode & """)"
End If
If Len(Me.Tcode & vbNullString) > 0 Then
If Len(buildWhereClause & vbNullString) > 0 Then buildWhereClause = buildWhereClause & " AND "
buildWhereClause = buildWhereClause & "([Transport code] = '" & Me.Tcode & "')"
End If
' year
' Has the field got something in it?
If Len(Me.Ycode & vbNullString) > 0 Then
' If there is already something in the WHERE clause then add an AND
If Len(buildWhereClause & vbNullString) > 0 Then buildWhereClause = buildWhereClause & " AND "
' Using single quotes rather than double quotes can make life easier
' when dealing with strings in SQL
buildWhereClause = buildWhereClause & "([years] = '" & Me.Ycode & "')"
End If
If Len(Me.Ycode & vbNullString) > 0 Then
If Len(buildWhereClause & vbNullString) > 0 Then buildWhereClause = buildWhereClause & " AND "
buildWhereClause = buildWhereClause & "([years] = '" & Me.Ycode & "')"
End If
'Month
' Has the field got something in it?
If Len(Me.Mcode & vbNullString) > 0 Then
' If there is already something in the WHERE clause then add an AND
If Len(buildWhereClause & vbNullString) > 0 Then buildWhereClause = buildWhereClause & " AND "
' Using single quotes rather than double quotes can make life easier
' when dealing with strings in SQL
buildWhereClause = buildWhereClause & "([Months] = '" & Me.Mcode & "')"
End If
If Len(Me.Mcode & vbNullString) > 0 Then
If Len(buildWhereClause & vbNullString) > 0 Then buildWhereClause = buildWhereClause & " AND "
buildWhereClause = buildWhereClause & "([Months] = '" & Me.Mcode & "')"
End If
'POScode
' Has the field got something in it?
If Len(Me.Pcode & vbNullString) > 0 Then
' If there is already something in the WHERE clause then add an AND
If Len(buildWhereClause & vbNullString) > 0 Then buildWhereClause = buildWhereClause & " AND "
' Using single quotes rather than double quotes can make life easier
' when dealing with strings in SQL
buildWhereClause = buildWhereClause & "([POScode] = '" & Me.Pcode & "')"
End If
If Len(Me.Pcode & vbNullString) > 0 Then
If Len(buildWhereClause & vbNullString) > 0 Then buildWhereClause = buildWhereClause & " AND "
buildWhereClause = buildWhereClause & "([POScode] = '" & Me.Pcode & "')"
End If
'subPOScode
' Has the field got something in it?
If Len(Me.Scode & vbNullString) > 0 Then
' If there is already something in the WHERE clause then add an AND
If Len(buildWhereClause & vbNullString) > 0 Then buildWhereClause = buildWhereClause & " AND "
' Using single quotes rather than double quotes can make life easier
' when dealing with strings in SQL
buildWhereClause = buildWhereClause & "([SubPOScode] = '" & Me.Scode & "')"
End If
If Len(Me.Scode & vbNullString) > 0 Then
If Len(buildWhereClause & vbNullString) > 0 Then buildWhereClause = buildWhereClause & " AND "
buildWhereClause = buildWhereClause & "([SubPOScode] = '" & Me.Scode & "')"
End If
End Function
after I created a button again and the code is here: