LotusScript Error on For All statement

I have the following code code for calculating vacation days..  It is failing on ONLY the Forall loop, giving the error, Object Variable not set.  I looked through it and thought it was holiday not declared, but when I add Dim holiday as NotesDateTime, the code won't save because it says "alias variable was previously declared'.  Designer help says "You cannot declare a reference variable outside a ForAll statement. ".  When I rem out the Forall loop, the code runs but it doesn't calculate the days taken.

I can paste the rest of the code in if you want, but I'm only pasting in here the part that I know is failing and the declaration that applies.  Again there is no Dim for holiday, but when I put it in the code won't save, saying Data not saved due to script error, and the error is:  FORALL alias variable was previously declared: HOLIDAY

It's not DIM'd in this object or any other.  The variable TotalDays at the end is what I need.

Dim counter As Integer
Dim flag As Integer
Dim days As Integer
Dim duration() As String
Dim TotalDays As Integer
Dim count As Integer
Dim hookdoc As NotesDocument
Dim startdate As NotesDateTime
Dim enddate As NotesDateTime
      
Do While startdate.TimeDifference(enddate) <= 0   'Once we check the end date we're done!!
      flag = 0
      days = Weekday(startdate.LSLocalTime)
If days <> 1 And days <> 7 Then '                 'check for holidays
                  
      Forall holiday In hookdoc.AllHolidays
         If Cdat(holiday) = startdate.LSLocalTime Then flag=1
      End Forall
                  
      If flag =0 Then
            Redim Preserve duration(0 To counter)
            duration(counter) = startdate.LSLocalTime
            counter = counter +1
      End If
End If
Call startdate.AdjustDay(1)
Loop
  doc.TotalDays = counter
  doc.Duration = duration
  Call uidoc.Refresh
End If      
jkee54Asked:
Who is Participating?
 
Sjef BosmanGroupware ConsultantCommented:
There's an Exit Forall and an End Forall. As soon as you have found that it's a holiday, and you set flag=1, there's no point in continuing the search. Hence the Exit Forall, to break the loop (like a break-statement in C).

You like NotesDateTime objects? I don't, and theres hardly ever any reason to use them. A Variant variable will do, by definition the integral part of the double value indicates the day, whereas the fraction indicates the hours/minutes/seconds on the day itself. Somewhat rewritten, one gets this:

    Dim counter As Integer
    Dim IsHoliday As Integer
    Dim days As Integer
    Dim duration() As String
    Dim TotalDays As Integer
    Dim count As Integer
    Dim hookdoc As NotesDocument
    Dim startdate As NotesDateTime
    Dim enddate As NotesDateTime
    Dim tday As Variant ' or Double
    Dim eday As Variant ' or Double
         
    tday= startdate.LSLocalTime
    eday= enddate.LSLocalTime
    Do While tday<=eday   'Once we check the end date we're done!!
         IsHoliday= False
         days = Weekday(tday)
        If days <> 1 And days <> 7 Then '                 'check for holidays
                       
             Forall holiday In hookdoc.AllHolidays
                If Cdat(holiday) = tday Then
                    IsHoliday= True
                    Exit Forall
                End If
             End Forall
                       
             If Not IsHoliday Then
                  Redim Preserve duration(0 To counter)
                  duration(counter) = tday
                  counter = counter +1
             End If
        End If
        tday= tday + 1
    Loop
    doc.TotalDays = counter
    doc.Duration = duration
    Call uidoc.Refresh
0
 
Sjef BosmanGroupware ConsultantCommented:
Whatever the reason is, be it a globally defined variable holiday or anything else, you can't declare a foral-variable. Why not change the code to
     Forall hol In hookdoc.AllHolidays
        If Cdat(hol) = startdate.LSLocalTime Then
            flag=1
            Exit Forall
         End If
     End Forall

PS Your calculation is effective but rather crude, wouldn't you say?
0
 
Sjef BosmanGroupware ConsultantCommented:
Here's some code you might be interested in:

    http:Q_21382610.html "Calculating Time Difference"
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
jkee54Author Commented:
I don't get it, it looks like youare "Exit Forall" -ing twice - isn't that the same code I sent except for that flag = 1 is on the next line and  the extra Exit Forall?

Do you have a better suggestion?
0
 
jkee54Author Commented:
I have looked at similar code, but I only have to calculate days, not hours, and I have to accomodate for holidays, which are stored in an array on a form.
0
 
marilyngCommented:

Hi jkee54.. I don't see anywhere where hookdoc is instantiated.  I assume that it's supposed to be a document containing the holidays, so it's going to be in a non-existant view that you didn't instantiate either.    At any rate, I'm not at a computer with Notes on it, so I did this in Notepad.. if you get errors,  post them here, and I'm sure the EE masses will reply and trounch on the solution to make it their own :)  (Or you can wait until later today, and I will fix)


'I assume you are instantiating
Dim session as New NotesSession
Dim ws as New NotesUiWorkspace
Dim db as NotesDatabase
set db = session.currentdatabase
Dim uidoc as Notesuidocument
set uidoc = ws.currentdocument
set doc = uidoc.document

