I have a main form with a subform on it. The main form has a Calendar Control on it. When the main form loads, I set its recordsource and I set the date for the calendar. I then use the calendar's date to filter records in the subform.
Here is what's in the load event of the main form.
Here's what's in the load event of the subform
This all works fine the first two times I open the main form. But the third time I open it, I get the error:
The debugger debugs to the line in the main form's Load event:
Now my guess is that something is out of whack with the order of events firing when the main form and subform open. According to Microsoft's documentation the following events fire when a form is opened:
Does that mean that when the main form opens and the Load event fires, that the subform's Load event hasn't fired yet and there is no RecordSource, therefore raising the error when I try to set the subform's filter property? If that is the case, why wouldn't I get the error every time I opent the main form and not just every third time?
Ideas?
Here is what's in the load event of the main form.
Code:
Private Sub Form_Load()
Me.RecordSource = "SELECT EmployeeID FROM " & _
"tblEmployee WHERE EmployeeID=4"
'Set a combobox's default value so it displays the employee
Me.cboEmployee.DefaultValue = 4
'Set the calendar's date to the current date
Me.CtlCalendar.Value = FormatDateTime(Now(),vbShortDate)
'Filter the subform based on the calendar date
Me.SubformName.Form.Filter = "[Date Worked] = #" & Me.CtlCalendar.Value & "#"
Me.SubformName.Form.OrderBy = "[Date Worked],[Started]"
Me.SubformName.Form.FilterOn = True
Me.SubformName.Form.OrderByOn = True
Me.CtlCalendar.SetFocus
End Sub
Code:
Private Sub Form_Load()
Dim strSQL As String
strSQL = "SELECT * FROM tblTimeRecord WHERE fkEmployeeID=4"
Me.RecordSource = strSQL
End Sub
Code:
Err.Number = 2455
Err.Message = You entered an expression that has an invalid reference to the property Form/Report
Code:
Me.SubformName.Form.Filter = "[Date Worked] = #" & Me.CtlCalendar.Value & "#"
Code:
Open --> Load --> Activate --> Current (in that order)
Ideas?