Results 1 to 13 of 13

Thread: [RESOLVED] Run query within a query using code [RESOLVED]

  1. #1

    Thread Starter
    Hyperactive Member greaseman's Avatar
    Join Date
    Jul 2005
    Location
    Somewhere, out there...
    Posts
    295

    Resolved [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:
    1. Dim rsMstrPN As New ADODB.Recordset
    2.     Dim rsDuplicates As New ADODB.Recordset
    3.     Dim strSQL As String
    4.     Dim strErrMsg As String
    5.     Dim lngRecCount As Long
    6.     Dim lngTotalRec As Long
    7.     Dim strPartId As String
    8.  
    9. '==========================================================================
    10. ' Variable Initialization Section
    11. '==========================================================================
    12.     DeletePNdata = False
    13. '==========================================================================
    14. ' Code Section
    15. '==========================================================================
    16.  
    17.     strSQL = ""
    18.     strSQL = strSQL & "select DuplicatePartsBad.PNID from DuplicatePartsBad "
    19.     strSQL = strSQL & "Order by DuplicatePartsBad.PNID"
    20.     rsDuplicates.Open strSQL, conJetMasterDb, adOpenStatic,    adLockReadOnly, adCmdText
    21.     If Not (rsDuplicates.EOF = True And rsDuplicates.BOF = True) Then
    22.         rsDuplicates.MoveFirst
    23.         Do While Not rsDuplicates.EOF
    24.              strPartId = rsDuplicates!PNID
    25.              strSQL = ""
    26.              strSQL = strSQL & "Delete * from MasterPN where MasterPN.PNId    = " & strPartId & "; "
    27.              Debug.Print strSQL
    28.              rsMasterPN.Open strSQL, conJetMasterDb, adOpenStatic, adLockOptimistic, adCmdText
    29.         Loop
    30.     End If
    31.    
    32.     rsMstrPN.Close
    33.     rsDestination.Close

    Thaks to all of you who help out this myopic programmer!!
    Last edited by Hack; Mar 15th, 2006 at 07:04 AM.

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

    Re: Run query within a query using code

    Use connection object to delete records:
    VB Code:
    1. Do While Not rsDuplicates.EOF
    2.     strPartId = rsDuplicates!PNID
    3.     strSQL = "Delete * from MasterPN where PNId = " & strPartId
    4.     [B]conJetMasterDb.Execute strSQL[/B]
    5.     rsDuplicates.MoveNext
    6. Loop

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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:
    1. 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.

  4. #4
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Run query within a query using code

    Perhasp try this:

    VB Code:
    1. strSQL = ""
    2.     strSQL = strSQL & "select DuplicatePartsBad.PNID from DuplicatePartsBad "
    3.     strSQL = strSQL & "Order by DuplicatePartsBad.PNID"
    4.     rsDuplicates.Open strSQL, conJetMasterDb, adOpenStatic, adLockReadOnly, adCmdText
    5.     If Not (rsDuplicates.EOF = True And rsDuplicates.BOF = True) Then
    6.         rsDuplicates.MoveFirst
    7.         Do While Not rsDuplicates.EOF
    8.              strPartId = rsDuplicates!PNID
    9.              strSQL = "Delete * from MasterPN where MasterPN.PNId = " & strPartId & "; "
    10.              Debug.Print strSQL
    11.              conJetMasterDb.Execute strSQL
    12.              rsDuplicates.MoveNext
    13.         Loop
    14.     End If
    15.     rsDuplicates.Close

    If you're not returning a recordset then there is no need to use rs.Open, just Conn.Execute sql.

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

    Re: Run query within a query using code

    Quote 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...

  6. #6
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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".

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

    Re: Run query within a query using code

    Quote 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.

  8. #8
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    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:
    1. Dim rsMstrPN As New ADODB.Recordset
    2. '.....
    3. '.....
    4. rsMasterPN.Open strSQL, conJetMasterDb, adOpenStatic, adLockOptimistic, adCmdText
    Frans

  9. #9
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

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

    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.

  11. #11
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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.

  12. #12
    old fart Frans C's Avatar
    Join Date
    Oct 1999
    Location
    the Netherlands
    Posts
    2,926

    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.
    Frans

  13. #13

    Thread Starter
    Hyperactive Member greaseman's Avatar
    Join Date
    Jul 2005
    Location
    Somewhere, out there...
    Posts
    295

    Thumbs up Re: Run query within a query using code

    To all of you who replied...... thank you!! That certainly was a lot of interesting "discussion" (ahem), but the line of code (conJetMasterDb.Execute strSQL) suggested did the trick.

    As far as the path the responses started following, it doesn't really matter, as long as what gets suggested does the trick.

    Incidentally, thanks for the pickup on my typo..... however, even correcting the typo still did not work.... I had to put in the conJetMasterDb.Execute strSQL line of code to get things working.

    I also had to put in a rsDuplicates.MoveNext line of code to keep my snippet from looping forever.

    Thanks again for your good and fast responses! Love this forum!
    Last edited by greaseman; Mar 14th, 2006 at 11:05 AM. Reason: bvcbc

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