Solved

dao cannot open any more databases

Posted on 2008-09-29
5
229 Views
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,
    T
0
Comment
Question by:TongZ
  • 3
  • 2
5 Comments
 
LVL 18

Expert Comment

by:mdougan
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.
0
 

Author Comment

by:TongZ
ID: 22598420
mdougan,
    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?
   T
0
 
LVL 18

Expert Comment

by:mdougan
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.Clear
    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
        lrs.MoveLast
        lrs.MoveFirst
        While Not lrs.EOF
            cboCharacter.AddItem ValidString(lrs("CHARACTERNAME"))
            cboCharacter.ItemData(cboCharacter.NewIndex) = ValidNum(lrs("CHARACTERCODE"))
            lrs.MoveNext
        Wend
    End If
   
    cboCharacter.Text = SAVE
   
    lrs.Close
    lqd.Close
   
0
 

Accepted Solution

by:
TongZ earned 0 total points
ID: 22605052
mdougan
    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)
    fldRec.MoveLast()
    fldRec.MoveFirst()

    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...

   Thanks
   T
   
   
0
 
LVL 18

Expert Comment

by:mdougan
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.

Cheers!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now