Hi there I'm not a developer and so am unaware of best practices. I created this to bypass manual data copying of log data. This agent is for a single feed which I'll copy and adjust for each additional one. For the specified feed it reads the log for the last time processed and number of files processed so far today and for yesterday. It also counts files in the input folder and reads the server's timezone. Each data item is separated by a comma for csv and emailed, which later is hosted on a website. Thanks for any constructive criticism.
Sub Initialize
Dim customername As String
Dim servername As String
Dim feedname As String
Dim alertthresholdinhours As Integer
Dim inputfeedpath As String
' Set for each feed
customername = "gRrhio"
servername = "gRrhioEdge2"
feedname = "FF Thompson ADT"
alertthresholdinhours = 6
inputfeedpath = "\\mhinec\elycon\data\adt\*.*"
' Counts files in input folder
Dim pathName As String, fileName As String
Dim inputfeedcounter As Integer
inputfeedcounter = 0
pathName$ = inputfeedpath
fileName$ = Dir$(pathName$, 0)
Do While fileName$ <> ""
inputfeedcounter = inputfeedcounter + 1
fileName$ = Dir$()
Loop
Dim entry As NotesViewEntry
Dim vc As NotesViewEntryCollection
Dim filesprocessed As Integer
Dim session As New NotesSession
Dim db As NotesDatabase
Dim newDoc As NotesDocument
Dim rtitem As NotesRichTextItem
Set db = session.CurrentDatabase
Dim view As NotesView
Set view = db.GetView( "Sessions\by Feed" )
Set newDoc = New NotesDocument( db )
Set rtitem = New NotesRichTextItem( newDoc, "Body" )
Dim todaysdate As New NotesDateTime("Today")
Dim flag As Integer
Dim counter As Integer
Dim files As Integer
Dim errors As Integer
Dim lastdate As String
Dim lastdayran As String
Dim lasttime As String
Dim lasttimeran As String
Dim filesp As Integer
Dim lastdayfiles As Integer
Dim lastdaysfiles2 As Integer
Dim terrors As Integer
Dim lastdayerrors As Integer
lastdate = ""
lastdayran = ""
counter = 0
flag = 0
filesp = 0
lastdayfiles = 0
lastdaysfiles2 = 0
terrors = 0
lastdayerrors = 0
' Finds date for last time processed, counts files processed and errors
While flag = 0
Dim dateTime As New NotesDateTime(todaysdate.DateOnly)
Dim keyarray(1) As Variant
keyarray(0) = feedname
Set keyarray(1) = dateTime
Set vc = view.GetAllEntriesByKey(keyarray, False)
Set entry = vc.GetFirstEntry
If entry Is Nothing Then
Call todaysdate.AdjustDay(-1)
End If
While Not entry Is Nothing
files = 0
Forall colval In entry.ColumnValues
If counter = 9 Then
counter = 0
Elseif counter = 8 Then
counter = 9
Elseif counter = 7 Then
counter = 8
Elseif counter = 6 Then
errors = Cint(colval)
counter = 7
Elseif counter = 5 Then
counter = 6
Elseif counter = 4 Then
files = Cint(colval)
counter = 5
Elseif counter = 3 Then
counter = 4
Elseif counter = 2 Then
counter = 3
lasttime = colval
Elseif counter = 1 Then
counter = 2
lastdate = colval
Elseif counter = 0 Then
counter = 1
End If
End Forall
filesp = filesp + files
terrors = terrors + errors
Set entry=vc.GetNextEntry (entry)
flag = 1
Wend
Wend
lastdayfiles = filesp
lastdayerrors = terrors
lastdayran = lastdate
lasttimeran = lasttime
'Counts previous files processed
filesp = 0
terrors = 0
lastdate = ""
flag = 0
Call todaysdate.AdjustDay(-1)
While flag = 0
Dim dateTime2 As New NotesDateTime(todaysdate.DateOnly)
Dim keyarray2(1) As Variant
keyarray2(0) = feedname
Set keyarray2(1) = dateTime2
Set vc = view.GetAllEntriesByKey(keyarray2, False)
Set entry = vc.GetFirstEntry
If entry Is Nothing Then
Call todaysdate.AdjustDay(-1)
End If
While Not entry Is Nothing
files = 0
Forall colval In entry.ColumnValues
If counter = 9 Then
counter = 0
Elseif counter = 8 Then
counter = 9
Elseif counter = 7 Then
counter = 8
Elseif counter = 6 Then
counter = 7
Elseif counter = 5 Then
counter = 6
Elseif counter = 4 Then
files = Cint(colval)
counter = 5
Elseif counter = 3 Then
counter = 4
Elseif counter = 2 Then
counter = 3
Elseif counter = 1 Then
counter = 2
Elseif counter = 0 Then
counter = 1
End If
End Forall
filesp = filesp + files
Set entry=vc.GetNextEntry (entry)
flag = 1
Wend
Wend
lastdaysfiles2 = filesp
' Prints line of CSV into body of email
Call rtitem.AppendText ( customername )
Call rtitem.AppendText ( ", " )
Call rtitem.AppendText ( servername )
Call rtitem.AppendText ( ", " )
Call rtitem.AppendText ( datetime.timezone )
Call rtitem.AppendText ( ", " )
Call rtitem.AppendText ( lastdayran )
Call rtitem.AppendText ( " " )
Call rtitem.AppendText ( lasttimeran )
Call rtitem.AppendText ( ", " )
Call rtitem.AppendText ( lastdayfiles )
Call rtitem.AppendText ( ", " )
Call rtitem.AppendText ( lastdayerrors )
Call rtitem.AppendText ( ", " )
Call rtitem.AppendText ( lastdaysfiles2 )
Call rtitem.AppendText ( ", " )
Call rtitem.AppendText ( inputfeedcounter )
Call rtitem.AppendText ( ", " )
Call rtitem.AppendText ( alertthresholdinhours )
Call newDoc.Save( False, True )
newDoc.Subject = feedname
' Running from server line should be
'newDoc.SendTo = "Ecmon Feedcheck/Ecmonitor#ECMONITOR"
newDoc.SendTo = "AX1Forward Feedcheck/ACHQ#company.com"
newDoc.Send( False )
End Sub
For not being a developer, you can write a lot of code :)
If you are looking for some lessons to start becoming a good developer, then take Mitch's advice (from the comments) and break up this into subroutines. Lesson 1: There's definitely some repetitve code here, and it's always a good idea to put repetitive code into a method (function or subroutine) so it only exists once. The section that counts files processed and previous files processed looks similar and could probably be put into a routine like:
Function GetCountFilesProcessed() As Integer
'code here
End Function
However, you might even be able to save the need for that if I'm understanding your code correctly. Instead of doing that strange loop in the middle, it appears you're are trying to simply get a value from a column of a viewentry. Say you are interetsed in column 4's value. You can simply get at the value for the column you want by accessing it by index. For example, your files variable could be set directly to column 4's value by this line
files = Cint(entry.ColumnValues(4)) 'check this, it might be 3 if the array is zero based.
Anyway, bottom line is if this code works then you're off to a good start!
On more of the style side of things, I've always found it easier to maintain other people's code when they've
Declared Option 'Explicit' in the Declarations section
Declared variables from approximately higher to lower (eg, session before db before view before doc)
Prefixed notes objects with their type (docMail, dbMyDatabase, viewOutstandingInvoices)
Put all of the Declarations at the top before (helps finding the declaration when you come across a variable)
As others have mentioned, break it up into functions/subs where applicable.
Your comment about copying this agent for other instances of the same problem also raises a flag. Try to work out what is common between these agents and push those functions into a script library. This sort of thing saves a lot of time when maintaining the code as you don't need to think about in what way each agent is different (Eg. does my change apply to all instances of this agent, or just some of them?)
You want to decompose your code much more and ... one little thing. Instead of
while not item is nothing
which is a double negation and a popular brain bender.. write:
do until item is nothing
...
loop
This also allows you to break out of the loop with exit do
you can optimize both of your ForAll loops.
This is how the first one would look:
Forall colval In entry.ColumnValues
Select Case (counter)
Case 1: lastdate = colval
Case 2: lasttime = colval
Case 4: files = Cint(colval)
Case 6: errors = Cint(colval)
End Select
counter = (counter + 1) Mod 10
End Forall
This is how the second would one would look like:
Forall colval In entry.ColumnValues
if (counter = 4) Then files = Cint(colval)
counter = (counter + 1) Mod 10
End Forall
Just a note regarding this bit
While Not entry Is Nothing
files = 0
Forall colval In entry.ColumnValues
If counter = 9 Then
counter = 0
Elseif counter = 8 Then
counter = 9
....
As ken says you can get columnValues by using the entry.ColumnValues(x) method so interating through the values is un-needed. But; you could have done this
While Not entry Is Nothing
files = 0
counter = 0
Forall colval In entry.ColumnValues
counter = counter + 1
Select case counter
case 6
errors = Cint(colval)
.....
end select
Some good points already. To add to them if you have variables that share a common object then create a class. Add your variables to that class.
So instead of say:
Dim userFullName as String
Dim age as Integer
Dim addressLine1 as String
' ... etc.
You can have:
Class UserDetails
Dim fullName as String
Dim age as Integer
Dim addressLine1 as String
' ... etc
End Class
and reference:
Dim u as new UserDetails
u.fullName = "full name"
u.age = 22
u.addressLine1 = "1 main street"
The advantage of this is you can add methods to manipulate that data and you know the code relates to that object rather then hunting through your application.
Related
I am currently writing a macro for excel and experiencing some issues using the "i = i + 1" code. I have used this elsewhere in my macro and it has worked fine, however for this section it is failing to increment correctly.
As this is quite a large piece of code, I have only included the section which is having issues, however if in order to properly debug this more code is required i am happy to provide this.
I have checked the syntax as has a colleague and we are struggling to find out where the issue is originating from, any help on this would be appreciated.
All declared variables:
D
im lastSite As Range: Set lastSite = wsReport.Range("I2:I1000")
Dim expectedSite As Range: Set expectedSite = wsReport.Range("H2:H1000")
Dim expectedLocation As Range: Set expectedLocation = wsReport.Range("G2:G1000")
Dim lostTOA As Range: Set lostTOA = wsTOAOut.Range("G2:G1000")
Dim lostMissing As Range: Set lostMissing = wsMissing.Range("G2:G1000")
siteCode = wsHome.Range("Q6")
Dim last
Dim expected
Dim lostOut
Dim lostMiss
departments = wsHome.Range("R6:R20")
Working as intended:
Dim iAllocation As Integer
For Each dept In departments
If dept = "" Then
Exit For
End If
wsLBD.Range("A" & rowCounterDepartments).Value = dept
For Each expected In expectedLocation
If expected = dept And Not expected = "" Then
iAllocation = iAllocation + 1
End If
Next
wsLBD.Range("C" & rowCounterDepartments).Value = iAllocation
iAllocation = 0
rowCounterDepartments = rowCounterDepartments + 1
Next
Not Working as intended:
rowCounterDepartments = 2
departmentsOut = wsHome.Range("R6:R20")
Dim iLoss As Integer
Dim iMissing As Integer
For Each lostOut In lostTOA
If lostOut = "" Then
Exit For
End If
For Each deptOut In departmentsOut
If lostOut = deptOut And Not lostOut = "" Then
iLoss = iLoss + 1
End If
Next
wsLBD.Range("B" & rowCounterDepartments).Value = iLoss
iLoss = 0
rowCounterDepartments = rowCounterDepartments + 1
Next
The output expected should be a total count for each department, however it is only outputting a 1 or 0 per line
This has now been resolved, i re-wrote this piece of code and it was a syntax error on my part, a couple of the pieces were the wrong way round.
I am programming a kind of parser which reads an Excel table and then creates a
List of processes with some properties like Name, StartTime, EndTime etc.
For this I have a class Process and in the main file, I have a processList (Scripting.Dictionary), where I put the processes as I read the lines... For this assignment, the key is a String called MSID.
Now the problem is that for some reason, I am only able to access the Object from the Dictionary and alter its parameters inside one part of my If-ElseIf statement. In the other case, it throws 424-object required error and I have no idea why.
Here is the code
Sub ParseMessages()
' ITERATOR VARIABLES
Dim wb As Workbook, ws As Worksheet
Dim rowIter As Long, row As Variant
Dim A As Variant, B As Variant, C As Variant, D As Variant, E As Variant, F As Variant ' A,B,C,D,E,F variables for the cells of each row
' PROCESS PARAMETERS
Dim MSID As Variant
Dim StartTime As Variant
Dim EndTime As Variant
' OBJECTS
Dim process As process
Dim processList As Scripting.Dictionary ' DICTIONARY where the error happens
Set processList = New Scripting.Dictionary
Worksheets(1).Activate
'####### MAIN LOOP ######################################################
For rowIter = 1 To 11
row = Rows(rowIter)
A = row(1, 1)
B = row(1, 2)
C = row(1, 3)
D = row(1, 4)
E = row(1, 5)
F = row(1, 6)
Dim startIndex As Long, endIndex As Long, count As Long
' ------ PROCESSSTART -> MSID, processName, startTime
If (.....) Then
Debug.Print (vbNewLine & "Process start")
If (...) Then ' --MSID
startIndex = InStr(F, "Nr") + 3 '3 to skip "Nr "
endIndex = InStr(startIndex, F, "(")
count = endIndex - startIndex
MSID = Mid(F, startIndex, count)
StartTime = B
Debug.Print (StartTime & " -> " & MSID)
' **** MAKE new Process object, add to collection
Set process = New process
process.StartTime = StartTime
process.MSID = MSID
processList.Add MSID, process ' Add to the dictionary, KEY, VALUE
ElseIf (...) Then ' --ProcessName
startIndex = InStr(F, "=") + 2
endIndex = InStr(F, "<") - 1
count = endIndex - startIndex
processName = Mid(F, startIndex, count)
Debug.Print (processName)
' **** Add Name to the last element of the dictionary
processList(processList.Keys(processList.count - 1)).Name = processName 'get last Process Object
processList(MSID).Name = "Just Testing" ' !!!! here it works
Else
End If
' ------ END OF PROCESS ->
ElseIf (......) Then
startIndex = InStr(D, "MSID") + 5
endIndex = InStr(startIndex, D, "]")
count = endIndex - startIndex
MSID = Mid(D, startIndex, count)
EndTime = B
Debug.Print (EndTime & " End of process " & MSID)
' **** Add End time for the process from the collection, specified by MSID
Debug.Print ("Test of " & processList(MSID).Name) ' !!!!! Doesn't work
processList(MSID).Name = "Just Prooooooving" ' !!!!! Here doesn't work
processList(MSID).EndTime = EndTime ' !!!!! Does not work
End If
Next
End Sub
So to specify the question - why is it that this works:
processList(MSID).Name = "Just Testing" ' !!!! here it works
And this doesn't:
processList(MSID).Name = "Just Prooooooving" ' !!!!! Here doesn't work
If I first prove if the Object with the MSID key exists in the dictionary,
it's not found.
If processList.Exists(MSID) Then
Debug.Print ("Process exists, altering it...")
processList(MSID).Name = "Just Prooooooving" ' !!!!! Here doesn't work
processList(MSID).EndTime = EndTime
End If
But on the very first line where the condition is evaluated, I get something different by debug. It's THERE! See picture below
Debugging - MSID there but not able to access Dictionary entry with this as a key
Can you suggest how to solve this issue?
Thank you very much in advance for your help!
So... It's a bit shameful but after some hours of trying to solve this problem I found out,
that I added the Object to the list with MSID="124 " as Key.
When I tried to access, I of course used MSID with value "124".
Notice the difference? Yes, that space at the end.
The tricky part is - VBA debugger trims spaces at the end of Strings,
so it's actually impossible to see it. The same situation is if you print this out - impossible to see...
So in the end, I spent many hours looking for the answer, which is so simple :/
All I can do is to laugh about this.
Sub Initialize
On Error GoTo ErrorOut
Dim sess As NotesSession
Dim db As NotesDatabase
Dim doc, searchDoc, reqNumDoc As NotesDocument
Dim body As NotesMIMEEntity
Dim header As NotesMIMEHeader
Dim stream As NotesStream
Dim vwSearchRequests As NotesView
Dim reqNum, totalNotify, totalAccepted, totalRejected, totalOOO, totalNoRes As Integer
Dim reqSer, reqJRSS, reqSPOC, reqNumStr As String
Dim reqDate As String
Dim reqNumColl As NotesDocumentCollection
Dim reqPanelRes As NotesItem
Dim reqPanelResValue As Variant
Set sess = New NotesSession
Set db = sess.CurrentDatabase
Set vwSearchRequests = db.GetView("RequestDocReport")
vwSearchRequests.Autoupdate = False
Set searchDoc = vwSearchRequests.GetFirstDocument
While Not searchDoc Is Nothing
reqSer = "Service"
reqJRSS = searchDoc.PS_JRSS(0)
reqSPOC = "Hiring SPOC"
totalAccepted = 0
totalRejected = 0
totalOOO = 0
totalNoRes = 0
totalNotify = 0
reqNum = searchDoc.PS_RequestNo(0)
reqNumStr = {PS_RequestNo = "} & reqNum & {"}
Set reqNumColl = vwSearchRequests.GetAllDocumentsByKey(reqNumStr)
Set reqNumDoc = reqNumColl.GetFirstDocument
While Not reqNumColl Is Nothing
If Not reqNumDoc.GetFirstItem("PanelResponse") Is Nothing Then
reqPanelResValue = reqNumDoc.GetItemValue("PanelResponse")
MsgBox CStr(reqPanelResValue(0))
'Exit Sub
If CStr(reqPanelResValue(0)) = "Accepted" Then
totalAccepted = totalAccepted + 1
End If
If CStr(reqPanelResValue(0)) = "Rejected" Then
totalRejected = totalRejected + 1
End If
If CStr(reqPanelResValue(0)) = "OOO" Then
totalOOO = totalOOO + 1
End If
Else
If CStr(reqPanelResValue(0)) = "" Then
totalNoRes = totalNoRes + 1
End If
End If
totalNotify = totalNotify + 1
Set reqNumDoc = reqNumColl.GetNextDocument(reqNumDoc)
Wend
what is the error in code? The code is getting stuck after
If Not reqNumDoc.GetFirstItem("PanelResponse") Is Nothing Then
reqPanelResValue = reqNumDoc.GetItemValue("PanelResponse")
Instead of line
While Not reqNumColl Is Nothing
write
While Not reqNumDoc Is Nothing
You got an infinitive loop because the collection reqNumColl is not nothing all the time even when you reached the last document in collection. Instead you have to test the document reqNumDoc.
Another issue might be your code for collection calculation:
reqNumStr = {PS_RequestNo = "} & reqNum & {"}
Set reqNumColl = vwSearchRequests.GetAllDocumentsByKey(reqNumStr)
The way you coded it the first sorted column in view should contain
PS_RequestNo = "12345"
Probably, your view contains in first sorted column just the request number. If so, your code would be just:
Set reqNumColl = vwSearchRequests.GetAllDocumentsByKey(reqNum)
if column contains a numeric value or
Set reqNumColl = vwSearchRequests.GetAllDocumentsByKey(cStr(reqNum))
if it contains a string.
Apart from any other problems you might have in your code (and #Knut is correct about the cause of your infinite loop), this is not a good pattern:
If Not reqNumDoc.GetFirstItem("PanelResponse") Is Nothing Then
reqPanelResValue = reqNumDoc.GetItemValue("PanelResponse")
You're retrieving the item twice when you don't actually have to.
This woould be much better:
If reqNumDoc.HasItem"PanelResponse") Then
reqPanelResValue = reqNumDoc.GetItemValue("PanelResponse")
I have some code that gets a list of all views&folders in a database. The problem is everything is very poorly organized. The list of views shows as almost random, although I assume there is some order...
Is there a way to take the list of all the views that show on the dialog prompt, and then sort the list alphabetically?
Sub Initialize
Dim s As New NotesSession
Dim w As New NotesUIWorkspace
Dim dbSource As NotesDatabase, dbDest As NotesDatabase
Dim source As NotesView, dest As NotesView
Dim vc As NotesViewEntryCollection
Dim docDest As NotesDocument
Dim ve As NotesViewEntry
Dim folders() As String
Dim i As Integer
Dim ret, rez
Set dbSource = s.CurrentDatabase
ForAll v In dbSource.Views
If v.IsFolder Then
i = i + 1
ReDim Preserve folders( i - 1 ) As String
folders( i - 1 ) = v.Name
End If
End ForAll
ret = w.Prompt( PROMPT_OKCANCELLISTMULT, "Folder selection", "Select one or more folders to move.", folders(0), folders )
If IsEmpty( ret ) Then
MessageBox "User canceled", , "Folder not selected"
Exit Sub
Else
rez = w.Prompt( 13, "Database selection", "Choose the database to move the folder to" )
ForAll f In ret
Set source = dbSource.GetView( f )
Set vc = source.AllEntries
Set dbDest = s.GetDatabase( rez(0), rez(1), False )
Call dbDest.EnableFolder( f )
Set ve = vc.GetFirstEntry
Do Until ve Is Nothing
Set docDest = ve.Document.CopyToDatabase( dbDest )
Call docDest.PutInFolder( f )
Set ve = vc.GetNextEntry( ve )
Loop
Call vc.RemoveAllFromFolder( f )
Call source.Remove
End ForAll
End If
End Sub
You can add this subroutine to sort the folders array before you include it in your prompt method.
Just call ShellSort(folders) before the line with w.Prompt();
Sub ShellSort( ar( ) As String )
Dim Lower As Integer
Dim Upper As Integer
Dim botMax As Integer
Dim i As Integer
Dim k As Integer
Dim h As Integer
Dim v As String
Lower% = Lbound( ar$( ) )
Upper% = Ubound( ar$( ) )
h% = 1
Do
h% = (3*h%) + 1
Loop Until h% > Upper%-Lower%+1
Do
h% = h% \ 3
botMax% = Lower% + h% - 1
For i% = botMax% + 1 To Upper%
v$ = ar$( i% )
k% = i%
While ar$( k% - h% ) > v$
ar$( k% ) = ar$( k% - h% )
k% = k% - h%
If (k% <= botMax%) Then Goto wOut
Wend
wOut:
If (k% <> i%) Then ar$(k%) = v$
Next
Loop Until h% = 1
End Sub
(source: http://www.dominoexperts.com/articles/Very-fast-sorting-algorithm)
You can use #Sort in a Evaluate call to get the folder names sorted.
Dim foldersList As String
Dim folders As Variant
...
ForAll v In dbSource.Views
If v.IsFolder Then
foldersList = foldersList + |:"| + Replace(Replace(v.Name, |\|, |\\|), |"|, |\"|) + |"|
End If
End ForAll
If foldersList <> "" then
folders = Evaluate(|#Sort(| + StrRight(foldersList, ":") + |)|)
...
The code above works this way:
First collect all folder names in a string as a formula list like
"xyz":"abc":"mno":"def"
Make sure that quotation marks in folder names are replaced by \" and backslashes for subfolders are replaced by \\.
Second evaluate a formula with #Sort("xyz":"abc":"mno":"def").
The result is a sorted variant string array you can use in your w.Prompt(..., ..., folders(0), folders ) like before.
The benefit of this solution is a very short code and it is fast too.
If you use a list instead of an array then the values will be automatically placed in a sorted order. eg.
Dim folders List As Boolean
ForAll v In dbSource.Views
If (v.IsFolder) Then folders(v.Name) = True
End ForAll
I have two databases: one holds the employee summary info, and the other one holds the serial number of the employee. On the database1, I have this agent that lets you import text file which contains updated records of the employee. But, In order for this agent to import a text file records successfully, the text file to be import must have a serial number record same as on the database2. It's working by the way, but I need to create a log when importing, still got no idea on how to resolve this. The logs(date imported, success and fail file imported) must be viewed on the fields. Can you help me out? Here's my code:
LoadAPMSAWDdoc
Sub LoadAPMSAWDdoc(Rname As Variant, directory As Variant, Datef As Variant)
Dim session As New NotesSession
Dim Tdoc As NotesDocumentCollection
Dim dateTime As New NotesDateTime ("01/01/2000")
'12/20/2005
Dim LocView As notesview
Dim LocDoc As NotesDocument
Dim subsidiary As String
Print "Loading APMSAWD - Award Information"
Set cdb = Session.CurrentDatabase
'12/20/2005
'StaffServerName = cdb.Server
Set LocView = cdb.GetView("LsvLocationProfile")
'02/07/2006
'Set LocDoc = LocView.getdocumentbykey(cdb.Server)
Set LocDoc = LocView.getfirstdocument
StaffServerName = LocDoc.z_ExtServer(0)
'SearchFormula$ = "Select Form = ""dfAward"" & #Date(s_Created) != #Date(#Today) "
If (ibmmy = True) And (ibmgdc = True) Then
SearchFormula$ = "Select Form = ""dfAward"" "
ElseIf (ibmmy = True) Then
SearchFormula$ = "Select Form = ""dfAward"" & I_GDCEmployee = ""IBM MY"""
Else
SearchFormula$ = "Select Form = ""dfAward"" & I_GDCEmployee = ""IBM GDC"""
End If
Set Tdoc = cdb.Search( SearchFormula$, DateTime, 0 )
If Tdoc.Count <> 0 Then
Call Tdoc.RemoveAll(True)
End If
'Get an unused file number
file_no% = Freefile()
Open (Trim(directory + "apmsawd.txt")) For Input As file_no%
Set db = Session.CurrentDatabase
Select Case Datef
Case "DMY" : Cdatf = "dd/mm/yyyy"
Case "MDY" : Cdatf = "mm/dd/yyyy"
Case "YMD" : Cdatf = "yyyy/mm/dd"
Case Else :
Print "LoadAPMSAWDdoc - Unknown system date format"
Exit Sub
End Select
Do While Not Eof(file_no%)
Line Input #file_no%, tmp
SerialNo = Trim$(Mid$(tmp,1,6))
AB = 0
For i = 29 To 0 Step -1
x1 = 8 + (i * 50)
x2 = 11 + (i * 50)
x3 = 41 + (i * 50)
x4 = 49 + (i * 50)
temp = Strconv(Trim$(Mid$(tmp,x2,30)),3)
If temp <> "" Then
Redim Preserve ACode(AB)
Redim Preserve ADes(AB)
Redim Preserve ADate(AB)
Redim Preserve AAmt(AB)
Acode(AB) = Trim$(Mid$(tmp,x1,3))
ADes(AB) = temp
If Trim$(Mid$(tmp,x3,8)) <> "" Then
AD1 = Setdate(Trim$(Mid$(tmp,x3,8)), "mm/dd/yy", Datef)
ADate(AB) = Cdat(Format(AD1, Cdatf))
'Datenumber ( Val(Trim$(Mid$(tmp,x3+6,2))) , Val(Trim$(Mid$(tmp,x3+3,2))) , Val(Trim$(Mid$(tmp,x3,2))) )
Else
ADate(AB) = Null
End If
AAmt(AB) = Val(Trim$(Mid$(tmp,x4,9)))
AB = AB + 1
Else
Exit For
End If
Next
subsidiary = Filter(CStr(SerialNo))
If (subsidiary = "AMY" And ammmy = True) Or (subsidiary = "ADC" And aaadc = True) Then
Set doc = New NotesDocument(db)
doc.Form = "dfAward"
doc.E_StaffSerialNo = SerialNo
doc.I_GDCEmployee = subsidiary
If AB = 0 And Trim$(Mid$(tmp,1461,30)) = "" Then
Redim Preserve ACode(AB)
Redim Preserve ADes(AB)
Redim Preserve ADate(AB)
Redim Preserve AAmt(AB)
ACode(0) = ""
ADes(0) = ""
ADate(0) = Null
AAmt(0) = Null
End If
doc.E_AwardType = ADes
doc.E_AwardDate = ADate
doc.E_AwardAmt = AAmt
doc.G_AuthorDisp = Rname
doc.s_created = Now
Call doc.Save (True, True)
End If
Loop
Close file_no%
Print "Award information imported"
End Sub
I'm sorry if I only posted some functions coz my code is too long and can't fit here.
First of all: It is VERY bad practice, to permanently delete all documents that match a search criteria just to directly afterwards add them back.
Deletion stubs will explode and this database will become slower and slower and at some point will not be usable anymore.
Better build a key to identify the document, get the document using the key and then update if necessary...
I usually build a list with all keys / unids (or documents, if there are not to much of them) and remove any document found in the "source" (text document in your case) from that list after processing.
Any document left in the list after running through the import file can be deleted...
Dim lstrUnids List as String
Set doc = Tdoc.GetFirstDocument()
While not doc is Nothing
lstrUnids( doc.E_StaffSerialNo(0) ) = doc.UniversalID
set doc = TDoc.GetNextDocument(doc)
Wend
But now back to your Question:
To write a simple Log you can use the NotesLog- Class. You can either log to a database (Template: Agent Log ), log to a mail or log to the Agents log (complicated to read) or even to a file.
Just do it like this:
Dim agLog as New NotesLog( "MyImportLog" )
Call agLog.OpenNotesLog( Server , logdbPath )
...
Call agLog.LogMessage( "Award information imported" )
...
Call agLog.Close() 'IMPORTANT !!!!