I've written code for a form in Access 2000, which contains a Calendar Control 9.0. Version A of the code pasted below crashes Access with an Application Error after about 2 - 5 clicks on the calendar control. Version B pasted below works fine. Why? The only difference is where I declare the variables.
Thanks,
Josh
Version A:
Version B:Code:Option Compare Database Option Explicit Dim rs As DAO.Recordset 'the recordset Dim Q As String 'query string Dim CalValue As String 'storage for CalendarControl.value Private Sub CalendarControl_Click() 'set subform to only display for the date picked CalValue = CalendarControl.Value Q = "SELECT * FROM [TimeSheetTable] WHERE [Date1] = #" & _ CalValue & "#;" TimeSheetSubForm.Form.RecordSource = Q 'enter date into subform if empty Set rs = TimeSheetSubForm.Form.Recordset If rs.EOF Then TimeSheetSubForm.Form.txtDate.SetFocus TimeSheetSubForm.Form.txtDate.Text = CalValue Else End If End Sub Private Sub Form_Load() 'show today when form opens CalendarControl.Value = Now CalendarControl_Click End Sub Private Sub Form_Unload(Cancel As Integer) 'destroy recordset Set rs = Nothing End Sub
Code:Option Compare Database Option Explicit Private Sub CalendarControl_Click() Dim rs As DAO.Recordset 'the recordset Dim Q As String 'query string Dim CalValue As String 'storage for CalendarControl.value 'set subform to only display for the date picked CalValue = CalendarControl.Value Q = "SELECT * FROM [TimeSheetTable] WHERE [Date1] = #" & _ CalValue & "#;" TimeSheetSubForm.Form.RecordSource = Q 'enter date into subform if empty Set rs = TimeSheetSubForm.Form.Recordset If rs.EOF Then TimeSheetSubForm.Form.txtDate.SetFocus TimeSheetSubForm.Form.txtDate.Text = CalValue Else End If Set rs = Nothing End Sub Private Sub Form_Load() 'show today when form opens CalendarControl.Value = Now CalendarControl_Click End Sub




Reply With Quote