Results 1 to 8 of 8

Thread: Opening and closing a recordset in VB6 [Resolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Resolved Opening and closing a recordset in VB6 [Resolved]

    Hi,

    I'm developing a database application in Visual Basic 6 and there are some occaisions where I need to execute two different queries on different tables. When I try and close the recordset I'm using to do the first query for the second query, I get an error saying that the recordset is already closed, despite the fact that I have just opened it.

    VB Code:
    1. Private Sub DoUpdate(ByVal objConn as ADODB.Connection, _
    2.   ByVal strSurname as String, ByVal strNew as String, _
    3.   ByVal intID as Integer)
    4. Dim objRS as ADODB.Recordset
    5.  
    6. Set objRS = New ADODB.Recordset
    7.  
    8. objRS.Open "INSERT INTO Members(Surname) VALUES('" & strSurname & "');", _
    9.   objConn, adOpenStatic, adLockPessimistic
    10.  
    11. objRS.Close
    12.  
    13. objRS.Open "UPDATE Register SET NewUser='" & strNew & _
    14.   "' WHERE MemID=" & intID & ";", objConn, adOpenStatic, adLockPessimistic
    15.  
    16. objRS.Close
    17.  
    18. End Sub

    Any ideas?
    Last edited by olamm2k; Mar 10th, 2005 at 02:08 PM.

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

    Re: Opening and closing a recordset in VB6

    Check the state of your recordset object, and take whatever steps are appropriate.
    VB Code:
    1. If objRs.State = adStateOpen Then objRs.Close

  3. #3
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Opening and closing a recordset in VB6

    Insert and Update statements do not open a recordset.

    You should use the Execute method of the Connection or Command objects to run these type of statements.

    But you could always check to see if the recordset is open before attempting to close.

    VB Code:
    1. If Not objRs is Nothing Then
    2.    If objRs.State = adStateOpen then
    3.          objRs.Close
    4.    End If
    5. End If

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Opening and closing a recordset in VB6

    My current version of the code does use an If statement to check the status - that was just an example of what I was trying before.

    I didn't know INSERT and UPDATE don't open a recordset. How would I implement these queries using the Execute method of the Connection or Command objects as suggested?

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

    Re: Opening and closing a recordset in VB6

    Quote Originally Posted by brucevde
    Insert and Update statements do not open a recordset.
    Very true...thats what I get for scanning the question rather than reading it. *slaps self*
    Quote Originally Posted by olamm2k
    How would I implement these queries using the Execute method of the Connection or Command objects as suggested?
    You would use the command object
    VB Code:
    1. objConn.Execute "Your SQL String"

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Opening and closing a recordset in VB6

    Quote Originally Posted by Hack
    You would use the command object
    VB Code:
    1. objConn.Execute "Your SQL String"
    That's the connection object. How would I do it with a Command object (I've never used one before)?

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

    Re: Opening and closing a recordset in VB6

    Quote Originally Posted by olamm2k
    That's the connection object. How would I do it with a Command object (I've never used one before)?
    I'm having a bad day. I meant the connection object, not the command object. I don't use the command object for INSERTS or UPDATES either, but I do use the connection object.

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Feb 2005
    Posts
    116

    Re: Opening and closing a recordset in VB6

    I've just been reading what MSDN has to say about Command objects, and I don't think I need to use one here, so I'll use the Execute method of the Connection object.

    Thanks

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