[AccessD] Creating HTML file

Doug Murphy dw-murphy at cox.net
Fri Jul 27 18:27:21 CDT 2007


Hi Steve,

I do this quite a bit.  What I do is to create the skeleton of the html page
in my html editor, Dream Weaver.  I embed tags in the page where I want
Access to insert specific current data, like the date the page was updated.
The tag I use looks like an html comment, i.e., "<!-- Last Updated -->"  The
text part of the tag allows me to insert in that spot. I then use this file
as a template.  When I want to create a page I open the template file from
Access and open a new file to output to.  The routine then iterates trough
the template file, inserting data where required and then outputting the
lines to the output file.  For this application the majority of the data
goes into a table so I put a tag where the table goes and input data from a
record set into the table with the appropriate table, table row and cell
tags.  I got the basic routine from some one on this list several years ago.


The routine I use follows.

---Code follows---
Sub sWriteHTMLReport(strTemplateFile As String, strFinalReportFile As
String, rst As DAO.Recordset)
    Dim intFFIn As Integer
    Dim intFFOut As Integer
    Dim strLine As String
    Dim iCount As Integer
    Dim sField As String
    
    'Get Free File Handle for working with template.html
    intFFIn = FreeFile
    'Open temp.htm for read only using Free File handle
    Open strTemplateFile For Input Access Read As #intFFIn
    'Get Free File Handle for working with report.html ' report.html is the
output file
    intFFOut = FreeFile
    'Open report.html for writing
    Open strFinalReportFile For Output Access Write As #intFFOut
    'Do
    'Insert update date
    Do
    '   Read Line from template.html
        Input #intFFIn, strLine
    '   If Line = <!-- Last Updated --> then date goes here
        If strLine = "<!-- Last Updated -->" Then
    '       Exit Loop
            strLine = "<I>(Last Updated: " & Format(Date, "mmmm dd, yyyy") &
")</I>"
    '   If Line = <!-- Data Goes Here -> then
        ElseIf strLine = "<!-- Data Goes Here -->" Then
    '       Exit Loop
            Exit Do
    '   End If
        End If
    '   Write Line to report.html
        Print #intFFOut, strLine
    'Loop While Not EOF ' just to make sure that you don't get errors if you
    Loop While Not (EOF(intFFIn))
    'forget to add the comment
    
    'open Data recordset
    'Recordset passed instead of opening her to make it a bit more generic
    
    'Do
    Do
    '   Build OutPutString by iterating through the field collection and
adding fields to the string
        strLine = "<TR>"
        For iCount = 0 To rst.Fields.Count - 1
            'Check to see if the field is null and if so put in a Line break
            If IsNull(rst.Fields(iCount).Value) Then
                sField = "<BR>"
            Else
                sField = rst.Fields(iCount).Value
            End If
            strLine = strLine & "<TD>" & sField & "</TD>"
        Next iCount
        'Trim off the last <TD>
        strLine = Mid(strLine, 1, Len(strLine) - 5)
        'Add the end of row tag
        strLine = strLine & "</TD></TR>"
    '   write output string to report.html
        Print #intFFOut, strLine
    '   Move to Next Record
        rst.MoveNext
    'loop while there are still records in the recordset
    Loop While Not (rst.EOF)
    
    'do while not End Of template.html (the condition needs to go at the top
in case you forgot to add the comment)
    Do While Not (EOF(intFFIn))
    '   Read Line from template.html
        Input #intFFIn, strLine
    '   Write Line to report.html
        Print #intFFOut, strLine
    'Loop
    Loop
    '
    'Close Report.html
    'Close template.html
    Close
End Sub
----End code----------

If you just want to insert a few pieces of data in specific locations you
could just copy the template file into a string and use replace to locate
the tags and insert the values.  After inserting all data you would save the
string to a new file.

Hope this helps. I don't remember who helped me out, but am glad to share.

Doug

-----Original Message-----
From: accessd-bounces at databaseadvisors.com
[mailto:accessd-bounces at databaseadvisors.com] On Behalf Of Steve Schapel
Sent: Friday, July 27, 2007 2:04 PM
To: Access Developers discussion and problem solving
Subject: [AccessD] Creating HTML file

Hi.  I am just about to do something I have never tried before.  I need to
create a static web page from Access data.  Using OutputTo from a report has
too many problems.  Using DoCmd.TransferText acExportHTML doesn't give me
the formatting flexibility that I need.  Therefore I've decided to try to
loop through a recordset and build the html file in code.  Main problem
being that my html skills are minimal.  Has anyone done this type of thing?
Any tips?  Thanks.

Regards
Steve
--
AccessD mailing list
AccessD at databaseadvisors.com
http://databaseadvisors.com/mailman/listinfo/accessd
Website: http://www.databaseadvisors.com




More information about the AccessD mailing list