Okay, Ponnfar, we're back:
The code that you asked me about contained two issues:
1. As I mentioned, since GetResponse is a variant (like a memory bank with a name), it doesn't take "arguments" in parenthesis.
2. In the code line where you used "DateDiff", which IS a function, the "arguments" must take a specific pattern of: unit switch, date formatted variant, date formatted variant. You only had one date formatted variant after the function.
I am very interested in you and other members of this forum becoming as proficient programmers as you want to be! To give you a little more help on the "datediff" function, I wrote a new and improved demo brain and it is attached.
However, I also ask that you eyeball the code and see if it makes sense to you. I added a LOT more annotation remarks than I usually do, so I would like your feedback.
The section of demo code follows below, and it is also in the new attached demo brain. The demo brain will compliment the user for responding quickly, make a remark in seconds-units for waits over 20 seconds, make a remark in minutes-units for waits over 2 minutes, and make a remark in hours-units for waits over 2 hours. I specifically wrote this for you to analyze and hopefully enjoy!
Here is the code section:
'-----------------------------
'BEGIN SPECIAL DEMO AREA FOR QUALIFIED DATEDIFF
'
'THIS IS A DEMONSTRATION OF THE "DATEDIFF" FUNCTION.
'THE FIRST ARGUMENT AFTER DATEDIFF
'TELLS THE FUNCTION WHAT UNITS TO USE.
'USE "S" FOR SECONDS, "D" FOR DAYS.
'THE SECOND AND THIRD ARGUMENTS MUST BE VARIANTS IN DATE FORMAT.
'(CERTAIN FUNCTIONS REQUIRE ARGUMENTS IN PARENTHESIS
'AND SEPARATED BY COMMAS TO WORK,
'AND "DATEDIFF" IS ONE OF THEM.)
'THE DATEDIFF FUNCTION RETURNS A NUMBER EQUAL TO THE
'DIFFERENCE BETWEEN THE SECOND AND THIRD ARGUMENTS PRESENTED TO IT.
'ALL POSSIBLE ARGUMENTS ARE LISTED IN THE "VBS IN A NUTSHELL" BOOK!
'First we prevent error messages by making sure that
'our variant "LastResponseTime" is actually
'in date format. We do this by
'testing whether the variant "LastResponseTime" is
'equal to the date format, using the function "IsDate."
'If the answer it true, we don't need to do anything.
'If the answer is false, we'll initialize the value
'of "LastResponseTime" to the computer's system time,
'which is the function "Now".
If IsDate(LastResponseTime) = False Then LastResponseTime = Now
'Next we'll obtain the number of seconds since the
'last exchange with the user. We do this using
'the "DateDiff" function and the required
'arguments that pertain to that function,
'and we create a variant named "HowLongBeen"
'to hold the number of seconds:
HowLongBeen = DateDiff("s", LastResponseTime, Now)
'Now we give Hal something to say as a "default":
GetResponse = "It's nice when you respond to me quickly."
'Next we do a little simple math to see if the wait
'has been greater than 20 seconds:
If HowLongBeen > 20 Then BeenLongTime = True
'Now we give Hal an impatient thing to say, if the variant
'we called "BeenLongTime" is "True"!
If BeenLongTime = True Then
GetResponse = "It has been " & HowLongBeen & " seconds since you spoke!"
End If
'Now, suppose that we wanted a special remark if the wait has been
'more than two minutes! Examine and analyze the code below:
'(Remember that the "Int" function returns an integer with
'any decimal places simply dropped, not rounded.)
HowManyMinutes = Int(HowLongBeen/60)
If HowManyMinutes > 1 Then
GetResponse = "It has been over " & HowManyMinutes & " minutes since you last spoke to me, and that is too long! "
End If
'Now, suppose that we wanted a special remark if the wait has been
'more than two hours! Examine and analyze the code below:
HowManyHours = Int(HowManyMinutes/60)
If HowManyHours > 1 Then
GetResponse = "It has been over " & HowManyHours & " hours since you last spoke to me, and that is way, way, way too long <UserName>!"
End If
'Finally we record the new time for the next round:
LastResponseTime = Now
'That completes this demo routine.
'------------------------------------------------------------------
Have a great day, and let me know if this makes this particular function and routine clear!
Sincerely,
Don
Download Attachment: Date Difference Qualified Demo.uhp12.35 KB