Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   Index   Templates   Seminars   TechHelp   Forums   Help   Contact   Join   Order   Logon  
 
Home > Courses > Access > Developer > D26 > < D25 | D27 >
Back to Access Developer 26    Comments List
Upload Images   Link   Email  
Recordset in recordset
Ole Simonsen 
    
4 years ago
Hi Richard!
I try to open an record set within a recordset. It runs. But I can not close the second recordset.
Adam Schwanz 
            
4 years ago
What do you mean you can not close the second record set? Do you get an error or something (if so what does it say?). Could you share your code or some images so we have something to go off of? Snip & Sketch
Richard Rost 
          
4 years ago
Slam the lid harder. :)

Yeah... need more info for this one.
Ole Simonsen OP 
    
4 years ago
Errormessage when I try to colse rsAID:
My code:
        Set rs = CurrentDb.OpenRecordset("SELECT * FROM PopxaordreidQ WHERE OrdreID=" & OrdreID)
        X1 = 0
        While Not rs.EOF
            If rs!Levert = False Then
                X1 = X1 + 1
                Set rsAID = CurrentDb.OpenRecordset("SELECT * FROM ArtikkelT WHERE Artikkelid =" & rs!ArtikkelID)
                With rsAID
                    If !ArtikkelUtleid = 0 Then
                        MsgBox "Antall utleid er 0 fra fr!"
                    Else
                        .Edit
                        !ArtikkelUtleid = !ArtikkelUtleid - rs!PakketAntallA
                        .Update
                        .Close
                        MsgBox "Antall utleid er: " & rs!PakketAntallA
                    End If
                End With
                'rsAID.Close
                'Set rsAID = Nothing
                MsgBox rs!PakketOrdreProduktXArtikkelID
                DoCmd.RunSQL "DELETE * from PakketOrdrePro
Ole Simonsen OP 
    
4 years ago
"Close". And the error say: Run-time error '91' Object variable or with block variable not set.
Kevin Robertson 
          
4 years ago
You are trying to close rsAid twice. Once inside the With block, then again after.
Kevin Robertson 
          
4 years ago
Just noticed the problem. You haven't closed your While loop.
Richard Rost 
          
4 years ago
He might have run out of room to post his code and didn't realize it. Not ending your While loop would result in a syntax error.

Ole: post a screen shot of then entire block of code if you can. That's usually better that copying and pasting the text.
Ole Simonsen OP 
    
4 years ago
My mistake. This is the part of code that gives the error message:
        Set rs = CurrentDb.OpenRecordset("SELECT * FROM PopxaordreidQ WHERE OrdreID=" & OrdreID)
        While Not rs.EOF
            If rs!Levert = False Then
                Set rsAID = CurrentDb.OpenRecordset("SELECT * FROM ArtikkelT WHERE Artikkelid =" & rs!ArtikkelID)
                With rsAID
                    If !ArtikkelUtleid = 0 Then
                        MsgBox "Amount out is 0 !"
                    Else
                        .Edit
                        !ArtikkelUtleid = !ArtikkelUtleid - rs!PakketAntallA
                        .Update
                        .Close
                        MsgBox "Amount out is: " & rs!PakketAntallA
                    End If
                End With
            Else
                X1 = uDialog2("Order delivered. Item not changed! ", , 255, 240, 200, 14, , , "OK !")
            End If
            DoCmd.RunSQL "DELETE * from PakketOrdreProduktXArtikkelT WHERE (Pakk
Ole Simonsen OP 
    
4 years ago
I tried to close rsAID in two different places. Thats why there were double set of "Close rsAID".
Ole Simonsen OP 
    
4 years ago

Ole Simonsen OP 
    
4 years ago
This is the code Im using without the "With rsAID"
Ole Simonsen OP 
    
4 years ago
And finali I realiced that you did not see thee entire code :)
Next is the one that did not work.
Ole Simonsen OP 
    
4 years ago

Richard Rost 
          
4 years ago
OK, so what's the specific error message you're getting, and which line does it break on?
Ole Simonsen OP 
    
4 years ago
Hi Richard!

