Results 1 to 3 of 3

Thread: Object required error...pls help

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2004
    Posts
    21

    Exclamation Object required error...pls help

    I get an object required error when working with rstSubjectList from cmdAdd_Click. When I go to tracking mode from the command button, it says rstSubjectList = Nothing. It seems that the recordset is not being carried accross from the form_load, despite the two events being local to each other (they are in the same form). Please tell me how I can fix this error.

    VB Code:
    1. Private Sub Form_Load()
    2. Dim rstSubjectList As Recordset
    3.  
    4.  
    5. Set rstSubjectList = db.OpenRecordset("SELECT Subject FROM Subjects ORDER BY Subject")
    6. rstSubjectList.MoveFirst
    7.     Do While Not rstSubjectList.EOF
    8.         cboSubject.AddItem (rstSubjectList.Fields("Subject").Value)
    9.         rstSubjectList.MoveNext
    10.     Loop
    11.  
    12. End Sub
    13.  
    14. Private Sub cmdAdd_Click()
    15. With rstSubjectList 'Add subject if it doesn't already exist
    16.     .FindFirst "Subject ='" & cboSubject.Text & "'"
    17.     If .NoMatch = True Then
    18.         .AddNew
    19.         .Fields("Subject").Value = cboSubject.Text
    20.         .Update
    21.     End If
    22. End With

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Object required error...pls help

    If you need your recordset object to be available throughout your application or at least form's life cycle then move declaration to general section:

    Option Explicit
    Dim rstSubjectList As Recordset

    Although, change it to the following:

    Dim rstSubjectList As New Recordset

    so it would be initialized or do it during Form_Load event:

    Set rstSubjectList = New Recordset

  3. #3
    Fanatic Member Armbruster's Avatar
    Join Date
    Sep 2002
    Location
    Maryland Heights, MO
    Posts
    857

    Re: Object required error...pls help

    RhinoBull is correct, but I would stay away from:

    VB Code:
    1. Dim rstSubjectList As New Recordset

    http://www.vbforums.com/showthread.p...ghlight=arm%2A
    "Look! Up in the sky! It's a bird! It's a plane! It's Diaper-Head Boy! (there by my name!) Yes, Diaper-Head Boy, who disguised as my son, Seth, fights a never-ending battle for truth, justice and terrorizing my house!

    Resistance is futile, you will be compiled . . . Please!

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