Here is my script:
Dim s As New notessession Dim db As notesdatabase Dim view As NotesView Dim item As NotesItem Dim doc As NotesDocument Dim nbcol As Integer Dim newline As String Dim fileName As String Dim fileNum As Integer Dim dateTime As New NotesDateTime("" ) dateTime.LSLocalTime = Now Dim dateTime1 As NotesDateTime Dim adocCollection As NotesDocumentCollection Dim username As String Set db = s.CurrentDatabase Set view = db.GetView("ExportPayCor") view.AutoUpdate = False 'need to create a date for export Dim chdt As String chdt = dateTime.DateOnly Set doc = view.GetFirstDocument 'this is to grab the date from the timesheet and all other dates for PTO Exempt Set item = doc.GetFirstItem("Date1") Set dateTime1 = item.DateTimeValue Dim date1value As Variant date1value = doc.Date1(0) Dim date2value As Variant date2value = doc.Date2(0) Dim date3value As Variant date3value = doc.Date3(0) Dim date4value As Variant date4value = doc.Date4(0) Dim date5value As Variant date5value = doc.Date5(0) Dim date6value As Variant date6value = doc.Date_1(0) Dim date7value As Variant date7value = doc.Date_2(0) Dim date8value As Variant date8value = doc.Date_3(0) Dim date9value As Variant date9value = doc.Date_4(0) Dim date10value As Variant date10value = doc.Date_5(0) fileName2 = Inputbox$("Enter your file name") fileName = "K:ShareCindy" + fileName2 + ".txt" fileNum = Freefile() Open fileName For Output As fileNum Print #fileNum, "H"; "PAYCOR01" ;chdt;"DominoDB ";"N";doc.DepDate(0) While Not (doc Is Nothing) 'good this works 8/29/06 If doc.Dept(0) = "Info Technology" Then If doc.FWPagerM(0) > 0 Or doc.NWPagerM(0) > 0 Then Print #fileNum, "D"; "000" + doc.EmpNo(0); " "; doc.TTReg(0);" ";" ";" ";" ";" ";" ";" ";" ";" ";"1";"BW" End If Else Print #fileNum, "D"; "000" + doc.EmpNo(0); " "; doc.TTReg(0);" ";" ";" ";" ";" ";" ";" ";" ";" ";" ";"BW" End If If doc.TPTO(0) > 0 Then Print #fileNum, "D";"000" + doc.EmpNo(0);" ";doc.TTPTO(0) + "PTO" End If If doc.TOverTime(0) > 0 Then Print #fileNum, "D";"000" + doc.EmpNo(0);" ";doc.TTOverTime(0) + "OT" End If If doc.TFloat(0) > 0 Then Print #fileNum, "D";"000" + doc.EmpNo(0);" ";doc.TTFloat(0) + "Float" End If If doc.THoliday(0) > 0 Then Print #fileNum, "D";"000" + doc.EmpNo(0);" ";doc.TTHoliday(0) + "Hol" End If If doc.TJury(0) > 0 Then Print #fileNum, "D";"000" + doc.EmpNo(0);" ";doc.TTJury(0) + "Jury" End If If doc.TOtherPd(0) > 0 Then Print #fileNum, "D";"000" + doc.EmpNo(0);" ";doc.TTOtherPd(0) + "OPD" End If If doc.TDoubleTime(0) > 0 Then Print #fileNum, "D";"000" + doc.EmpNo(0);" ";doc.TTOtherPd(0) + "OPD" End If
I'm not 100% clear on all the details of what your code is doing, but it looks like essentially, you are writing a report that a view cannot provide and you want to create totals.
What you want to do is place your "Print' statement in a subroutine. And for this to work, the file you are writing to will have to be available across calls to the subroutine. Now the most obvious ways to do this are by using a global variable or by passing the file to the subroutine. Now you may be asking, "How can I pass an open file to a subroutine?" Well you can't. But you CAN pass a stream.
As you probably know, global variable are BAD and you should almost never use them. However, global variables are useful for the kinds of data that, in object-oriented programming, would be a class property. LotusScript supports object-oriented programming sort of, but its support is so minimal that almost no one uses it. Generally, what you do instead is create a Script Library to act like a class (or more like a Unit in Pascal). Here is how to create on to add your totals.
- Create a new script library. Name it "Reporter."
- In the "Options" section of the library, comment out the "Option Public." It is bad programming style to publicly expose code by default (in any language). As is so often this case in Notes, you should ALWAYS turn off this default and instead explicitly declare subroutines as public when they need to be public.
- In the declarations section of the library, add the following code to define a stream and the counters needed for your totals:
Dim session As NotesSession Dim stream As NotesStream Dim totalPTO As Integer ' . . . other counters go here . . .
- In the library's Initialize event, add the code "Set Session = New NotesSession." The initialize event will take the place of an object constructor.
- Now create a subroutine called "BeginReport." Since we cannot pass anything to the initialize event (like we can in a real object constructor in most languages) we need our own routine to initialize our "class"):
Public Sub BeginReport(ByVal pathName As String) stream = session.CreateStream Call stream.Open(pathname, "ASCII") totalPTO=0 End Sub
- Now add another subroutine to write each line:
Private Sub WriteEntry(ByVal empNo As String, ByVal cat As String, ByVal amt As Double) ' Since you were using Print #, instead of "Wrtie #" 'no delimiters are written to the file but you can add 'commas and tabs chr(9) or whatever you may need. 'You can also break the call up into multiple calls as Call stream.WriteText("D" + "000" + empNo + " ", EOL_NONE) Call stream.WriteText(Format(amt, "$99,990.00") + cat, EOL_CRLF) Select Case category Case "PTO" totalPTO = totalPOT + amount . . . End Select End Sub
- And finally, add one more subroutine to close the stream.
Now, I've obviously made this a little more complex than it has to be so I can illustrate how to set up a "unit" in a LotusScript Library. But using it is simple.
- In the "Options" section of your agent, assign a Use "Reporter" to reference the script library.
- Instead of opening a file, call BeginReport.
- Instead of printing to the file, call WriteEntry.
And in practice, you will no doubt want to add one more routine to the library to print or otherwise return the totals, but I'll leave that as an exercise. You can also use a LotusScript list for a more generic set of counters without having to manually create each counter and use a select case as I have done here. If I have a chance I'll talk about lists in an up coming Blog.
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.