We help IT Professionals succeed at work.

lotus script for date comparison with message box

Ray Padilla
Ray Padilla asked
on
826 Views
Last Modified: 2013-12-18
Experts, I need this turned into a real script:
Sub Queryopen(Source As Notesuidocument, Mode As Integer, Isnewdoc As Variant, Continue As Variant)
 
If Source.Document.HireDate > Now Then
messagebox
continue=false
end if

End Sub

This is for a view if the hiredate is not today or greater then a message box stating that person is expected to start on whatever the hiredate field says else allow the doc to open
Comment
Watch Question

Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
I don't really care why you want to do this but here's how you do it...

Sub Queryopen(Source As Notesuidocument, Mode As Integer, Isnewdoc As Variant, Continue As Variant)
      
      If (Not Source.IsNewDoc) Then
            Dim hireDate As NotesDateTime
            Dim nowDate As NotesDateTime
            Set hireDate = DtGetLsDateTime(Source.Document.GetItemValue("HireDate")(0))
            Set nowDate = DtGetLsDateTime(Now)
            If (DtGetDaysDifference(hireDate, nowDate) < 0) Then
                  Msgbox "No!"
                  Continue = False
            End If
      End If
      
End Sub

... You'll also need these support functions.  Add them to your library!

Function DtGetDaysDifference(dtSource1 As NotesDateTime, dtSource2 As NotesDateTime) As Integer
      
      '/**
      ' * Determines the number of days between the two dates.
      ' * @param dtSource1 The first date.
      ' * @param dtSource2 The second date.
      ' * @return The number of days by which the two dates differ.  If Source1 is before Source2 the return value will be positive.  If Source1 is after Source2 the return value will be negative.  If the days are the same, the return value will be zero.
      ' */
      
      Const SECONDS_PER_DAY = 86400
      Dim dtDate1 As New NotesDateTime(dtSource1.DateOnly)
      Dim dtDate2 As New NotesDateTime(dtSource2.DateOnly)
      DtGetDaysDifference = (dtDate2.Timedifference(dtDate1) / SECONDS_PER_DAY)
      
End Function

Function DtGetLsDateTime(value As Variant) As NotesDateTime
      
      '/**
      ' * Creates a date time object from a LotusScript value.
      ' * @param value A LotusScript date/time value.
      ' * @return The value contained in a NotesDateTime object.
      ' */
      
      Set DtGetLsDateTime = New NotesDateTime("")
      If Isdate(value) Then DtGetLsDateTime.LSLocalTime = value
      
End Function

Commented:
Isn't that a little complicated for something where you can just compare values directly?
Wow, I can't believe that I never tested this myself.  I always assumed that direct manipulation of DateTime was not allowed in LotusScript.  Are you sure that this is officially supported?

Commented:
A date field returns an array of variants each of which is a Lotusscript time value. You can get at the underlying, more-complex Notes date value using the DateTimeValue property of the item, but it is normally so much easier to just treat the field as an array of scalars, same as any other regular field vale in Notes translates into a Lotusscript array of scalars.

Yes, definitely supported.  Go to designer help, Values property (of NotesItem). First table shows the data type for reading.  Includes a row:
Date-time or range of date-time values      Array of Variant of type DATE. A scalar value is a 1-element array.

And of course, VALUES maps into the late-binding document dynamic property for field names.


Hmm, I think I just saved you a lot of coding. If it is any indication, my 7 lines is the same as your 18.  Enjoy!
Ray PadillaIT  Director

Author

Commented:
First thanks for the responses but maybe I need to explain the reason for this....I have an application that HR uses for Employee information, I then grab certain non sensitive data and using an agent populate an Employee lookup (sort of a nab) the is veiwable by all our employees but it is not editable by anyone. I also use this Employee lookup as my feed for our other applications, i.e. I populate manager fields, department information,  etc.  THe problem I am facing is that HR adds the new hires to the Employee Information system (EIS) once they have accepted the position which is proper procedure, this allows the manager to submit request for equipment phone lines etc. but what happens is that employees will start trying to contact this person before they are in the building, which sometimes can be up to 2 weeks. So what I want if for the persons name to show up on the view but when somebody tries to open that particular record if the hire date has not yet passed they get a pop up displaying the date that person is to be in the building, which is the hire date.....hope this better explains my need. I was looking at the Queryopenview as the trigger for the pop up and the lock on the record....
Ray PadillaIT  Director

Author

Commented:
This is my script:
Sub Queryopendocument(Source As Notesuiview, Continue As Variant)
      
      Dim docs As NotesDocumentCollection
      Dim doc As NotesDocument
      Dim item As NotesItem
      
      Set docs = Source.Documents
      Set doc = docs.GetFirstDocument
      
      TomorrowDate = Today + 1
      HireDateValue = Source.Document.HireDate(0)
      If HireDateValue >= TomorrowDate Then
            Messagebox " This Employee Document will not be available until " & Cstr(HireDateValue)
            continue=False
      End If
      
End Sub

getting the error NOT A MEMBER Document on line 11
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Ray PadillaIT  Director

Author

Commented:
Thanks Bill and Qwa
That worked perfectly anyway I can get rid of the time and just display the date?
Look at the Format function.  It does the same thing as Cstr, but it has options.
Ray PadillaIT  Director

Author

Commented:
can you please explain?!?! are you saying to call the function instead of the Cstr in order to remove the time ?
Ray PadillaIT  Director

Author

Commented:
never mind thanks guys I got it changed the Cstr to Cdat and it worked !!! thanks again!
Cdat worked because it converted the DateTime to a Date, then the concatenation operator (&) converted that Date back to a String.  It's more efficient to use the Format function because only one conversion takes place.  Plus, you can format the date any way you want.

Messagebox " This Employee Document will not be available until " & Format(HireDateValue, "MMDDYYYY")

Commented:
Thi is more complicated than you need.  A warning column in the view owuld have proably sufficed and been a lot simpler.  FOr that matter, you could have put the hire date in the view, and used selective coloring to highlight the value when it is in the future.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.