Results 1 to 4 of 4

Thread: DAO.Recordset weirdness crashes Access

  1. #1

    Thread Starter
    Black Cat JoshT's Avatar
    Join Date
    Nov 2000
    Location
    WNY, USA
    Posts
    4,032
    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:
    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
    Version B:
    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

  2. #2
    Fanatic Member
    Join Date
    Oct 2000
    Location
    London
    Posts
    1,008
    Has to be with poor memory management by VB - Dims inside the procedure ensure that variable lifetime does not extend beyond the end of the procedure lifetime - Version A leaves variables hanging around, crashing the machine...

    Paul.
    Not nearly so tired now...

    Haven't been around much so be gentle...

  3. #3
    Fanatic Member
    Join Date
    Aug 2000
    Posts
    617

    hi;.

    Another difference u forget to point out and that can be crucial


    is where you set

    Set rs = Nothing

    Check it out...!!

  4. #4
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ....

    Actually I have never programmed in Access, but from whatever VB knowledge I have, I can give you another tip.

    In your first code, after you are done with the recordset, close it with something like

    Code:
    rsRecordSet.Close

    Or the problem could be the statement

    [code]
    Set rsRecordSet = Nothing
    [\code]


    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width