|
-
Mar 10th, 2005, 01:04 PM
#1
Thread Starter
Lively Member
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:
Private Sub DoUpdate(ByVal objConn as ADODB.Connection, _
ByVal strSurname as String, ByVal strNew as String, _
ByVal intID as Integer)
Dim objRS as ADODB.Recordset
Set objRS = New ADODB.Recordset
objRS.Open "INSERT INTO Members(Surname) VALUES('" & strSurname & "');", _
objConn, adOpenStatic, adLockPessimistic
objRS.Close
objRS.Open "UPDATE Register SET NewUser='" & strNew & _
"' WHERE MemID=" & intID & ";", objConn, adOpenStatic, adLockPessimistic
objRS.Close
End Sub
Any ideas?
Last edited by olamm2k; Mar 10th, 2005 at 02:08 PM.
-
Mar 10th, 2005, 01:08 PM
#2
Re: Opening and closing a recordset in VB6
Check the state of your recordset object, and take whatever steps are appropriate.
VB Code:
If objRs.State = adStateOpen Then objRs.Close
-
Mar 10th, 2005, 01:11 PM
#3
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:
If Not objRs is Nothing Then
If objRs.State = adStateOpen then
objRs.Close
End If
End If
-
Mar 10th, 2005, 01:15 PM
#4
Thread Starter
Lively Member
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?
-
Mar 10th, 2005, 01:22 PM
#5
Re: Opening and closing a recordset in VB6
 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*
 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:
objConn.Execute "Your SQL String"
-
Mar 10th, 2005, 01:30 PM
#6
Thread Starter
Lively Member
Re: Opening and closing a recordset in VB6
 Originally Posted by Hack
You would use the command object
VB Code:
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)?
-
Mar 10th, 2005, 01:39 PM
#7
Re: Opening and closing a recordset in VB6
 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.
-
Mar 10th, 2005, 02:07 PM
#8
Thread Starter
Lively Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|