FUNCTION CompileTemplate(strQuery, strTemplate)
   DIM arrFieldDataArray
   DIM arrSlotArray
   DIM arrParamArray
   DIM intMaxFields
   DIM intMaxItems
   DIM intMinFields
   DIM intMinItems
   DIM intMinSlots
   DIM intMaxSlots
   DIM intMinRecords
   DIM intMaxRecords
   DIM intSlotEndIndex
   DIM intTotalIndex
   DIM intSlotID
   DIM intFieldLength
   DIM intMaxParams
   DIM blnParser
   DIM strSlotValue
   DIM strRemaining
   DIM strCurrentString
   DIM strSlot
   DIM strCompiled
   DIM strResult
   DIM rst
   
   set rst = CreateObject("ADODB.recordset")
   rst.open ObjConn, strQuery, adOpenForwardOnly

   IF NOT rst.EOF AND NOT rst.BOF THEN
      arrFieldDataArray = rst.getrows
   ELSE
      Exit Function
   END IF

   arrSlotArray = Split(strTemplate, "[")
   intMaxSlots = UBound(arrSlotArray)
   intMaxRecords = UBound(arrFieldDataArray, 2) + 1
   intMaxFields = UBound(arrFieldDataArray)

   FOR intMinRecords = 0 TO intMaxRecords - 1
      strCompiled = arrSlotArray(0)

      FOR intMinSlots = 1 TO intMaxSlots
         strCurrentString = arrSlotArray(intMinSlots)
         intSlotEndIndex = Instr(strCurrentString, "]")
         intTotalIndex = Len(strCurrentString)
      
         strSlot = Left(strCurrentString, intSlotEndIndex - 1)
         strRemaining = Mid(strCurrentString, intSlotEndIndex + 1,
                            intTotalIndex - intSlotEndIndex + 1)

         arrParamArray = Split(strSlot, "|")
         intMaxParams = UBound(arrParamArray)
         intSlotID = arrParamArray(0)

         IF CInt(intSlotID - 1) <= CInt(intMaxFields) THEN
            strSlotValue = arrFieldDataArray(intSlotID - 1, 
                           intMinRecords)
         ELSE
            strSlotValue = ""
         END IF
         
         IF intMaxParams >= 1 THEN
            intFieldLength = arrParamArray(1)
            IF intFieldLength > 0 THEN
               strSlotValue = Cut(strSlotValue, intFieldLength)
            END IF 
         END IF
         IF intMaxParams >= 2 THEN
            blnParser = arrParamArray(2)
            IF blnParser THEN
               strSlotValue = RemoveHTML(strSlotValue)
            END IF
         END IF

         strCompiled = strCompiled & strSlotValue & strRemaining
      NEXT
      strResult = strResult & strCompiled
   NEXT
   CompileTemplate = strResult
END FUNCTION