Results 1 to 4 of 4

Thread: Help with error: 'Operation is not allowed when the object is open'

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2010
    Posts
    30

    Help with error: 'Operation is not allowed when the object is open'

    please someone help me...
    i need to figure out how this error be remove
    i will attached the codes i used... and the program it self

    FORM1 codes
    vb Code:
    1. Dim db As New ADODB.Connection
    2. Dim rsstudinfo As New ADODB.Recordset
    3. Dim rsgrade As New ADODB.Recordset
    4. Dim x As Integer
    5.  
    6. Private Sub cmd1_Click()
    7.     frmSearch.Show
    8. End Sub
    9.  
    10. Private Sub cmdNext_Click()
    11. rsgrade.Open "select * from tblGrade where studnum ='" & txtsearch.Text & "'", db, adOpenDynamic, adLockOptimistic
    12. If rsgrade.EOF = True Then
    13.     MsgBox "END OF RECORD", vbOKOnly, "END OF FILE"
    14.    
    15.     rsgrade.MovePrevious
    16.    
    17. Else
    18.     Form1.txta.Text = rsgrade.Fields(0)
    19.     Form1.txtb.Text = rsgrade.Fields(1)
    20.     Form1.txtc.Text = rsgrade.Fields(2)
    21.     Form1.txtd.Text = rsgrade.Fields(3)
    22.     rsgrade.MoveNext
    23.  
    24. End If
    25.  
    26.  
    27. End Sub
    28.  
    29. Private Sub cmdPrev_Click()
    30. rsgrade.Open "select * from tblGrade where studnum ='" & txtsearch.Text & "'", db, adOpenDynamic, adLockOptimistic
    31. If rsgrade.BOF = True Then
    32.     MsgBox "END OF RECORD", vbOKOnly, "END OF FILE"
    33.    
    34.     rsgrade.MoveNext
    35.  
    36. Else
    37.     Form1.txta.Text = rsgrade.Fields(0)
    38.     Form1.txtb.Text = rsgrade.Fields(1)
    39.     Form1.txtc.Text = rsgrade.Fields(2)
    40.     Form1.txtd.Text = rsgrade.Fields(3)
    41.     rsgrade.MovePrevious
    42.  
    43. End If
    44.  
    45. End Sub
    46.  
    47. Private Sub Command1_Click()
    48. Form1.txt1.Text = rsstudinfo.Fields(0)
    49.     Form1.txt2.Text = rsstudinfo.Fields(1)
    50.     Form1.txt3.Text = rsstudinfo.Fields(2)
    51.     Form1.txt4.Text = rsstudinfo.Fields(3)
    52.     Form1.txt5.Text = rsstudinfo.Fields(4)
    53.     rsstudinfo.MoveLast
    54. End Sub
    55.  
    56. Private Sub Command2_Click()
    57. Form1.txt1.Text = rsstudinfo.Fields(0)
    58.     Form1.txt2.Text = rsstudinfo.Fields(1)
    59.     Form1.txt3.Text = rsstudinfo.Fields(2)
    60.     Form1.txt4.Text = rsstudinfo.Fields(3)
    61.     Form1.txt5.Text = rsstudinfo.Fields(4)
    62.     rsstudinfo.MoveFirst
    63. End Sub
    64.  
    65. Private Sub Command3_Click()
    66. If rsstudinfo.BOF = True Then
    67.     MsgBox "END OF RECORD", vbOKOnly, "END OF FILE"
    68.     rsstudinfo.MoveNext
    69. Else
    70.     Form1.txt1.Text = rsstudinfo.Fields(0)
    71.     Form1.txt2.Text = rsstudinfo.Fields(1)
    72.     Form1.txt3.Text = rsstudinfo.Fields(2)
    73.     Form1.txt4.Text = rsstudinfo.Fields(3)
    74.     Form1.txt5.Text = rsstudinfo.Fields(4)
    75.     rsstudinfo.MovePrevious
    76. End If
    77.    
    78. End Sub
    79.  
    80. Private Sub Command4_Click()
    81. If rsstudinfo.EOF = True Then
    82.     MsgBox "END OF RECORD", vbOKOnly, "END OF FILE"
    83.    
    84.     rsstudinfo.MovePrevious
    85. Else
    86.     Form1.txt1.Text = rsstudinfo.Fields(0)
    87.     Form1.txt2.Text = rsstudinfo.Fields(1)
    88.     Form1.txt3.Text = rsstudinfo.Fields(2)
    89.     Form1.txt4.Text = rsstudinfo.Fields(3)
    90.     Form1.txt5.Text = rsstudinfo.Fields(4)
    91.     rsstudinfo.MoveNext
    92. End If
    93. End Sub
    94.  
    95. Private Sub Form_Load()
    96. db.Open "Provider=Microsoft.jet.oledb.4.0; Data Source=" & App.Path & "\db1.mdb"
    97. rsstudinfo.Open "select * from tblStudinfo", db, adOpenDynamic, adLockOptimistic
    98.  
    99. Do While rsstudinfo.EOF = False
    100.     Form1.txt1.Text = rsstudinfo.Fields(0)
    101.     Form1.txt2.Text = rsstudinfo.Fields(1)
    102.     Form1.txt3.Text = rsstudinfo.Fields(2)
    103.     Form1.txt4.Text = rsstudinfo.Fields(3)
    104.     Form1.txt5.Text = rsstudinfo.Fields(4)
    105.     rsstudinfo.MoveNext
    106.     'Do While rsgrade.EOF = False
    107.      '   Form1.txta.Text = rsgrade.Fields(0)
    108.       '  Form1.txtb.Text = rsgrade.Fields(1)
    109.        ' Form1.txtc.Text = rsgrade.Fields(2)
    110.         'Form1.txtd.Text = rsgrade.Fields(3)
    111.         'rsgrade.MoveNext
    112.     'Loop
    113. Loop
    114.  
    115.  
    116. End Sub
    117.  
    118. '------------------------------------------------
    119.  
    120. 'FORM SEARCH
    121.  
    122. Dim db As New ADODB.Connection
    123. Dim rsstudinfo As New ADODB.Recordset
    124. Dim rsgrade As New ADODB.Recordset
    125. Private Sub cmdSearch_Click()
    126. 'db.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & App.Path & "\db1.mdb"
    127. 'db.Open
    128. db.Open "Provider=Microsoft.jet.oledb.4.0; Data Source=" & App.Path & "\db1.mdb"
    129. Form1.txtsearch.Text = txtsearch.Text
    130.     rsstudinfo.Open "Select * from tblSTudinfo where StudNum like '" & txtsearch.Text & "%'", db, adOpenDynamic, adLockOptimistic
    131.     rsgrade.Open "select * from tblGrade where studnum ='" & txtsearch.Text & "'", db, adOpenDynamic, adLockOptimistic
    132.     Form1.txt1.Text = rsstudinfo.Fields(0)
    133.     Form1.txt2.Text = rsstudinfo.Fields(1)
    134.     Form1.txt3.Text = rsstudinfo.Fields(2)
    135.     Form1.txt4.Text = rsstudinfo.Fields(3)
    136.     Form1.txt5.Text = rsstudinfo.Fields(4)
    137.     Do While rsgrade.EOF = False
    138.         Form1.txta.Text = rsgrade.Fields(0)
    139.         Form1.txtb.Text = rsgrade.Fields(1)
    140.         Form1.txtc.Text = rsgrade.Fields(2)
    141.         Form1.txtd.Text = rsgrade.Fields(3)
    142.         rsgrade.MoveNext
    143.     Loop
    144.     rsgrade.MoveFirst
    145. Me.Hide
    146. End Sub
    147.  
    148. '-----------------------------
    149.  
    150. 'MODULE 1
    151.  
    152. Dim db As New ADODB.Connection
    153. Dim rsstudinfo As New ADODB.Recordset
    154. Dim rsgrade As New ADODB.Recordset
    155.  
    156. Public Sub main()
    157. db.Open "Provider=Microsoft.jet.oledb.4.0; Data Source=" & App.Path & "\db1.mdb"
    158. Form1.Show
    159. Form1.txtCtr.Text = ""
    160. End Sub
    161.  
    162. Public Sub field()
    163. db.Open "Provider=Microsoft.jet.oledb.4.0; Data Source=" & App.Path & "\db1.mdb"
    164. rsstudinfo.Open "select * from tblStudinfo", db, adOpenDynamic, adLockOptimistic
    165. Do While rsstudinfo.EOF = False
    166.     Form1.txt1.Text = rsstudinfo.Fields(0)
    167.     Form1.txt2.Text = rsstudinfo.Fields(1)
    168.     Form1.txt3.Text = rsstudinfo.Fields(2)
    169.     Form1.txt4.Text = rsstudinfo.Fields(3)
    170.     Form1.txt5.Text = rsstudinfo.Fields(4)
    171.     rsstudinfo.MoveNext
    172. Loop
    173.  
    174.  
    175. End Sub




    thanks who those will reply....
    Attached Files Attached Files
    Last edited by Hack; Feb 17th, 2010 at 07:08 AM. Reason: Added Highlight Tags

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Help with error: 'Operation is not allowed when the object is open'

    On what line of code does the error occur?

    (PS: I edited your post and added [highlight=vb]your code goes in here[/highlight] - it makes code so much easier to read.)

  3. #3
    Freelancer akhileshbc's Avatar
    Join Date
    Jun 2008
    Location
    Trivandrum, Kerala, India
    Posts
    7,652

    Re: Help with error: 'Operation is not allowed when the object is open'

    When you have finished with the database and wants to open a new connection, you have to close the connection first.

    Example:
    Code:
    Dim cn As ADODB.Connection
    Dim rs As ADODB.Recordset
    
    Set cn = New ADODB.Connection
    cn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
          "Data Source= " & App.Path & "\database.mdb"
        cn.Open
    Set rs = New ADODB.Recordset
        rs.Open "settings", cn, adOpenKeyset, adLockPessimistic, adCmdTable 
    
    '// Here you will do the necessary coding.eg. searching, filling Listbox, etc.)
    
    '// Now we have to close it
    rs.close
    
    '// now opening another table
        rs.Open "mytable", cn, adOpenKeyset, adLockPessimistic, adCmdTable 
    
    '// rest of the code
    
    '// now close all the connections and empty everything from the memory
    
    rs.Close
    cn.Close
    Set rs = Nothing
    Set cn = Nothing
    ...

    If my post was helpful to you, then express your gratitude using Rate this Post.
    And if your problem is SOLVED, then please Mark the Thread as RESOLVED (see it in action - video)
    My system: AMD FX 6100, Gigabyte Motherboard, 8 GB Crossair Vengance, Cooler Master 450W Thunder PSU, 1.4 TB HDD, 18.5" TFT(Wide), Antec V1 Cabinet

    Social Group: VBForums - Developers from India


    Skills: PHP, MySQL, jQuery, VB.Net, Photoshop, CodeIgniter, Bootstrap,...

  4. #4
    PowerPoster
    Join Date
    Jun 2001
    Location
    Trafalgar, IN
    Posts
    4,141

    Re: Help with error: 'Operation is not allowed when the object is open'

    If your error is happening at lines 11 or 30 then I would try putting rsgrade.Close at lines 25 and 44.

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