1
votes

I have been looking for some code that creates a calendar year and will take data from a table and place it in the corresponding date on the calendar. I found some code online (from an older version of access) that pretty much fit the bill, with some modifications it does exactly what I need it to do. Originally, the code pulled data from one table and it was set up to run on the current year. I use two queries, qr_SafetyCal and qr_SafetyCal2, to refine data from the one table. The first query prioritizes the data and eliminates multiple events on any given day. The second query uses the results from the first and specifies the year in the query criteria.

The code works flawlessly as long as I set the year criteria in the qr_SafetyCal2 and specify the first day, ex. 1/1/2017 (datStart) in the underlying code of the calendar year I want displayed.

After getting the code squared away I created a pop up form the user to select the year for the report but when I run the report I get the following error, Runtime Error 3061 too few parameters expected 1.

From what I have been able to research, I believe I changed the dynamic of the code when I referenced the form in the query criteria that the DAO Recordset Used. As I understand it, the criteria in the query is not passed to the rs and therefore needs to be declared in the code. What I can't figure out is how to declare the variables in the code through reference to the form. I hope that makes some sense to somebody, long explanation but hard to describe something you don't understand.

Below is all the code and you will see some things I've rem'd out that I have tried but did not work. Any help would be greatly appreciated. I apologize ahead of time if the code is not formatted correctly.

Option Compare Database
Option Explicit

Private m_strCTLLabel  As String
Private m_strCTLLabelHeader As String
Private colCalendarDates As Collection

Function getCalendarData() As Boolean
Dim rs As DAO.Recordset
Dim strDate As String
Dim strCode As String

Dim i As Integer

    'Dim qdf As DAO.QueryDef
    'Set qdf = CurrentDb.QueryDef("qr_SafetyCal2")
    'qdf.Parameters("[Forms]![fr_SafetyCal]![cboYear]") = [Forms]![fr_SafetyCal]![cboYear]
    'Set rs = qdf.OpenRecordset("qr_SafetyCal2", dbOpenDynaset)

    Set rs = CurrentDb.OpenRecordset("qr_SafetyCal2", dbOpenDynaset)

    Set colCalendarDates = New Collection
    With rs
        If (Not .BOF) Or (Not .EOF) Then
            .MoveLast
            .MoveFirst
        End If
        If .RecordCount > 0 Then
            For i = 1 To .RecordCount
                strDate = .Fields("Date")
                strCode = .Fields("ShortName")
                colCalendarDates.Add strCode, strDate
                .MoveNext
            Next i
        End If
        .Close
    End With
    'Return of dates and data collection form qr_SafetyCal2
    Set rs = Nothing

End Function

Public Sub loadReportYearCalendar(theReport As Report)
Dim i As Integer
Dim datStart As Date
Dim rptControl As Report

    m_strCTLLabel = "labelCELL"
    m_strCTLLabelHeader = "labelDAY"

    'Load calendar data for the specified year into the collection
    Call getCalendarData

    With theReport
        'Get the first month of the specified year
        datStart = "1/1/2017" '"1/1/" & Year(Date), "1/1/" & Forms!
        [fr_SafetyCal]![cboYear], Forms![fr_SafetyCal]![txtCalYear]
        'Add the specified year to the report's label
        .Controls("labelCalendarHeaderLine2").Caption = Year(datStart) & " 
        iCalendar"
        For i = 1 To 12
            'Set pointer to subreport control hosting the mini-calendar
            Set rptControl = .Controls("childCalendarMonth" & i).Report
            'Run procedure to populate control with it's respective year
            Call loadReportCalendar(rptControl, datStart)
            'Reset and obtain first day of the following month
            datStart = DateAdd("m", 1, datStart)
        Next i
     End With
    'Clean up
    Set colCalendarDates = Nothing
    Set rptControl = Nothing
End Sub


