Results 1 to 11 of 11

Thread: [RESOLVED] Speed comparison with DAO and ADO

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    51

    Resolved [RESOLVED] Speed comparison with DAO and ADO

    I had an application developed under Access 97 and Excel 2003. I decided to bite the bullet and convert them to the 2007 I'd just installed.

    The old application format ran well in Excel 2007 (compatibility mode) without a hitch. That bought me time to test and learn.

    I first imported the old mdb file into the new accdb format. It worked well.

    I first tried to go to easy route by converting it under the new DAO methods. This is the code of a typical openrecordset code:

    Code:
    dim ws as DAO.Workspace
    Dim db as DAO.database
    
    Set ws = DBEngine.Workspaces(0)
    ws.OpenDatabase("MyDB", False, False)
    sql = "Select * from myTable Order by Name"
    set rs = ws.OpenRecordset(sql)
    
    TextBox1.Clear
    Do until rs.EOF
    TextBox1.AddItem rs!TableData
    rs.MoveNext
    Loop
    This code ran but it is very slow. The non-converted version (ran under 2007) was instantaneous on each click. But the new DAO version show the AddItem process, with flickers. The lag time is fraction of a second, but you can tell it is much slower than the old version.

    So, I convert them to the ADO method and here is the code:

    Code:
    Public conn as ADODB.Database
    
    Sub Loaddb()
        dbFile = "MyDB.accdb"
    
        Set conn = New ADODB.Connection
        conn.Provider = "Microsoft.ACE.OLEDB.12.0"
        conn.Open dbFile
    End Sub
    
    Sub Command_Click()
    Loaddb
    UserForm1.Show
    End Sub
    
    Sub UserForm1_Initialize()
        Dim sql As String
    
        sql = "Select * from myTable Order by Name"
        Set rs = New ADODB.Recordset
        rs.Open sql, conn, adOpenStatic, adLockOptimistic
    
        TextBox1.Clear
        
        Do until rs.EOF
            TextBox1.AddItem rs!TableData
            rs.MoveNext
        Loop
    End Sub
    The ADO code runs a lot faster than the ADO conversion. It is almost as fast as the unconverted version in compatibility mode.

    Any reasons? Something wrong with my DAO code?
    Last edited by ssal; Mar 11th, 2015 at 11:46 AM.

  2. #2
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Speed comparison with DAO and ADO

    Quote Originally Posted by ssal View Post
    This is the code of a typical openrecordset code:

    Code:
    dim ws as DAO.Workspace
    Dim db as DAO.database
    
    Set ws = DBEngine.Workspaces(0)
    ws.OpenDatabase("MyDB", False, False)
    sql = "Select * from myTable Order by Name"
    set rs = ws.OpenRecordset(sql)
    
    TextBox1.Clear
    Do until rs.EOF
    TextBox1.AddItem rs!TableData
    rs.MoveNext
    Loop
    This code ran but it is very slow
    indeed that code will be very slow,it will not even run

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    51

    Re: Speed comparison with DAO and ADO

    Aah! I guess I missed a little. Try the following:

    Code:
    dim ws as DAO.Workspace
    Dim db as DAO.database
    
    Set ws = DBEngine.Workspaces(0)
    Set db = ws.OpenDatabase("MyDB", False, False)
    sql = "Select * from myTable Order by Name"
    set rs = db.OpenRecordset(sql)
    
    TextBox1.Clear
    Do until rs.EOF
    TextBox1.AddItem rs!TableData
    rs.MoveNext
    Loop

  4. #4
    PowerPoster
    Join Date
    Dec 2004
    Posts
    24,972

    Re: Speed comparison with DAO and ADO

    is textbox1 actually a textbox?
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    51

    Re: Speed comparison with DAO and ADO

    Yes, textbox1 is a textbox control.

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,244

    Re: Speed comparison with DAO and ADO

    Since when does a TextBox-Control have the AddItem-and Clear-Methods??? *confused*
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    51

    Re: Speed comparison with DAO and ADO

    Quote Originally Posted by Zvoni View Post
    Since when does a TextBox-Control have the AddItem-and Clear-Methods??? *confused*
    Another mistake. It should be listbox

    Code:
    Dim ws as DAO.Workspace
    Dim db as DAO.database
    
    Set ws = DBEngine.Workspaces(0)
    Set db = ws.OpenDatabase("MyDB", False, False)
    sql = "Select * from myTable Order by Name"
    set rs = db.OpenRecordset(sql)
    
    ListBox1.Clear
    Do until rs.EOF
    ListBox1.AddItem rs!TableData
    rs.MoveNext
    Loop
    And

    Code:
    Public conn as ADODB.Database
    
    Sub Loaddb()
        dbFile = "MyDB.accdb"
    
        Set conn = New ADODB.Connection
        conn.Provider = "Microsoft.ACE.OLEDB.12.0"
        conn.Open dbFile
    End Sub
    
    Sub Command_Click()
    Loaddb
    UserForm1.Show
    End Sub
    
    Sub UserForm1_Initialize()
        Dim sql As String
    
        sql = "Select * from myTable Order by Name"
        Set rs = New ADODB.Recordset
        rs.Open sql, conn, adOpenStatic, adLockOptimistic
    
        ListBox1.Clear
        
        Do until rs.EOF
            ListBox1.AddItem rs!TableData
            rs.MoveNext
        Loop
    End Sub

  8. #8
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Speed comparison with DAO and ADO

    Public conn as ADODB.Database
    since when does ADO contain a 'Database' object ?

    If your aim is to optimize your db for speed, you will get the most benefit, if you do it at the db level
    the method to access the db comes a far second
    use the access method you are most comfortable with, and know the best

    also if you want to compare the speed of different access methods, at least let them do the same thing
    a fully dynamic cursor is a completely different thing than a static cursor

    also you do not have to choose between ADO or DAO, you can use them both at the same time

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    51

    Re: Speed comparison with DAO and ADO

    Another mistake

    It should be

    Code:
    Dim conn as adodb.connection
    Was my DAO code in dynamic and my ADO code static cursor?

    How you open ADO recordset with static cursor?

  10. #10
    Frenzied Member
    Join Date
    Jun 2014
    Posts
    1,084

    Re: Speed comparison with DAO and ADO

    since you are using access 2007: reading matter:
    https://msdn.microsoft.com/en-us/lib...ffice.12).aspx
    also helpfull is googling for 'ADO tutorial' and 'DAO tutorial'

  11. #11

    Thread Starter
    Member
    Join Date
    Mar 2015
    Posts
    51

    Re: Speed comparison with DAO and ADO

    Quote Originally Posted by IkkeEnGij View Post
    since you are using access 2007: reading matter:
    https://msdn.microsoft.com/en-us/lib...ffice.12).aspx
    also helpfull is googling for 'ADO tutorial' and 'DAO tutorial'
    Thanks you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width