The error message: Run-time error '91' Object variable or with block variable not set.
Line: rsAID.Close
Scott Axton 
          
4 years ago
Ole -

You are trying to close the record set when it wasn't set.

The  second record set - rsAID - is only set if If rs!Levert = False.  
So...  if rs!Levert = True  The rsAID set never fires, and the code generates an error because you can't close something that was never open in the first place.



Scott Axton 
          
4 years ago
To fix it you need to move the "Set rsAID = ... " outside of the If statement.  I would put it right under the Set rs = .

Alternatively you could initialize the rsAID there to a blank string:

Set rs = ....
Set rsAID = ""

Then if rs!Levert = False the rsAID would be changed to the code you have.
Scott Axton 
          
4 years ago
Sorry - one more thing.  I recommend cleaning up your "set" statements in the reverse order of setting them.
So...

    Set rs =
    Set rsAID =

        Some code here

    rsAID.Close
    rs.Close
    Set rsAID = Nothing
    Set rs = Nothing



Ole Simonsen OP 
    
4 years ago
Hi Scott! Thx for your reply.
I do get error message: Type mismached if I use : Set rsAID = "". I tried to move the Set rsAID = ..... before the If statement.
But as long as the Set rsAID is inside the While NOT rs.EOF. It runs correclty but produses error messages.
Scott Axton 
          
4 years ago
Sorry I thought you could initialize the rsAID (It might have to be NULL instead - I'll have to try in a different setting.)

So what I would have you try is close the rsAID right after you are done using it.
Just after End With but before the Else.

I'm shooting in the dark a bit here because of our language barrier and also not having your database.


Ole Simonsen OP 
    
4 years ago
Hi Scott!
I simplyfied the code:

        While Not rs.EOF
            Nr1 = Nr1 + 1
            Nr2 = rs!ArtikkelUtleid
            Set rsAID = CurrentDb.OpenRecordset("SELECT * FROM ArtikkelT WHERE Artikkelid =" & rs!ArtikkelID)
            With rsAID
                .Edit
                !ArtikkelUtleid = !ArtikkelUtleid - rs!PakketAntallA
                .Update
                .Close
                MsgBox "Antall utleid er: " & rs!PakketAntallA
            End With
            rsAID.Close
            Set rsAID = Nothing
            X1 = uDialog("ItemID: " & rs!ArtikkelID & Nr2, , 255, 240, 200, 14, , , "OK !")
            rs.MoveNext
        Wend
Ole Simonsen OP 
    
4 years ago
I do get errormessage: 3420 on rsAID.Close
Kevin Robertson 
          
4 years ago
The problem may be you are opening and closing rsAid with every iteration of your loop. Try setting the recordset before the loop and close and destroy your objects when the loop is finished.
Scott Axton 
          
4 years ago
Ole go back to the code above.  Before I confused things trying to set the rsAID to "" I said the same thing Kevin just mentioned.

See the next image.


    Set rs =
    Set rsAID =

        Do the rest of the code here.

    rsAID.Close
    rs.Close
    Set rsAID = Nothing
    Set rs = Nothing
Scott Axton 
          
4 years ago

Kevin Robertson 
          
4 years ago
Also rsAid is being closed in your With block. Remember to remove it too.
Ole Simonsen OP 
    
4 years ago
Hi Scott! This brings me to the original question to open a recordset within a recordset. I think that this is not doable.  If I put rsAID before the WHILE NOT rs.EOF. Then only the first record of rsAID is changed.  Not the rest. But the code you refered to does the job. But do not let me close the rsAID. Does not matter where I place the close rsAID.
Scott Axton 
          
4 years ago
Ole -
You just added something you didn't mention before to the problem.  If you want to step through the rsAID you'll need the While Not rsAID.EOF just like you had for the rs.EOF.  
In addition you will need to have the rsAID.MoveNext for that "loop" as well.

I suggest you go back and re-watch Access Developer 19   The Nested Recordsets is covered in lesson 3.
Ole Simonsen OP 
    
4 years ago
Hi Scott! THX. Il do that.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in Access Developer 26.
 

 
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2025 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 4/18/2025 9:44:43 AM. PLT: 0s