Hi Richard! I try to open an record set within a recordset. It runs. But I can not close the second recordset.
Adam Schwanz
@Reply 5 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
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 SimonsenOP
@Reply 5 years ago
"Close". And the error say: Run-time error '91' Object variable or with block variable not set.
Kevin Robertson
@Reply 5 years ago
You are trying to close rsAid twice. Once inside the With block, then again after.
Kevin Robertson
@Reply 5 years ago
Just noticed the problem. You haven't closed your While loop.
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 SimonsenOP
@Reply 5 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 SimonsenOP
@Reply 5 years ago
I tried to close rsAID in two different places. Thats why there were double set of "Close rsAID".
Ole SimonsenOP
@Reply 5 years ago
Ole SimonsenOP
@Reply 5 years ago
This is the code Im using without the "With rsAID"
Ole SimonsenOP
@Reply 5 years ago
And finali I realiced that you did not see thee entire code :)
Next is the one that did not work.
OK, so what's the specific error message you're getting, and which line does it break on?
Ole SimonsenOP
@Reply 5 years ago
Hi Richard!
The error message: Run-time error '91' Object variable or with block variable not set.
Line: rsAID.Close
Scott Axton
@Reply 5 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
@Reply 5 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
@Reply 5 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 SimonsenOP
@Reply 5 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
@Reply 5 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 SimonsenOP
@Reply 5 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 SimonsenOP
@Reply 5 years ago
I do get errormessage: 3420 on rsAID.Close
Kevin Robertson
@Reply 5 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
@Reply 5 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
@Reply 5 years ago
Kevin Robertson
@Reply 5 years ago
Also rsAid is being closed in your With block. Remember to remove it too.
Ole SimonsenOP
@Reply 5 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
@Reply 5 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 SimonsenOP
@Reply 5 years ago
Hi Scott! THX. Il do that.
Sorry, only students may add comments.
Click here for more
information on how you can set up an account.
If you are a Visitor, go ahead and post your reply as a
new comment, and we'll move it here for you
once it's approved. Be sure to use the same name and email address.
This thread is now CLOSED. If you wish to comment, start a NEW discussion in
Access Developer 26.