|
-
Mar 14th, 2006, 08:54 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Run query within a query using code [RESOLVED]
I'm having a bit of a headache with trying to run a delete query using the results of a select query, in VB code. My first query runs OK, but when I try running the second query, I keep getting a message, "Object required."
I've enclosed my code that is causing the problem..... if y'all could take a look at it and tell me the erors of my ways, I would be very happy.
VB Code:
Dim rsMstrPN As New ADODB.Recordset
Dim rsDuplicates As New ADODB.Recordset
Dim strSQL As String
Dim strErrMsg As String
Dim lngRecCount As Long
Dim lngTotalRec As Long
Dim strPartId As String
'==========================================================================
' Variable Initialization Section
'==========================================================================
DeletePNdata = False
'==========================================================================
' Code Section
'==========================================================================
strSQL = ""
strSQL = strSQL & "select DuplicatePartsBad.PNID from DuplicatePartsBad "
strSQL = strSQL & "Order by DuplicatePartsBad.PNID"
rsDuplicates.Open strSQL, conJetMasterDb, adOpenStatic, adLockReadOnly, adCmdText
If Not (rsDuplicates.EOF = True And rsDuplicates.BOF = True) Then
rsDuplicates.MoveFirst
Do While Not rsDuplicates.EOF
strPartId = rsDuplicates!PNID
strSQL = ""
strSQL = strSQL & "Delete * from MasterPN where MasterPN.PNId = " & strPartId & "; "
Debug.Print strSQL
rsMasterPN.Open strSQL, conJetMasterDb, adOpenStatic, adLockOptimistic, adCmdText
Loop
End If
rsMstrPN.Close
rsDestination.Close
Thaks to all of you who help out this myopic programmer!!
Last edited by Hack; Mar 15th, 2006 at 07:04 AM.
-
Mar 14th, 2006, 09:01 AM
#2
Re: Run query within a query using code
Use connection object to delete records:
VB Code:
Do While Not rsDuplicates.EOF
strPartId = rsDuplicates!PNID
strSQL = "Delete * from MasterPN where PNId = " & strPartId
[B]conJetMasterDb.Execute strSQL[/B]
rsDuplicates.MoveNext
Loop
-
Mar 14th, 2006, 09:03 AM
#3
Re: Run query within a query using code
Action queries (Delete/Insert/Update/...) do not return recordsets, so should be run like this instead:
VB Code:
conJetMasterDb.Execute strSQL
You didn't mention where the error occured, which would blatantly be useful for us to know. I can see where it is, and it is due to a typo in the variable name.. but as explained above you dont need the variable anyway.
-
Mar 14th, 2006, 09:03 AM
#4
Re: Run query within a query using code
Perhasp try this:
VB Code:
strSQL = ""
strSQL = strSQL & "select DuplicatePartsBad.PNID from DuplicatePartsBad "
strSQL = strSQL & "Order by DuplicatePartsBad.PNID"
rsDuplicates.Open strSQL, conJetMasterDb, adOpenStatic, adLockReadOnly, adCmdText
If Not (rsDuplicates.EOF = True And rsDuplicates.BOF = True) Then
rsDuplicates.MoveFirst
Do While Not rsDuplicates.EOF
strPartId = rsDuplicates!PNID
strSQL = "Delete * from MasterPN where MasterPN.PNId = " & strPartId & "; "
Debug.Print strSQL
conJetMasterDb.Execute strSQL
rsDuplicates.MoveNext
Loop
End If
rsDuplicates.Close
If you're not returning a recordset then there is no need to use rs.Open, just Conn.Execute sql.
-
Mar 14th, 2006, 09:09 AM
#5
Re: Run query within a query using code
 Originally Posted by si_the_geek
Action queries (Delete/Insert/Update/...) do not return recordsets, so should be run like this instead: ...
Not exactly - you can still use recordset object to execute any sql statement.
It's not conventional way but lots of people use it...
-
Mar 14th, 2006, 09:11 AM
#6
Re: Run query within a query using code
Well yes you can use a recordset object to run them, but it is slower and wastes memory.. hence "should be".
-
Mar 14th, 2006, 09:14 AM
#7
Re: Run query within a query using code
 Originally Posted by si_the_geek
Well yes you can use a recordset object to run them, but it is slower and wastes memory.. hence "should be".
Please SI, don't even start that nonsense - this technic is very widely used and performance depends on many many many criterias.
Personally I prefer to use connection object but it doesn't mean it's better and/or someone should do the same.
-
Mar 14th, 2006, 09:17 AM
#8
Re: Run query within a query using code
Both is possible.
The error is caused by a typo
The recordset is declared as rsMstrPN , but opened as rsMasterPN.
VB Code:
Dim rsMstrPN As New ADODB.Recordset
'.....
'.....
rsMasterPN.Open strSQL, conJetMasterDb, adOpenStatic, adLockOptimistic, adCmdText
-
Mar 14th, 2006, 09:24 AM
#9
Re: Run query within a query using code
Yep, as I mentioned above 
It certainly isn't nonsense RhinoBull.
It is slower, as the API's which ADO uses for opening a recordset interrogate the DBMS in a different way, which takes longer than the simpler method used by Execute. It may not be a big deal, but it does have an impact. For larger scale systems this can cause slowdown, but admittedly for most programs/databases the difference wont be noticable.
It wastes memory, as the recordset object uses a comparatively large amount of resources - which aren't needed at all.
Just because something is widely used does not mean it is the right way to do things. Using a recordset object works, but is not the optimal method.
-
Mar 14th, 2006, 09:28 AM
#10
Re: Run query within a query using code
Oh, you have a wrong idea, man... Sql is evaluated first and if it is supposed to return something then recodset will be initialzed and populated, otherwise recordset object "is left along"... so stop here and agree that we disagree.
-
Mar 14th, 2006, 09:43 AM
#11
Re: Run query within a query using code
It always make me laugh when you get like this. You have a great knowledge of VB, but when it gets into specifics like this your lack of in-depth knowledge shows itself - as does your will to disparage others.
You are right that the SQL is evaluated first, but are you aware of the two contrasting methods involved as part of each object? Are you aware of how the preparations are done, and how the returns of an SQL statement are evaluated by ADO?
If you don't want to have discussions about the issues and reasoning behind what people have said, then please don't start them.
-
Mar 14th, 2006, 10:41 AM
#12
Re: Run query within a query using code
OK, now kiss and make up.
I dont think the overhead will be big, but in order to call the Open method of a recordset, the object has at least to be created.
-
Mar 14th, 2006, 10:58 AM
#13
Thread Starter
Hyperactive Member
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
|