Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win


dao cannot open any more databases

Posted on 2008-09-29
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Instead of error trapping or hard-coding for non-updateable fields when using QODBC, let VBA automatically disable them when forms open. This way, users can view but not change the data. Part 1 explained how to use schema tables to do this. Part 2 h…
Learn how to number pages in an Access report over each group. Activate two pass printing by referencing the pages property: Add code to the Page Footers OnFormat event to capture the pages as there occur for each group. Use the pages property to …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Suggested Courses

636 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