[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

LotusScript Error on For All statement

Posted on 2006-05-18
21
Medium Priority
?
1,895 Views
Last Modified: 2013-12-18
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      
0
Comment
Question by:jkee54
  • 7
  • 6
  • 3
  • +1
19 Comments
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 16709591
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
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 16709852
Here's some code you might be interested in:

    http:Q_21382610.html "Calculating Time Difference"
0
 

Author Comment

by:jkee54
ID: 16710148
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:jkee54
ID: 16710231
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
 
LVL 46

Accepted Solution

by:
Sjef Bosman earned 400 total points
ID: 16710300
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
 
LVL 18

Expert Comment

by:marilyng
ID: 16710387

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

Expert Comment

by:HemanthaKumar
ID: 16710976
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
 

Author Comment

by:jkee54
ID: 16711122
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
 
LVL 18

Expert Comment

by:marilyng
ID: 16712191
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
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 16713180
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
 
LVL 18

Assisted Solution

by:marilyng
marilyng earned 400 total points
ID: 16714814
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
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 16715925
:-$
0
 
LVL 18

Expert Comment

by:marilyng
ID: 16722313
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
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 16722460
Wouldn't dare... Backdoor open?
0
 
LVL 24

Expert Comment

by:HemanthaKumar
ID: 16736846
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
 
LVL 46

Expert Comment

by:Sjef Bosman
ID: 16737970
Read again:
> It is failing on ONLY the Forall loop, giving the error, Object Variable not set.
0
 
LVL 18

Expert Comment

by:marilyng
ID: 16738964
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
 
LVL 24

Expert Comment

by:HemanthaKumar
ID: 16745277
sorry , I was overlooking the question &  thought it was a compile issue.

0
 
LVL 18

Expert Comment

by:marilyng
ID: 16747095
It happens :)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem "Can you help me recover my changes?  I double-clicked the attachment, made changes, and then hit Save before closing it.  But when I try to re-open it, my changes are missing!"    Solution This solution opens the Outlook Secure Temp Fold…
This article covers general Notes 8.5 troubleshooting information including recreating the Notes\Data folder.
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Loops Section Overview
Suggested Courses
Course of the Month17 days, 16 hours left to enroll

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question