'------------------------------------------------- Create_HtmlCalendar
Function Create_HtmlCalendar( _
psQueryOrSQL As String _
, Optional ByVal psPathFile As String = "" _
) As String
'create calendar report for a web browser
'160523 ... 170723 strive4peace
'CALLS
'SetTheColors
'StartTime
'EndTime
'WriteHTMLheader
'WriteHTMLfooter
'DoesFieldExistInRs
'GetFilenameFromPathFile
'PARAMETERS
'query or SQL statement needs to have values (for desired month) and be sorted by CalDate
' psPathFile is the path\filename.ext to create with the results
' _yymmdd-hhnn.HTM added if no extension specified
' if not specified, will be called Calendar... in CurrentProject.Path\Calendar\
'QUERY FIELDS
' CalDate - calendar date - REQUIRED
' CalTitle - calendar title - OPTIONAL - read from first record
' Text1 - first text in cell - OPTIONAL
' Text2 - second text in cell - OPTIONAL
' Text3 - third text in cell - OPTIONAL
'if no data is written in the cell, the day number in the cell
'will still be colored if there is a record
'RETURNS
' path\filename of calendar that was generated
' if not successful: ""
'EXAMPLEs:
' call Create_HtmlCalendar("qCalendar")
' MsgBox Create_HtmlCalendar("qCalendar_NoCellText")
' Application.FollowHyperlink Create_HtmlCalendar("qCalendarText1Only")
'assume calendar was not successful
Create_HtmlCalendar = ""
'set up error handler
On Error GoTo Proc_Err
'set StartTime to time how long it takes
Call StartTime
'dimension variables
Dim db As DAO.Database _
, rs As DAO.Recordset _
, qdf As DAO.QueryDef
Dim iFileNumber As Integer _
, nNumDaysWithData As Integer _
, iCol As Integer _
, iColWidth As Integer _
, iColor As Integer _
, iDOW1 As Integer _
, iDOW2 As Integer _
, i As Integer _
, nDate1 As Date _
, nDate2 As Date _
, ndate As Date _
, sPath As String _
, sFilename As String _
, sParameter As String _
, sColor1 As String _
, sColor2 As String _
, sColor3 As String _
, sCalTitle As String _
, vCriteria As Variant _
, boo1 As Boolean _
, boo2 As Boolean _
, boo3 As Boolean _
, booDayData As Boolean
sPath = ""
sFilename = ""
'Call Set The Colors
Call SetTheColors
nNumDaysWithData = 0
iColWidth = 168 '150 for narrower '150 for narrower
vCriteria = Null
Set db = CurrentDb
'see query with parameters was passed
If Left(psQueryOrSQL, 7) <> "SELECT " Then
If db.QueryDefs(psQueryOrSQL).Parameters.Count > 0 Then
Set qdf = db.QueryDefs(psQueryOrSQL)
With qdf
For i = 1 To .Parameters.Count
With .Parameters(i - 1)
.Value = InputBox(.Name, "Enter Query Parameter")
'criteria is written on Calendar report below main title
vCriteria = (vCriteria + ", ") & .Value
End With 'Parameter 'Parameter
Next i
End With 'qdf 'qdf
Set rs = qdf.OpenRecordset(dbOpenDynaset, dbSeeChanges)
End If
End If
If rs Is Nothing Then
Set rs = db.OpenRecordset(psQueryOrSQL, dbOpenDynaset, dbSeeChanges)
End If
If Not DoesFieldExistInRs(rs, "CalDate") Then
MsgBox "CalDate is Missing from the specified recordset" _
& vbCrLf & vbCrLf & "Calendar cannot be generated " _
& "without a field called 'CalDate'" _
& vbCrLf & vbCrLf & "For instructions, Read comments for " _
& "Create_HtmlCalendar" _
, , "Field named 'CalDate' is Required"
GoTo Proc_Exit
End If
With rs
If .EOF Then
MsgBox "Calendar has no dates in specified range", , "No Data"
.Close
Set rs = Nothing
GoTo Proc_Exit
End If
.MoveFirst
booDayData = False
If DoesFieldExistInRs(rs, "CalTitle") Then
sCalTitle = rs!CalTitle
Else
sCalTitle = "Calendar"
End If
boo1 = DoesFieldExistInRs(rs, "Text1")
boo2 = DoesFieldExistInRs(rs, "Text2")
boo3 = DoesFieldExistInRs(rs, "Text3")
nDate1 = DateSerial(Year(!CalDate), Month(!CalDate), 1) ' 1st day of specified month/year ' 1st day of specified month/year
nDate2 = DateSerial(Year(!CalDate), Month(!CalDate) + 1, 0) ' last day of specified month/year ' last day of specified month/year
'------------- determine path\filename
'see if psPathFile was specified
If psPathFile = "" Then
'use calendar title for filename -- replace spaces with _ underscore
sFilename = Replace(sCalTitle, " ", "_")
Else
sFilename = GetFilenameFromPathFile(psPathFile)
If Len(sFilename) <> Len(psPathFile) Then
sPath = Left(psPathFile, Len(psPathFile) - Len(sFilename))
End If
End If
If sFilename = "" Then
'use calendar title for filename -- replace spaces with _ underscore
sFilename = Replace(sCalTitle, " ", "_")
End If
'if file has no extension, add Mon-Yr for calendar _
and _yymmdd-hhnn for when generated _
and .HTM
If Not InStr(sFilename, ".") > 0 Then
sFilename = sFilename _
& "_" & Format(nDate1, "mmm-yyyy") _
& "_" & Format(Now(), "yymmdd-hhnn") _
& ".htm"
End If
'use \CALENDAR under CurrentProject.Path if path not specified
If Trim(sPath) = "" Then
sPath = CurrentProject.Path & "\CALENDAR\"
If Dir(sPath, vbDirectory) = "" Then
MkDir sPath
End If
End If
psPathFile = sPath & sFilename
iDOW1 = Weekday(nDate1)
iDOW2 = Weekday(nDate2)
'*************************************************************************************
'-------------------------------------------------------------------- create web page
iFileNumber = FreeFile
On Error Resume Next
Close #iFileNumber
If Dir(psPathFile) <> "" Then
Kill psPathFile
' DoEvents: DoCmd.Hourglass True
End If
'-------------------------------------------------------------------- ~header
On Error GoTo Proc_Err
Open psPathFile For Output As #iFileNumber
Call WriteHTMLheader(iFileNumber _
, Format(!CalDate, "mmmm yyyy") _
, sCalTitle _
, Nz(vCriteria, "") _
)
'define table
Print #iFileNumber, "<BR>"
Print #iFileNumber, "<TABLE border=2 CELLPADDING=2 WIDTH = " & 7 * iColWidth & ">"
Print #iFileNumber, Space(2) & "<TR>"
'print days of week
For iCol = 1 To 7
Print #iFileNumber, Space(4) & "<td ALIGN=center VALIGN=center WIDTH = " & iColWidth & ">"
Print #iFileNumber, Space(6) & "<font size = 2>"
Print #iFileNumber, Space(8) & Mid("SunMonTueWedThuFriSat", (iCol - 1) * 3 + 1, 3)
Print #iFileNumber, Space(6) & "</font>"
Print #iFileNumber, Space(4) & "</td>"
Next iCol
Print #iFileNumber, Space(2) & "</TR>"
'-------------------------------------------------------------------- ~detail
'print information on days
'start a new row
Print #iFileNumber, Space(2) & "<TR>"
'determine number of squares before the calendar starts
If iDOW1 <> 1 Then
Print #iFileNumber, Space(4) & "<td VALIGN=top WIDTH = " & iColWidth _
& " COLSPAN=" & (iDOW1 - 1) & ">"
Print #iFileNumber, Space(4) & "</td>"
End If
For ndate = nDate1 To nDate2
'see if we need to go to another row
If Weekday(ndate) = 1 And Day(ndate) <> 1 Then
'go to another row
Print #iFileNumber, Space(2) & "</TR>"
Print #iFileNumber, Space(2) & "<TR>"
End If
'-------------- print day number
Print #iFileNumber, Space(4) & "<td VALIGN=top WIDTH = " & iColWidth & ">"
Print #iFileNumber, Space(6) & "<p ALIGN=right>"
Print #iFileNumber, Space(8) & "<font size = 3"
iColor = 0 'so color will change to 1 for 1st entry on a date 'so color will change to 1 for 1st entry on a date
'see if there is any data for this day
If Not .EOF() Then
If !CalDate = ndate Then
booDayData = True
Print #iFileNumber, Space(10) & "color = blue><b>"
nNumDaysWithData = nNumDaysWithData + 1
Else
booDayData = False
Print #iFileNumber, Space(10) & "color = gray>"
End If
Else
Print #iFileNumber, Space(10) & "color = gray>"
End If
Print #iFileNumber, Space(10) & CStr(Day(ndate)) & " "
If booDayData Then
Print #iFileNumber, Space(8) & "</b>";
Else
Print #iFileNumber, Space(8);
End If
Print #iFileNumber, "</font>"
Print #iFileNumber, Space(6) & "</p>"
Print #iFileNumber, Space(6) & "<p ALIGN=left>"
Print #iFileNumber, Space(8) & "<font face=""Verdana"" size=1 color=green>"
If Not .EOF Then
If !CalDate = ndate Then
Do
If .EOF Then GoTo End_Of_Day
'switch colors
If iColor <> 1 Then
iColor = 1
Else
iColor = 2
End If
sColor1 = masColor(iColor, 1)
sColor2 = masColor(iColor, 2)
sColor3 = masColor(iColor, 3)
If boo1 Then
If Len(!Text1 & "") > 0 Then
Print #iFileNumber, Space(8) & "<font color=" & sColor1 & ">"
Print #iFileNumber, Space(10) & "<b> " & !Text1 & " </b> "
Print #iFileNumber, Space(8) & "</font>"
End If
End If
If boo2 Then
If Len(!Text2 & "") > 0 Then
Print #iFileNumber, Space(8) & "<font color=" & sColor2 & ">"
Print #iFileNumber, Space(10) & !Text2
Print #iFileNumber, Space(8) & "</font>"
End If
End If
If boo3 Then
If Len(!Text3 & "") > 0 Then
Print #iFileNumber, Space(8) & "<font color=" & sColor3 & ">"
Print #iFileNumber, Space(10) & !Text3
Print #iFileNumber, Space(8) & "</font>"
End If
End If
'print line break if there was any data
If boo1 Or boo2 Or boo3 Then Print #iFileNumber, Space(8) & "<BR/>"
If .EOF Then GoTo End_Of_Day
.MoveNext
If .EOF Then GoTo End_Of_Day
If !CalDate <> ndate Then GoTo End_Of_Day
Loop While !CalDate = ndate
End If '!CalDate = ndate '!CalDate = ndate
End If 'Not .EOF 'Not .EOF
End_Of_Day:
Print #iFileNumber, Space(8) & "</font>"
Print #iFileNumber, Space(6) & "</p>"
nextCol:
Print #iFileNumber, Space(4) & "</td>"
Next ndate
Print #iFileNumber, Space(2) & "</TR>"
Print #iFileNumber, "</table>"
.Close
End With 'rs 'rs
Set rs = Nothing
'--------------------------------------- number of days
Print #iFileNumber, " "
Print #iFileNumber, "<font face=""Verdana"" size=1 color=green>"
Print #iFileNumber, Space(4) _
& Format(nNumDaysWithData, "0") & " Days with information"
Print #iFileNumber, "</font>"
'--------------------------------------- date
Print #iFileNumber, "<font size=2 color = gray><i>"
Print #iFileNumber, Space(2) & " Generated "
Print #iFileNumber, Space(2) & Format(Now, "dd-mmm-yyyy, ddd, h:mm am/pm")
Print #iFileNumber, "</i></font>"
WriteHTMLfooter iFileNumber
Close #iFileNumber
DoEvents: DoCmd.Hourglass True
Create_HtmlCalendar_exit:
' On Error Resume Next
Debug.Print "Simple HTML calendar written by crystal (strive4peace)" & vbCrLf & vbCrLf _
& "Done generating " & vbCrLf & vbCrLf _
& psPathFile
Create_HtmlCalendar = psPathFile
Proc_Exit:
Call EndTime
On Error Resume Next
Set qdf = Nothing
If Not rs Is Nothing Then
rs.Close
Set rs = Nothing
End If
Set db = Nothing
Exit Function
Proc_Err:
MsgBox Err.Description, , _
"ERROR " & Err.Number _
& " Create_HtmlCalendar"
Resume Proc_Exit
Resume
End Function
|