Dim counter As Integer
Dim flag As Integer
Dim days As Integer
Dim duration() As String
Dim TotalDays As Integer
Dim count As Integer
'What is Hookdoc?  Is this the same as Doc?
Dim hookdoc As NotesDocument
Dim startdate As NotesDateTime
Dim enddate As NotesDateTime

'Somewhere in here you have to declare the view and document lookup.

Dim view as NotesView
set view = db.getview("This View Name")
if not view is nothing then
  set hookdoc = view.getdocumentbykey("Whateverthisholidaykeyis",true)
  if hookdoc is nothing then
    set hookdoc = view.getfirstdocument
  end if
else
  msgbox "Sorry, lookup view cannot be found for hookdoc"
  exit sub
end if

     
Do While startdate.TimeDifference(enddate) <= 0   'Once we check the end date we're done!!
     flag = 0
     days = Weekday(startdate.LSLocalTime)

If days <> 1 And days <> 7 Then '                 'check for holidays

if not hookdoc is nothing then
  if hookdoc.hasItem("AllHolidays") then
   if len(hookdoc.AllHolidays(0)) > 0 then
    Forall holiday In hookdoc.AllHolidays
        If Cdat(holiday) = startdate.LSLocalTime Then flag=1
     End Forall
    end if
 end if
else
  msgbox "Sorry, can't find the holidays document, will skip holidays"
end if
               
     If flag =0 Then
          Redim Preserve duration(0 To counter)
          duration(counter) = startdate.LSLocalTime
          counter = counter +1
     End If
End If

Call startdate.AdjustDay(1)

Loop

  doc.TotalDays = counter
  doc.Duration = duration
  Call uidoc.Refresh

End If  
0
 
HemanthaKumarCommented:
jkee54,

Ther error says everything...

Post the entire code where forall section is present , so that we can get a better idea.

Second global variables doesn't matter with forall...it is always private as far as 'forall' is concerned

~Hemanth

0
 
jkee54Author Commented:
OK, it is long, so I tried to shortcut.  Here is the entire code:

Sub CalculateDays
      Dim session As New NotesSession
      Dim workspace As New NotesUIWorkspace
      Dim doc As NotesDocument
      Dim uidoc As NotesUIDocument
      Dim startdate As NotesDateTime
      Dim enddate As NotesDateTime
      Dim startitem As NotesItem
      Dim enditem As NotesItem
      Dim counter As Integer
      Dim flag As Integer
      Dim days As Integer
         Dim hookdoc As NotesDocument
      Dim hookview As NotesView
      Dim db As NotesDatabase
      Dim duration() As String
      Dim TotalDays As Integer
      Dim count As Integer
      
      Set db = session.CurrentDatabase
      Set uidoc = workspace.CurrentDocument
      Set doc = uidoc.Document
      If workspace.DialogBox("EDChange", True, True, False, False, False, False, "Please Select the Beginning and Ending Dates.") Then
            doc.Counter = 2
            
          'if either field is empty don't do calculations and blank out days field.
            If doc.StartDate(0) = "" Or doc.EndDate(0) = "" Then
                  doc.TotalDays = ""
                  End
            End If
            Set startitem = doc.GetFirstItem("StartDate")
            Set startdate = startitem.DateTimeValue
            Set enditem = doc.GetFirstItem("EndDate")
            Set enddate = enditem.DateTimeValue
            counter = 0
            
          'Make sure that the start date is before the end date
      If startdate.TimeDifference(enddate) > 0 Then
            Messagebox "The Start Date cannot come after the End Date.", 16, "Incorrect Dates"
            Call uidoc.GoToField("StartDate")
            End
      End If
            
          'Get the list of Holidays
            Set hookview = db.GetView("Holidays")
            Set hookdoc = hookview.GetDocumentByKey("Holidays",True)
            
            
Do While startdate.TimeDifference(enddate) <= 0   'Once we check the end date we're done!!
            flag = 0
            days = Weekday(startdate.LSLocalTime)
      If days <> 1 And days <> 7 Then '                 'check for holidays
                        
            Forall holiday In hookdoc.AllHolidays
                  If Cdat(holiday) = startdate.LSLocalTime Then flag=1
            End Forall
                        
            If flag =0 Then
            Redim Preserve duration(0 To counter)
            duration(counter) = startdate.LSLocalTime
            counter = counter +1
      End If
                                      End If
            Call startdate.AdjustDay(1)
Loop
            doc.TotalDays = counter
            doc.Duration = duration
            Call uidoc.Refresh
      End If
End Sub
0
 
marilyngCommented:
Set hookview = db.GetView("Holidays")
          Set hookdoc = hookview.GetDocumentByKey("Holidays",True)

You need to check if hookdoc is something.. see my code for this..

if hookdoc is  nothing then
  ' do something.. declare a default, or exit, or skip over the stuff that depends on hookdoc.
else
 'Continue with hookdoc
end if

I'm guessing either the view doesn't exist, or the view first column isn't sorted, or the first column is "holidays" rather than "Holidays."

0
 
