Printing Reports based on a View
This tip describes the procedure for printing Reports based on a View.
The following code will print a columnar report based on a view/folder with little coding required. This report is limited in that I have not include the code to make it a continuos multi page report. It can be done in 6 lines of code. I have left hints in my comments enjoy?. G.A.Nott ? nottg@sympatico.ca
The view/folder should have as many columns as on your report. The name of the folder/view assigned it to the Const ReportName. Add the field names of the data for the report.
Create a subForm with a table and ALL fields as RICHTEXT computed fields. The fieldnames should be for row one A1, A2?, A8 the next row B1, B2?, B8 and so on.
Create a form and insert the subform. Name it and assign it to the Const FormName.
Create a view with one column. Select one field on your form with a view selection formula. SELECT(Form = "FormName"), save it. Assign it to the Const ViewForm.
The last you should already have in your application.
View where we can find the search key. It must be in the first column. Assign it to the Const SearchView. i.e. @Text( Date; "S0") + @UserName + SomeKeyField ...
Add a button to the form put the option in options and the Declarations in? and that's it.
Option Base 1 Dim s As NotesSession Dim db As NotesDatabase Dim doc As NotesDocument Dim dc As NotesDocumentCollection Dim collection As NotesDocumentCollection Dim view As NotesView Dim w As NotesUIWorkspace Dim uidoc As NotesUIDocument Dim uiview As NotesUIView Dim form As NotesForm Dim Item As NotesItem Dim rtItem As NotesRichTextItem Dim richStyle As NotesRichTextStyle Dim column As NotesViewColumn Dim TheSearchKey As Variant Sub Click(Source As Button) ' set the environment Set s = New NotesSession Set w = New NotesUIWorkspace Set db = s.CurrentDatabase ? Fill these in? that?s it Const ReportName = "" Const FormName = "" Const ViewForm = "" Const SearchView = "" ' name of the view where the search key can be found Set view = db.GetView( SearchView ) Set column = view.Columns( 0 ) Set doc = s.DocumentContext ' set your printing Fonts and Styles Set richStyle = s.CreateRichTextStyle richStyle.NotesFont = FONT_ARIAL richStyle.FontSize = 7 ' get the formula from the view and set the document collection to that key exact match only Set dc = view.GetAllDocumentsByKey( Evaluate( column.Formula , doc), True) ' we have a match If dc.count > 0 Then ' copy the document collection to the report folder Call dc.PutAllInFolder( ReportName ) ' set the view to the view folder name Set view = db.GetView( ReportName ) ' get the field names for the report from the column information aColumns = view.Columns ' create the arrays to print the records Redim Preserve aPrint( 1 To dc.Count , 1 To Ubound( aColumns ) + 1 ) ' the records to print ' step thru the document collection For i = 1 To dc.Count ' step through each document Set doc = view.GetNthDocument( i ) ' get the items to print For k = Lbound( aColumns ) To Ubound( aColumns ) ' set the notes item class name Set Item = doc.GetFirstItem( aColumns( k ).ItemName ) ' add the item(s) Values to the print array we could also get the text or dateTime value aPrint( i , k + 1 ) = item.Values Next Next ' we have the records to print... Remove All the records from the Folder Call dc.RemoveAllFromFolder( ReportName ) ' get the print form template for the report in this view Set view = db.GetView( ViewForm ) ' name of the view where we saved the empty form Set doc = view.GetFirstDocument ' get the document ' set doc to the first doc in the view Set doc = New NotesDocument ( db ) i = 0 ' counter A = 65 ' start with the letter A on the form B = A + dc.Count - 1 ' stop at this letter ' Names of the fields on the form For letterCode& = Asc(Chr$(A) ) To Asc(Chr$(B)) i = i + 1 ' up the counter ' set each item on the form For k = 1 To Ubound( aColumns ) + 1 ' set the RICHTEXT item A1_1 , A2_1... A1_7 , A2_7 Set rtitem = New NotesRichTextItem( doc, Chr$(letterCode&) + Trim(Str(k)) ) ' add our font and styles to the fields Call rtitem.AppendStyle(richStyle) ' get the item values in each element of the print array aLists = aPrint( i , k ) ' all fields should be RICHTEXT If ( rtitem.Type = RICHTEXT ) Then For x = Lbound( aLists ) To Ubound( aLists ) ' Append EACH ITEM to the RICHTEXT field Call rtitem.AppendText(Trim( aLists( x ))) ' don't insert a new line if it is the last element in array If x < Ubound( aLists ) Then Call rtitem.AddNewLine( 1 ) End If Next End If Next ' name of the print form Call doc.ReplaceItemValue( "Form", FormName ) Next ' save the records Call doc.Save( True, True ) ' Show the user the results Call w.EditDocument( False, doc, True ) Set uidoc = w.CurrentDocument ' Print the document Call uidoc.Print ' Close and Remove the print form document Call uidoc.Close Call doc.Remove( True ) Else Msgbox "Please select a record below the category",, "Category Selected" Exit Sub End If End Sub
Start the conversation
0 comments