print a form

shagha

Registered User.
Local time
Yesterday, 18:46
Joined
Jun 19, 2013
Messages
14
how do I print a filtered form?

I mean I have form, I want to print it by it's filtered value


thanks in advance for your help
 
It is unusual to print a form.
Design a report for this.

Any way... the command is:
DoCmd.PrintOut
 
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.
 
Code:
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:

Code:
Me.Filter = buildWhereClause
 Me.FilterOn = True

DoCmd.OpenReport "arrival", acViewNormal, buildWhereClause, , acWindowNormal

it doesn't work!
 

Users who are viewing this thread

Back
Top Bottom