473,394 Members | 1,971 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Recordset multiple open and close

Hello helper people who are smarter than me:
I have a form that needs to submit multiple queries to different tables
during one Sub's execution. Some sections are as simple as:

1| With rst
2| .Open query1
3| .Close
4| .Open query2
5| End With

However, what I run into is that .Open and .Close don't always get
triggered, or maybe the state isn't set, but I do know I frequently run
into the error "Method not allowed when the object is open" message at
places like line 4.

I take the next step: run an empty loop that waits for the Recordset to
Close before going on:

1| With rst
2| .Open query1
3| .Close
4| Do While .State <> adStateClosed
5| Loop
6| .Open query2
7| End With

Problem is, I get stuck in an infinite loop, because the recordset
never closes, even though ".Close" was the last command read before
going into the loop?

So, what would cause .Close not to close?

Apr 19 '06 #1
6 6786
bl*****@carolina.rr.com wrote:
Hello helper people who are smarter than me:
I have a form that needs to submit multiple queries to different tables
during one Sub's execution. Some sections are as simple as:

1| With rst
2| .Open query1
3| .Close
4| .Open query2
5| End With

However, what I run into is that .Open and .Close don't always get
triggered, or maybe the state isn't set, but I do know I frequently run
into the error "Method not allowed when the object is open" message at
places like line 4.

I take the next step: run an empty loop that waits for the Recordset to
Close before going on:

1| With rst
2| .Open query1
3| .Close
4| Do While .State <> adStateClosed
5| Loop
6| .Open query2
7| End With

Problem is, I get stuck in an infinite loop, because the recordset
never closes, even though ".Close" was the last command read before
going into the loop?

So, what would cause .Close not to close?

So why are you closing the recordset prior to opening query2? You might
be better op opening rst with query1, closing. Then create a new
With/Endwith set for query2.
Apr 19 '06 #2
Instead of opening/closing queries you could write the queries directly
in your loop using an array of select statements from your Form's code
module:

Sub RunMultipleQueries()
Dim arrQ As Variant, i As Integer, j As Integer, k As Integer
Dim DB As DAO.Database, RS As DAO.Recordset

arrQ = Array("Select fldx From tbl1 Where fldy = '" & txtSomething &
"'", "Select fldx From tbl2 Where fldy = '" & txtSomethingElse & "'")

Set DB = CurrentDB
For i = 0 to Ubound(arrQ)
Set RS = DB.OpenRecordset(arrQ(i))
Do While Not RS.EOF
'Do your stuff
RS.MoveNext
Loop
RS.Close
Next
End Sub

As you can see, this procedure also accommodates/uses parameters very
easily. If you want to run this procedure from a standard module
instead of a Form module, you can replace txtSomething/SomethingElse
with string variables that you could either pass in to the procedure or
use global string vars.

Sub RunMultipleQueries(strSomething As String, strSomethingElse As
String)
...
End Sub
Rich

*** Sent via Developersdex http://www.developersdex.com ***
Apr 19 '06 #3
I get an error when I consecutively call
..Open query1
..Open query2
citing that you can't open an open recordset. Makes sense to me.
The problem is, sometimes the ".Close" command between gets ignored,
and I get the error anyway.

It's not a matter of how I should construct the queries; these queries
are often very different, and setting up a loop to construct the
queries would be impractical, if not impossible - and again, that's not
the problem. Put in simple terms, the problem is this:

Bob: "Open a recordset based on query 1."
PC: "OKAY."
Bob: ".....ok, that's done, now close the recordset."
PC: "OKAY."
Bob: "And now open the same recordset using query 2."
PC: "I CAN'T. RECORDSET STILL OPEN."
Bob: "WT......?"

Apr 20 '06 #4
bl*****@carolina.rr.com wrote:
I get an error when I consecutively call
.Open query1
.Open query2
citing that you can't open an open recordset. Makes sense to me.
The problem is, sometimes the ".Close" command between gets ignored,
and I get the error anyway.

