Examples

 









Lotus Notes Calendaring with VB / VBA.

Part 3 - Examples and More

Page: << 1 2 3

In this section we will go over the ParseTimeArray function. I will also provide two example subs which use the functions in part 1 & 2.

The purpose of ParseTimeArray is to create a binary representation of a user's free time for the selected day. It requires both the output array and array length of GetUsedTime to work properly. I use this in my app in conjunction with a drop down box to flag the times that a counselor has appointments scheduled by putting a mark before the time. There is also a feature that allows the scheduler to select a time that is marked and return the binary representations of the other counselor's day so that they can all be easily compared with the day and time the secretary is trying to schedule for. It then returns a list of counselors who do have that time block free. If you are familiar with binary and VB, this function should make perfect sense. If not, I suggest you look for a good binary tutorial (there are millions) online. You will be glad you did. Trust me, you can't be a good programmer until you can effectively use binary!
Public Function ParseTimeArray(GetUsedTimeArray As Variant, ArrLen As Integer) As Long
Dim lngOut As Long, datTmp As Date, strDat As String, i As Integer, intBlock As Integer, intLen As Integer
'GetUsedTimeArray(x, 1) = Start Time
'GetUsedTimeArray(x, 2) = Length of appointment
For i = 1 To ArrLen
datTmp = GetUsedTimeArray(i, 1)
strDat = FormatDateTime(datTmp, vbLongTime)
intBlock = GetBlockByTime(strDat)
intLen = GetUsedTimeArray(i, 2)
'Set bits here
Do Until intLen = 0
lngOut = lngOut Or Digit_Weight(intBlock)
intLen = intLen - 30
intBlock = intBlock + 1
Loop
Next i
ParseTimeArray = lngOut
End Function

'Calculates the binary weight of a number in a list
'This is used to fit any combination of selections in a list into a single field by setting each record to on or off
Public Function Digit_Weight(ByVal Digit As Integer) As Long
Digit_Weight = ((2 ^ Digit) / 2)
End Function

The following function: GetBlockByTime() is required by ParseTimeArray and uses the table which follows it called tbl_Timeblocks. Since the day is divided up into half hour blocks, this function will assume that if someone is busy for part of the block, they are busy for the entire block. This makes it so that the code doesn't crap out when there is an appointment scheduled for 3:15 or 11:45.

Public Function GetBlockByTime(ThisTime) As Integer
Dim strTime As String, intPos As Integer
Dim strQuery As String, rsResult As ADODB.Recordset

strTime = CStr(ThisTime)
intPos = InStr(1, strTime, ":")
If intPos = 2 Then
strTime = Left(strTime, 3)
ElseIf intPos = 3 Then
strTime = Left(strTime, 4)
End If
Select Case Right(strTime, 1)
Case 1, 2
strTime = Left(strTime, intPos) & 0
Case 4, 5
strTime = Left(strTime, intPos) & 3
End Select

strQuery = "SELECT BlockID FROM tbl_Timeblocks WHERE StartTime LIKE '" & strTime & "%'"
Set rsResult = SqlGet(strQuery)

If Not rsResult.EOF Then
GetBlockByTime = rsResult("BlockID")
Else
GetBlockByTime = 0
End If
End Function

tbl_TimeBlocks:

BlockID StartTime
0 (null)
1 8:00 AM
2 8:30 AM
3 9:00 AM
4 9:30 AM
5 10:00 AM
6 10:30 AM
7 11:00 AM
8 11:30 AM
9 12:00 PM
10 12:30 PM
11 1:00 PM
12 1:30 PM
13 2:00 PM
14 2:30 PM
15 3:00 PM
16 3:30 PM
17 4:00 PM
18 4:30 PM
19 5:00 PM

This will create three test appointments for the user, on the day you specify. You can then use test2 to retrieve them. If this works properly, you should see three "True"s in the Immediate window.

Public Sub Test1(UserName As String, UserPass As String, strDate As String)
If strUserPass = "" Then Exit Sub
Debug.Print SendNewNotesAppointment(UserName, strUserPass, "Tester1", "TEST1 - YOU CAN DELETE THIS", strDate, "9:00AM", 30)
Debug.Print SendNewNotesAppointment(UserName, strUserPass, "Tester2", "TEST2 - YOU CAN DELETE THIS", strDate, "10:30AM", 60)
Debug.Print SendNewNotesAppointment(UserName, strUserPass, "Tester3", "TEST3 - YOU CAN DELETE THIS", strDate, "3:00PM", 120)
End Sub

This little gem grabs all the specified day's appointments for the specified user. It writes the Parsed Time Array binary string to the Immediate window as well as a summary of the day's appointments. The function at the bottom Dec_to_Bin is needed to convert the long integer datatype to a string of 1's and 0's that represent the number in binary form.

Public Sub Test2(User As String, strUserPass as stringThisDate As String)
Dim tmp
Dim tmplen As Integer
Dim i As Integer
If strUserPass = "" Then Exit Sub
tmp = GetUsedTime(User, strUserPass, ThisDate, tmplen)
Debug.Print "************************************************************"
Debug.Print "Parsed TimeArray: " & Dec_to_Bin(ParseTimeArray(tmp, tmplen))
For i = 1 To tmplen
Debug.Print tmp(i, 3) & "(" & tmp(i, 1) & "): " & tmp(i, 4) & " Duration: " & tmp(i, 2) & " Mins"
Next i
Debug.Print "************************************************************"
Debug.Print " "
End Sub

'Converts a decimal number to a binary string of 1's and 0's
Public Function Dec_to_Bin(DecNum) As String
Dim lngConvert As Long
Dim strConvert As String
Dim intBinDigits As Integer
Dim i As Integer

lngConvert = CLng(DecNum)

Do Until Digit_Weight(intBinDigits + 1) > lngConvert
intBinDigits = intBinDigits + 1
Loop

For i = intBinDigits To 1 Step -1
If lngConvert >= Digit_Weight(i) Then
lngConvert = lngConvert - Digit_Weight(i)
strConvert = strConvert + "1"
Else
strConvert = strConvert + "0"
End If
Next i

Do Until Len(strConvert) > 20
strConvert = "0" + strConvert
Loop

Dec_to_Bin = strConvert

End Function

This is what the output of test2 should look like if used on the same day with the same user as you specified in test1.

Returns this in the Immediate window:

************************************************************
Parsed TimeArray: 000111100000001100100
Appointment(4/15/2005 9:00:00 AM): Tester1 Duration: 30 Mins
Appointment(4/15/2005 10:30:00 AM): Tester2 Duration: 60 Mins
Appointment(4/15/2005 3:00:00 PM): Tester3 Duration: 120 Mins
************************************************************

Well, that's all folks! I hope you found this article useful. If you end up borrowing this code, I only ask three things of you out you for the hours I have put into writing this code.

1) If you improve it in any way, please send me the improvements. I will be sure to give you credit for your work!

2) Drop me a line from the Contact Us page giving me a quick description of what you are using it for so that I can put some user comments on the bottom here (if you don't want it posted on here, send it anyway and just specify that you don't want it posted).

3) At the top of the module you are putting this code into, please put the following:

'Lotus Notes Calendaring
'Developed by Joe Korzeniewski of www.vortexwd.net 'For an explanation of this code please visit www.vortexwd.net/Lotusnotescalendaring1.asp

Copyright (C) Vortex Web Development, 2005-2007    Visit my blog