home *** CD-ROM | disk | FTP | other *** search
- Attribute VB_Name = "Recordset2HTML"
- '-----------------------------------------------------------------------------------
- ' Author: Edwin Vermeer, vermeer@beursmonitor.com, www.beursmonitor.com
- '-----------------------------------------------------------------------------------
- ' Functions and procedures in this module:
- ' Sub CreateHTML : This procedure uses a recordset and a template file to create one or more HTML pages
- ' Function CreateHTMLpage : This function will return a HTML text base on a recordset and a template
- ' Funttion ReplaceFieldTags : This function replace all tags with the values of the current record in a recordset
- ' Sub test : This procedure is testing the above functions
- '-----------------------------------------------------------------------------------
- Option Explicit
- Dim myCounter As Long
-
-
-
-
- Public Sub CreateHTML(rs As Recordset, Template As String, TargetDir As String, FileGrouping As String)
- '-----------------------------------------------------------------------------------
- ' Author: Edwin Vermeer, vermeer@beursmonitor.com, www.beursmonitor.com
- '-----------------------------------------------------------------------------------
- ' This subroutine creates one or more HTML files base on a recordset and a
- ' template file. All files are stored in the targetdir. For each different
- ' value in the field Filegrouping in the recordset a file will be created.
- '-----------------------------------------------------------------------------------
- ' rs : Can be any reccordset. The sorting order must be set to the filegrouping and page groupings
- ' Template : The filename (including path) of the template file
- ' TargetDir : The location where the HTML file(s) will be created
- ' FileGrouping : A file will be created for each value int the field with this name.
- '-----------------------------------------------------------------------------------
- ' The next line is showing a (shortest possible) template with all functionality
- ' a<%b%><%GROUP c%>d<%e%><%GROUP DetailSection%>f<%g%><%ENDGROUP%>h<%i%><%ENDGROUP%>j<%k%>
- ' The result of this template can be described as:
- ' the page header: a (value of field b)
- ' and now for each value in c: d (value of field e)
- ' then for each reccord: f (value of field g)
- ' the footer for each value in c: h (value of field i)
- ' then the page footer: j (value of field) k
- '-----------------------------------------------------------------------------------
- Dim FileLine As String
- Dim TemplateFile As String
- Dim newfile As String
- Dim FileGroupingValue As String
- Dim i As Long
- Dim j As Long
-
- If rs.EOF Then Exit Sub
-
- ' Read the template into the memmory
- Open Template For Input As #1
- While Not EOF(1)
- Line Input #1, FileLine
- TemplateFile = TemplateFile & FileLine & vbCrLf
- Wend
- Close #1
-
- ' In case we want to display a progress bar/counter
- rs.MoveLast
- myCounter = rs.RecordCount
- rs.MoveFirst
-
- ' OK, now we want to process all reccords
- While Not rs.EOF
- FileGroupingValue = rs(FileGrouping)
- newfile = CreateHTMLpage(rs, TemplateFile, FileGrouping)
-
- ' Write the HTML file
- Open TargetDir & FileGroupingValue & ".html" For Output As #1
- Print #1, newfile
- Close #1
- If Not rs.EOF Then FileGroupingValue = rs(FileGrouping)
- Wend
- myCounter = 0
-
- End Sub
-
-
-
-
- Public Function CreateHTMLpage(rs As Recordset, TemplateFile As String, FileGrouping As String) As String
- '-----------------------------------------------------------------------------------
- ' Author: Edwin Vermeer, vermeer@beursmonitor.com, www.beursmonitor.com
- '-----------------------------------------------------------------------------------
- ' This function is called from the CreateHTML routine but can also be runned seperatly
- ' Based on the recordset rs and the TemplateFile it will return a string.
- ' This function whill start at the current reccord and stop as soon as the field
- ' with the nime FileGrouping is changed.
- '-----------------------------------------------------------------------------------
- ' rs : Can be any reccordset. The sorting order must be set to the filegrouping and page groupings
- ' TemplateTile : The complete template as a string.
- ' FileGrouping : A file will be created for each value int the field with this name.
- '-----------------------------------------------------------------------------------
- Dim GroupingBy() As String
- Dim GroupingValue() As String
- Dim GroupingHeader() As String
- Dim GroupingFooter() As String
- Dim FileGroupingValue As String
- Dim WorkingOnFile As Boolean
- Dim CurrentGroupingLevel As Long
- Dim SaveCurrentGroupingLevel As Long
- Dim i As Long
- Dim newfile As String
-
- ' First we need to preprocess the template into easy to handle arrays.
- '---------------------------------------------------------------------
- ' put all the group headers into an array. The last cell will contain the detail section plus all footers.
- GroupingHeader = Split(TemplateFile, "<%GROUP ")
- ' use the last header cell and put all the footers into an array. the last cell will contain the detail section
- GroupingFooter = Split(GroupingHeader(UBound(GroupingHeader)), "<%ENDGROUP%>")
- ' The group names are still in header array. Extract them to a sepperate array
- ReDim GroupingBy(UBound(GroupingHeader))
- For i = 0 To UBound(GroupingHeader) - 2
- ' First put the grouping by name into a sepperate array
- GroupingBy(i + 1) = Left(GroupingHeader(i + 1), InStr(1, GroupingHeader(i + 1), "%>") - 1)
- ' Then remove this grouping by name from the header array
- GroupingHeader(i + 1) = Mid(GroupingHeader(i + 1), InStr(1, GroupingHeader(i + 1), "%>") + 2)
- Next i
- ' If we do have grouping then the grouping by name of the last group will be in the footer instead of the header.
- If UBound(GroupingHeader) > 0 Then
- ' GroupingBy(UBound(GroupingHeader) - 1) = Left(GroupingFooter(0), InStr(1, GroupingFooter(0), "%>") - 1)
- GroupingFooter(0) = Mid(GroupingFooter(0), InStr(1, GroupingFooter(0), "%>") + 2)
- End If
- ' Just clear the cell that we don't need (not realy neccessery)
- GroupingHeader(UBound(GroupingHeader)) = ""
-
- ' Remember the current grouping by values
- ReDim GroupingValue(UBound(GroupingHeader))
- For i = 1 To UBound(GroupingBy) - 1
- GroupingValue(i) = rs(GroupingBy(i))
- Next
- FileGroupingValue = rs(FileGrouping)
-
- ' Now create the HTML file
- WorkingOnFile = True
- newfile = ReplaceFieldTags(GroupingHeader(0), rs)
- CurrentGroupingLevel = 1
- While WorkingOnFile And Not rs.EOF
- ' Put in the grouping headders if needed
- While CurrentGroupingLevel < UBound(GroupingBy)
- newfile = newfile & ReplaceFieldTags(GroupingHeader(CurrentGroupingLevel), rs)
- CurrentGroupingLevel = CurrentGroupingLevel + 1
- Wend
-
- ' add the detail section to the file
- newfile = newfile & ReplaceFieldTags(GroupingFooter(0), rs)
-
- ' test if we need footers
- rs.MoveNext
- If rs.EOF Then
- CurrentGroupingLevel = 1
- Else
- While rs(GroupingBy(CurrentGroupingLevel - 1)) <> GroupingValue(CurrentGroupingLevel - 1)
- GroupingValue(CurrentGroupingLevel - 1) = rs(GroupingBy(CurrentGroupingLevel - 1))
- CurrentGroupingLevel = CurrentGroupingLevel - 1
- If CurrentGroupingLevel = 1 Then GoTo JumpOut
- Wend
- JumpOut:
- End If
- rs.MovePrevious
-
- ' Put in the grouping footers if needed
- SaveCurrentGroupingLevel = CurrentGroupingLevel
- While CurrentGroupingLevel < UBound(GroupingBy)
- newfile = newfile & ReplaceFieldTags(GroupingFooter(CurrentGroupingLevel), rs)
- CurrentGroupingLevel = CurrentGroupingLevel + 1
- Wend
- CurrentGroupingLevel = SaveCurrentGroupingLevel
-
- rs.MoveNext
- myCounter = myCounter - 1
- DoEvents
- If Not rs.EOF Then If FileGroupingValue <> rs(FileGrouping) Then WorkingOnFile = False
- Wend
- rs.MovePrevious
- newfile = newfile & ReplaceFieldTags(GroupingFooter(UBound(GroupingFooter)), rs)
- rs.MoveNext
-
- CreateHTMLpage = newfile
-
- End Function
-
-
-
-
- Public Function ReplaceFieldTags(strText As String, rs As Recordset) As String
- '-----------------------------------------------------------------------------------
- ' Author: Edwin Vermeer, vermeer@beursmonitor.com, www.beursmonitor.com
- '-----------------------------------------------------------------------------------
- ' This function will replace all tags in strText that are formated like
- ' <%fieldname%> with the value of that fieldname. This will be done for the
- ' current reccord in the recordset rs
- '-----------------------------------------------------------------------------------
- ' rs : The recordset where the current reccord will be used
- ' strText : The text field where all the tags will be replaced
- '-----------------------------------------------------------------------------------
- Dim i As Long
- Dim j As Long
-
- ReplaceFieldTags = strText
- For i = 0 To rs.Fields.Count - 1
- j = InStr(1, ReplaceFieldTags, "<%" & rs(i).Name & "%>")
- While j > 0
- ReplaceFieldTags = Left(ReplaceFieldTags, j - 1) & rs(i) & Mid(ReplaceFieldTags, j + 4 + Len(rs(i).Name))
- j = InStr(1, ReplaceFieldTags, "<%" & rs(i).Name & "%>")
- Wend
- Next i
-
- ' Now we will replace some constant tags
- ReplaceFieldTags = ReplaceTags(ReplaceFieldTags, "HomePage", "www.beursmonitor.com")
- ReplaceFieldTags = ReplaceTags(ReplaceFieldTags, "eMail", "vermeer@beursmonitor.com")
- ReplaceFieldTags = ReplaceTags(ReplaceFieldTags, "App.Title", App.Title)
- ReplaceFieldTags = ReplaceTags(ReplaceFieldTags, "App.Version", App.Major & "." & App.Minor)
- ReplaceFieldTags = ReplaceTags(ReplaceFieldTags, "App.Comments", App.Comments)
- ReplaceFieldTags = ReplaceTags(ReplaceFieldTags, "App.CompanyName", App.CompanyName)
- ReplaceFieldTags = ReplaceTags(ReplaceFieldTags, "App.FileDescription", App.FileDescription)
- ReplaceFieldTags = ReplaceTags(ReplaceFieldTags, "App.LegalCopyright", App.LegalCopyright)
- ReplaceFieldTags = ReplaceTags(ReplaceFieldTags, "App.LegalTrademarks", App.LegalTrademarks)
- ReplaceFieldTags = ReplaceTags(ReplaceFieldTags, "App.ProductName", App.ProductName)
- ReplaceFieldTags = ReplaceTags(ReplaceFieldTags, "Date", Date)
- ReplaceFieldTags = ReplaceTags(ReplaceFieldTags, "Time", Time)
-
- End Function
-
-
- Public Function ReplaceTags(strText As String, strTag As String, strReplaceWith) As String
- '-----------------------------------------------------------------------------------
- ' Author: Edwin Vermeer, vermeer@beursmonitor.com, www.beursmonitor.com
- '-----------------------------------------------------------------------------------
- ' This function will replace all tags in strText with the name strTag that are
- ' formated like <%tagname%> with the value of strReplaceWith.
- '-----------------------------------------------------------------------------------
- ' strTag : The name of the tag.
- ' strText : The text field where all the tags will be replaced.
- ' strReplaceWith : The text that will be put inplace of the tag
- '-----------------------------------------------------------------------------------
- Dim j As Long
-
- ReplaceTags = strText
- j = InStr(1, ReplaceTags, "<%" & strTag & "%>")
- While j > 0
- ReplaceTags = Left(ReplaceTags, j - 1) & strReplaceWith & Mid(ReplaceTags, j + 4 + Len(strTag))
- j = InStr(1, ReplaceTags, "<%" & strTag & "%>")
- Wend
-
- End Function
-
- Public Sub test()
- '-----------------------------------------------------------------------------------
- ' Author: Edwin Vermeer, vermeer@beursmonitor.com, www.beursmonitor.com
- '-----------------------------------------------------------------------------------
- ' This routine whill test the functioning of the CreateHTML procedure
- '-----------------------------------------------------------------------------------
- Dim ws As Workspace
- Dim db As Database
- Dim rs As Recordset
-
- Set ws = CreateWorkspace("", "admin", "")
- Set db = ws.OpenDatabase(App.Path & "\BeursMonitor.mdb")
- Set rs = db.OpenRecordset("Select * from HuidigeKoers Where (((HuidigeKoers.Tijd) > DateAdd('d', -7, Now()))) Order by Bron, Groep, Fonds")
- CreateHTML rs, "c:\temp\template.htt", "c:\temp\", "Bron"
-
- End Sub
-
-