Public Sub loadReportCalendar(theReport As Report, Optional StartDate As 
Date, Optional theHeaderColor As Variant)
Dim i As Integer
Dim intCalDay As Integer
Dim datStartDate As Date
Dim intWeekDay As Integer

    datStartDate = StartDate
    intWeekDay = Weekday(datStartDate)

    With theReport

        .Controls("labelMONTH").Caption = Format(StartDate, "mmmm")

        'Change the day label's backcolor if necessary
        If Not (IsMissing(theHeaderColor)) Then
             For i = 1 To 7
                 .Controls("labelDayHeader" & i).BackColor = theHeaderColor
            Next
        End If

        For i = 1 To 42
            With .Controls(m_strCTLLabel & i)
                If (i >= intWeekDay) And (Month(StartDate) = 
                Month(datStartDate)) Then
                    If (datStartDate = Date) Then
                        .BackColor = 14277081
                    End If

                    On Error Resume Next

                    Dim strCaption As String
                    Dim strKey As String

                    strKey = datStartDate
                    strCaption = ""
                    strCaption = colCalendarDates.Item(strKey)
                    colCalendarDates.Remove strKey
                    'Set back color to grean on days in the past that have 
                    no corresponding event
                    If (datStartDate < Date) And (strCaption = vbNullString) Then
                    .Caption = Day(datStartDate)
                    .Bold = False
                    .BackColor = vbGreen
                    .ForeColor = vbWhite
                    .Heavy = True
                'Do not set a back color for days in the future
                ElseIf (datStartDate > Date) And (strCaption = vbNullString) Then
                    .Caption = Day(datStartDate)
                    .Bold = False
                'Set the corresponding labels and formats for each specified event
                Else
                    .Caption = strCaption
                    .Bold = True
                    Select Case strCaption
                        Case "FA"
                            .BackColor = vbYellow
                            .ForeColor = 0
                            .LeftMargin = 0
                            .TextAlign = 2
                        Case "FAM"
                            .BackColor = vbYellow
                            .ForeColor = 0
                            .LeftMargin = 0
                            .TextAlign = 2
                            .Heavy = True
                        Case "LTA"
                            .BackColor = vbRed
                            .ForeColor = vbWhite
                            .LeftMargin = 0
                            .TextAlign = 2
                        Case "MED"
                            .BackColor = vbRed
                            .ForeColor = vbWhite
                            .LeftMargin = 0
                            .TextAlign = 2
                    End Select

                End If

               datStartDate = DateAdd("d", 1, datStartDate)
            Else
                .Caption = ""
            End If
        End With
    Next i
End With

End Sub

Here is SQL for the two queries, the first is qr_SafetyCal and the second is qr_SafetyCal2:

SELECT tb_CaseLog.Date, Max(tb_Treatment.Priority) AS MaxOfPriority, 
Count(tb_Treatment.TreatmentID) AS CountOfTreatmentID
FROM tb_Treatment INNER JOIN tb_CaseLog ON tb_Treatment.TreatmentID = 
tb_CaseLog.Treatment
GROUP BY tb_CaseLog.Date;

SELECT qr_SafetyCal.Date, tb_Treatment.ShortName, 
qr_SafetyCal.CountOfTreatmentID AS [Count], Year([Date]) AS CalYear
FROM qr_SafetyCal INNER JOIN tb_Treatment ON qr_SafetyCal.MaxOfPriority = 
tb_Treatment.Priority;
1
Probably won't make any difference but because the Function does not return a value to the calling procedure, it could just be a Sub.June7
Code to set datStart should not even compile. Do you run Debug>Compile after code edits?June7

1 Answers

1
votes

No need to reference QueryDef.

Open the recordset object with filtered dataset by referencing the combobox like:

Set rs = CurrentDb.OpenRecordset("SELECT * FROM qr_SafetyCal2 WHERE CalYear=" & [Forms]![fr_SafetyCal]![cboYear], dbOpenDynaset)

or if the code is behind the form:

Set rs = CurrentDb.OpenRecordset("SELECT * FROM qr_SafetyCal2 WHERE CalYear=" & Me.[cboYear], dbOpenDynaset)

Both examples assume the field is a number type.

If there is no field in query with the year value, it can be extracted from date value field in the VBA construct:

Set rs = CurrentDb.OpenRecordset("SELECT * FROM qr_SafetyCal2 WHERE Year([YourFieldnameHere])=" & [Forms]![fr_SafetyCal]![cboYear], dbOpenDynaset)

Code for setting datStart variable:

'Get the first month of the specified year
datStart = "1/1/" & Forms![fr_SafetyCal].[cboYear]