dao cannot open any more databases

Posted on 2008-09-29
Last Modified: 2013-12-25
Hi all,
    I've been working on this project to convert this VB6 Access DB project to .NET. Things are going OK except I have run into persistent dao cannot open any more databases error.
    I have diligently tracked down every openrecordset/db connection to close them down and set to nothing after use, but it still pop out upon large number of queries. Could anyone please give some pointers as to what to do next?
    I begin to suspect whether the recordset.close truly close the connection etc or is it that's an intrinsic trait of Microsoft Access cannot handle repeated open/close etc (I think I read somewhere that is the case and microsoft said it is by design...)
    Anyway, looking forward to some helpful hints,
Question by:TongZ
  • 3
  • 2
LVL 18

Expert Comment

ID: 22597862
Are you trying to use the DAO under .NET?

I know that under VB6, there were issues, depending on the "cursortype".  If it was a server-side cursor, you had to read all the way to the end of the recordset before the cursor would actually close... so, you could try doing a MoveFirst then MoveLast just prior to doing your Close and then setting to Nothing.

But, under .NET, I wouldn't recommend using DAO.  Rather, you should be working with SQLCommand objects, Dataset objects and DataReader objects.

Author Comment

ID: 22598420
    I agree that dao shouldn't be used. However there are way too many places in the program to do quick conversion to the .NET way (ADO ?)
   The program already did the movefirst/movelast combo thing to force populate recordset... I am not sure I understand the implication of the different "cursortype" when openrecordset. Could you give a quick explanation as to which one can be more "reliably" closed afterwards?
LVL 18

Expert Comment

ID: 22599891
Well, it is hard to remember which of my old programs uses DAO and which uses ADO... most people made the switch many years ago.  Here is one routine that I think uses DAO.  Our database was SQL Server, and so, we wrote all of our SQL in Stored Procedures, so, to execute a Stored Procedure, you create a QueryDef object, and you do a DB_SQLPASSTHROUGH call, which tells DAO not to try to interpret the Query, but rather just pass it along to SQL Server to worry about.

In the OpenRecordset statement, the dbOpenSnapshot  flag specifies the "CursorType".  This type of cursor will be a readonly cursor, as it is just a snapshot of the data... not to be used for updating.  Not sure what the "CursorLocation" for this would be, but would guess something like  dbUseClient, meaning that it should bring all the results back to the client right-away.  The other option is dbUseServer which can be more efficient, in that it only brings back some of the data to the client right-away, and then it will bring down the rest as you movenext through the recordset.

We opened our database at the beginning of the program, and kept it open the whole time, until the end of the program.  Whenever we created the QueryDef object, we associated it with the database by using our database variable db.... so, for example

Dim lqd as QueryDef
lqd = db.CreateQueryDef(......

Are you opening and closing your database after each query?  Or maybe you are opening a connection to the db before each query, and then not closing the db object when done?

Dim SAVE As String
Dim lrs As Recordset
Dim lqd As QueryDef
Dim Parms As Variant
Dim sErrors As String
Dim lError As Variant

    On Error GoTo FillCharactersErr
    SAVE = cboCharacter.Text
    cboCharacter.AddItem "[None]"
    cboCharacter.ItemData(cboCharacter.NewIndex) = 0
    Parms = Array(gUserShow, Mode)
    ' this is a function we wrote that does a CreateQueryDef statement
    PrepareQuery lqd, "SPC_SEL_CHARACTER_BY_SHOW", Parms

    Set lrs = lqd.OpenRecordset(dbOpenSnapshot, DB_SQLPASSTHROUGH)

    If lrs.RecordCount <> 0 Then
        While Not lrs.EOF
            cboCharacter.AddItem ValidString(lrs("CHARACTERNAME"))
            cboCharacter.ItemData(cboCharacter.NewIndex) = ValidNum(lrs("CHARACTERCODE"))
    End If
    cboCharacter.Text = SAVE

Accepted Solution

TongZ earned 0 total points
ID: 22605052
    Thanks for the explanation.
    I've resolved the problem. Here is what happened:

   I have this function that would open a recordset like the following (to a MDB) at the beginning of a function (which gets called numerous times repeatedly):

    fldRec = stateDB.OpenRecordset(Me.YearFields, dao.RecordsetTypeEnum.dbOpenDynaset)

    Then there are a bunch of complicated actions taken through many conditional branches. I closed the recordset at the very end of the function, however, there are Return(s) somewhere midway in the function that I didn't catch, hence, whenever it reaches the end of the function, the recordset is closed, if not, then that connection is still open, hence the problem.

    I think recordset.Close() would indeed close the actual connection in the dao world, so this means, you can have a persistent MDB handle (through OpenDatabase call), but it is the OpenRecordset that is opening the connection, which needs to be closed after each use.

   I would assign partial points for your prompt response and clear explanation, but not sure how to do that though...

LVL 18

Expert Comment

ID: 22605115
Hi TongZ

No worries, you can feel free to delete the question and ask that your points be refunded.  Glad you found your solution!

I always added the code to close the recordsets just before the return, because you could logically see what recordsets would be open at that time.  Some people will code a label at the bottom of the routine and GoTo it in all cases, and the close statements would follow that label, but then others don't like using GoTos.


Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
In Microsoft Access, learn different ways of passing a string value within a string argument. Also learn what a “Type Mis-match” error is about.
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

822 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question