It's not a matter of how I should construct the queries; these queries
are often very different, and setting up a loop to construct the
queries would be impractical, if not impossible - and again, that's not
the problem. Put in simple terms, the problem is this:

Bob: "Open a recordset based on query 1."
PC: "OKAY."
Bob: ".....ok, that's done, now close the recordset."
PC: "OKAY."
Bob: "And now open the same recordset using query 2."
PC: "I CAN'T. RECORDSET STILL OPEN."
Bob: "WT......?"

Instead of being tricky why not open/close in steps
Set rst = New ADODB.Recordset
With rst
.Open "Query1"
If .State = adStateOpen Then
.Close
End If
End With
set rst = Nothing
Set rst = New ADODB.Recordset
With rst
.Open "Query2"
If .State = adStateOpen Then
.Close
End If
End With
set rst = Nothing
Apr 20 '06 #5
bl*****@carolina.rr.com wrote in message
<11**********************@i40g2000cwc.googlegroups .com> :
Hello helper people who are smarter than me:
I have a form that needs to submit multiple queries to different
tables during one Sub's execution. Some sections are as simple as:

1| With rst
2| .Open query1
3| .Close
4| .Open query2
5| End With

However, what I run into is that .Open and .Close don't always get
triggered, or maybe the state isn't set, but I do know I frequently
run into the error "Method not allowed when the object is open"
message at places like line 4.

I take the next step: run an empty loop that waits for the Recordset
to Close before going on:

1| With rst
2| .Open query1
3| .Close
4| Do While .State <> adStateClosed
5| Loop
6| .Open query2
7| End With

Problem is, I get stuck in an infinite loop, because the recordset
never closes, even though ".Close" was the last command read before
going into the loop?

So, what would cause .Close not to close?


Why are you opening recordsets when you don't do anything with them?

Have you hidden what you do with the recordsets, or are you opening
recordsets to execute action queries?

If the latter, consider trying something like this

With yourconnection
.execute "query1", , adcmdstoredproc + adexecutenorecords
.execute "query2", , adcmdstoredproc + adexecutenorecords
End With

--
Roy-Vidar
Apr 20 '06 #6
Found my issue: I was running action queries, but not running .Update
on the Recordset afterward. As soon as I entered that command,
everything ran fine. Odd that I had remembered that sometimes and
forgotten it at others. Guess that's what I get for being so darn
human. :)

May 2 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

23
by: Rob Meade | last post by:
Lo all, Ok - this is what I was aiming to do, and then I thought - naahhh, that cant be right! query database results to recordset results to array using GetRows update values in one column...
8
by: dmiller23462 | last post by:
My brain is nuked....Can anybody tell me right off the bat what is wrong with this code? Along with any glaring errors, please let me know the syntax to display a message (Response.Write would be...
1
by: berlinbrown | last post by:
What is the best way to open run a query and then open a recordset, for example. Set rst = exDatabase.OpenRecordset(sql) ' Run Query .... .... ....
3
by: alex_peri | last post by:
Hello All, I am having problems with sorting a recordset by fields in Access. I have a table with three columns called ID, SNo and Time and would like to sort the records by Time. I would like to...
2
by: Corey | last post by:
I am missing something here. I have a pop up form (loads from the "main form")that displays multiple command buttons. When a user selects a particular button, the recordset from the main form...
13
by: Jan | last post by:
Hi I have a database that I use to keep track of the sales promotions that we send to companies. I normally send a mailing based on a subset of the companies in the database (found using the...
18
by: Darryl Kerkeslager | last post by:
When I open an ADO Recordset, I close it. However, it seems that there may be some difference in this manner of opening a Recordset: Dim rL As ADODB.Recordset Set rL = New ADODB.Recordset ...
36
by: kjvt | last post by:
Based on a prior posting, I've written a function to convert a recordset to a dataview. The first call to the function for a given recordset works perfectly, but the second call always returns a...
23
by: PW | last post by:
Hi, I'd like to close a recordset and set the database to nothing if a recordset is open if an error has occured. Leaving a recordset open and a database open isn't a good idea, right? ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.