Here's some code you might be interested in:
http:Q_21382610.html "Calculating Time Difference"
Main Topics
Browse All TopicsI 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(e
flag = 0
days = Weekday(startdate.LSLocalT
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
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Here's some code you might be interested in:
http:Q_21382610.html "Calculating Time Difference"
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
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("Wha
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(e
flag = 0
days = Weekday(startdate.LSLocalT
If days <> 1 And days <> 7 Then ' 'check for holidays
if not hookdoc is nothing then
if hookdoc.hasItem("AllHolida
if len(hookdoc.AllHolidays(0)
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
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("EDCha
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("StartDat
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(e
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(
Do While startdate.TimeDifference(e
flag = 0
days = Weekday(startdate.LSLocalT
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(
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("AllHolida
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.
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("EDCha
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("StartDat
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(e
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(
End If
'.........................
Do While startdate.TimeDifference(e
flag = 0
days = Weekday(startdate.LSLocalT
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("AllHolida
If Len(hookdoc.AllHolidays(0)
Forall holiday In hookdoc.AllHolidays
If Cdat(holiday) = startdate.LSLocalTime Then flag=1
End Forall
End If
End If
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
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
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
Business Accounts
Answer for Membership
by: sjef_bosmanPosted on 2006-05-18 at 08:18:54ID: 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?