Link to home
Start Free TrialLog in
Avatar of jkee54
jkee54Flag for United States of America

asked on

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      
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

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?
Here's some code you might be interested in:

    http:Q_21382610.html "Calculating Time Difference"
Avatar of jkee54

ASKER

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?
Avatar of jkee54

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Sjef Bosman
Sjef Bosman
Flag of France image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of marilyng
marilyng


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  
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

Avatar of jkee54

ASKER

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
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."

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.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Wouldn't dare... Backdoor open?
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
Read again:
> It is failing on ONLY the Forall loop, giving the error, Object Variable not set.
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
sorry , I was overlooking the question &  thought it was a compile issue.

It happens :)