Sjef BosmanGroupware ConsultantCommented:
Or there is no field AllHolidays in that document.

    If Not(hookdoc Is Nothing) Then
        If hookdoc.HasItem("AllHolidays") Then
             Forall holiday In hookdoc.AllHolidays
                If Cdat(holiday) = tday Then
                    IsHoliday= True
                    Exit Forall
                End If
             End Forall
        End If
    End If

If there's only one document in the Holidays-view, ever, then just a GetFirstDocument would do. Better still, make a it ProfileDocument and get rid of the view.
0
 
marilyngCommented:
Ok, confused... I feel like I'm talking but everyone is skipping over me.. sjef, I said that in my first post when you didn't see that hookdoc or hookview might throw an error.

At any rate, here is  my suggested changes implemented in your code:

Dim session As New NotesSession
      Dim workspace As New NotesUIWorkspace
      Dim doc As NotesDocument
      Dim uidoc As NotesUIDocument
      Dim startdate As NotesDateTime
      Dim enddate As NotesDateTime
      Dim startitem As NotesItem
      Dim enditem As NotesItem
      Dim counter As Integer
      Dim flag As Integer
      Dim days As Integer
      Dim hookdoc As NotesDocument
      Dim hookview As NotesView
      Dim db As NotesDatabase
      Dim duration() As String
      Dim TotalDays As Integer
      Dim count As Integer
      
      Set db = session.CurrentDatabase
      Set uidoc = workspace.CurrentDocument
      Set doc = uidoc.Document
      If workspace.DialogBox("EDChange", True, True, False, False, False, False, "Please Select the Beginning and Ending Dates.") Then
            doc.Counter = 2
            
          'if either field is empty don't do calculations and blank out days field.
            If doc.StartDate(0) = "" Or doc.EndDate(0) = "" Then
                  doc.TotalDays = ""
                  End
            End If
            Set startitem = doc.GetFirstItem("StartDate")
            Set startdate = startitem.DateTimeValue
            Set enditem = doc.GetFirstItem("EndDate")
            Set enddate = enditem.DateTimeValue
            counter = 0
            
          'Make sure that the start date is before the end date
            If startdate.TimeDifference(enddate) > 0 Then
                  Messagebox "The Start Date cannot come after the End Date.", 16, "Incorrect Dates"
                  Call uidoc.GoToField("StartDate")
                  End
            End If
            
          'Get the list of Holidays
            Set hookview = db.GetView("Holidays")
            'Added by mgl...............................................................................................
            If hookview Is Nothing Then
                  Msgbox "There is no hookview in this database.."
            Else
                  Set hookdoc = hookview.GetDocumentByKey("Holidays",True)      
            End If
            '.........................................................................................................end addition      
            
            Do While startdate.TimeDifference(enddate) <= 0   'Once we check the end date we're done!!
                  flag = 0
                  days = Weekday(startdate.LSLocalTime)
                  If days <> 1 And days <> 7 Then '                 'check for holidays
                        
                        'Added by mgl.........................................................................
                        If hookdoc Is Nothing Then
                              Msgbox "There is no holidays document in this database..."
                        Else
                              If hookdoc.HasItem("AllHolidays") Then
                                    If Len(hookdoc.AllHolidays(0))>0 Then
                                          Forall holiday In hookdoc.AllHolidays
                                                If Cdat(holiday) = startdate.LSLocalTime Then flag=1
                                          End Forall
                                    End If
                              End If                              
                        End If
                        '........................................................................................................Finished mgl.
                        If flag =0 Then
                              Redim Preserve duration(0 To counter)
                              duration(counter) = startdate.LSLocalTime
                              counter = counter +1
                        End If
                  End If
                  Call startdate.AdjustDay(1)
            Loop
            doc.TotalDays = counter
            doc.Duration = duration
            Call uidoc.Refresh
      End If
0
 
Sjef BosmanGroupware ConsultantCommented:
:-$
0
 
marilyngCommented:
Oops, let's see... I summarized both sjef and my observations and made the corrections in the code.. sjef, feel free to jump in if I missed something.
0
 
Sjef BosmanGroupware ConsultantCommented:
Wouldn't dare... Backdoor open?
0
 
HemanthaKumarCommented:
guys, are u and the questioner speaking the same !

He is getting forall error and you guys are talking about some field, which u are not even aware ! So what does that have to do with compile error ?

For me it looks like (usual) code corruption, Run compact and fixup on this database. If that doesn't correct the issue, recreate the form by pasting the fields and code's into this form and delete the old one. Before doing anything just take a backup for safety
0
 
Sjef BosmanGroupware ConsultantCommented:
Read again:
> It is failing on ONLY the Forall loop, giving the error, Object Variable not set.
0
 
marilyngCommented:
Sorry, Heman.. not code corruption, I think.  Objects uninstantiated.

You will ALWAYS get an error on Forall d in doc.fieldname if:
         doc is nothing
         fieldname is nothing
         the contents of fieldname is empty.

The only way I know to avoid the error is:
    trap it with a handle_error
    check to see if doc is something
    check to see if doc has the field
    check to see if the field is not empty
0
 
HemanthaKumarCommented:
sorry , I was overlooking the question &  thought it was a compile issue.

0
 
marilyngCommented:
It happens :)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.