Results 1 to 9 of 9
  1. #1
    Persist is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jul 2010
    Location
    Melbourne Australia
    Posts
    32

    Questions on libraries, options, close & nothing statements using DAO

    Questions about one brief subroutine using DAO, concerning the:


    . Options statements
    . Close statements
    . Nothing statements
    . Object libraries used

    I have inserted the questions in the following code - as comments marked by ****.

    This is new to me, so I’d welcome other comments about this code.

    Code:
    Option Compare Database
    Option Explicit
    ‘ ******** Are these Options statements ok
    
    Private Sub btnTestCode_Click()
    On Error GoTo Err_btnTestCode_Click
     
       Dim dbsDoList As DAO.Database
       ' DAO.database user-defined type was initially not defined
       ' I fixed this with Tools > References > tick Microsoft DAO 3.6 object library
       ' Microsoft ActiveX data objects 2.1 library also ticked
    ‘ ***************** Should I untick ActiveX library
    
       Dim rsTasks As DAO.Recordset
       Dim strSQL As String
    
       Set dbsDoList = CurrentDb
     
       ' Open a recordset for all records from table tDoList
       ' The Fields of table tDoList include: Subject, Priority
       
       strSQL = "SELECT Subject, Priority FROM tDoList ORDER BY Priority"
       Set rsTasks = dbsDoList.OpenRecordset(strSQL, dbOpenDynaset)
     
       With rsTasks
          Do Until .EOF
          
            If ![Subject] = "xx" Then
                .Edit
                ![Priority] = 999977
                .Update
            End If
                
            .MoveNext
            
          Loop 
       End With 
     
    
    Exit_btnTestCode_Click:
    
       rsTasks.Close
       dbsDoList.Close
     
       Set rsTasks = Nothing
       Set dbsDoList = Nothing
    
        ‘ ***** Are these 4 statements in the right place
        ‘ ***** Are these 4 statements needed.
        Exit Sub
    
    Err_btnTestCode_Click:
        
        MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description
        ' vbCrLf = visual basic carriage return line feed
        Resume Exit_btnTestCode_Click
        
    End Sub
    I am running Microsoft Visual Basic for Applications 7.0” Copyright 2010

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    Those two option statements, at a minimum, should be in every code module.

    ActiveX library is needed if you are declaring objects from that library. Uncheck then Compile the code and see what happens.

    It is considered good practice when objects are set and opened, to explicitly close and destroy when no longer needed. VBA should do this automatically when the procedure ends but explicitly doing it is assurance.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    Persist is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jul 2010
    Location
    Melbourne Australia
    Posts
    32
    I did un-tick the Microsoft ActiveX data objects 2.1 library.
    I did not get compile errors - and my little database seeems to be running fine, so I will leave it un-ticked
    Thanks

  4. #4
    June7's Avatar
    June7 is offline VIP
    Windows 10 Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,770
    You will want the Microsoft ActiveX Data Objects x.x Library if you want to use ADODB objects in VBA.
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  5. #5
    Persist is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jul 2010
    Location
    Melbourne Australia
    Posts
    32
    I searched for pros and cons of DAO and ADODB.
    Somewhere I saw that Microsoft started with DAO, headed towards ADODB and now have moved back towards DAO.
    This is probably a great simplification, but it led me to try using DAO with no ADOBD. I may look at ADODB in the future.
    Thanks for your contributions.

  6. #6
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    The rule is
    If you create it, destroy it
    If you open it, close it.



    You created (declared) "dbsDoList ", but you did not open it. Therefore
    Code:
    
    bsDoList.Close             <-- do not need/ should not have
    Set dbsDoList = Nothing.   <-- NEED
    You created (declared) "rsTasks" AND you opened it. Therefore you need both
    Code:
    
    rsTasks.Close           <-- NEED
    Set rsTasks = Nothing   <-- NEED

    BTW, creating is using lines with the SET command
    Code:
    Set dbsDoList = CurrentDb                                     <-- Create
    Set rsTasks = dbsDoList.OpenRecordset(strSQL, dbOpenDynaset)  <-- Create AND Open

  7. #7
    Persist is offline Advanced Beginner
    Windows 7 64bit Access 2007
    Join Date
    Jul 2010
    Location
    Melbourne Australia
    Posts
    32
    I am wondering about the order of closing and setting to nothing. I have:
    rsTasks.Close
    Set rsTasks = Nothing

    This seems to be closing then destroying it - which sounds right.

    More excellent feedback - just what a novice DAO person needs.

    Thanks Steve.

  8. #8
    ssanfu is offline Master of Nothing
    Windows 7 32bit Access 2010 32bit
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Yep. That is the correct order: Close then destroy.

  9. #9
    Persist is offline Advanced Beginner
    Windows 10 Access 2010 32bit
    Join Date
    Jul 2010
    Location
    Melbourne Australia
    Posts
    32
    Here is the full code with the suggested changes and comments to explain.
    I've added about 5 more lines of code to make this subroutine the heart of a simple "Do List database"
    Only one table, tTasks with fields: Task, Priority, ActionDate, TaskID
    In this subroutine:
    . Tasks get sorted by priority and then re-allocated priorities 100, 110, 120 etc
    . Tasks = "xx" get priority 999999
    It is a short, useful example of DAO code
    Thanks for your help.

    Code:
    Private Sub btnresetPriorities_Click()
    On Error GoTo Err_btnresetPriorities_Click
     
       Dim dbsDoList As DAO.Database
       ' DAO.database user-defined type not defined
       ' Fix with Tools > References > Tick Microsoft DAO 3.6 object library
       ' I un-ticked Microsoft ActiveX Data Objects 2.1 library
       ' Access 2010 32 bit
    
       Dim rsTasks As DAO.Recordset
       Dim strSQL As String
     
       Dim NewPriority As Integer
       NewPriority = 100 ' Set first priority
     
       Set dbsDoList = CurrentDb
     
       ' Open a recordset for all records from tTasks
       ' tTasks Fields: Task, Priority, ActionDate
       
       strSQL = "SELECT Task, Priority FROM tTasks ORDER BY Priority"
       Set rsTasks = dbsDoList.OpenRecordset(strSQL, dbOpenDynaset)
     
       With rsTasks
          Do Until .EOF
          
            If ![Task] = "xx" Then
                .Edit
                ![Priority] = 999999
                .Update
            Else
                .Edit
                ![Priority] = NewPriority
                .Update
                NewPriority = NewPriority + 10
            End If
                
            .MoveNext
            
          Loop ' Do Until Loop
       End With ' rsTasks
         
    Exit_btnresetPriorities_Click:
    
       rsTasks.Close
       Set rsTasks = Nothing
       ' Set rsTasks = dbsDoList.OpenRecordset(strSQL, dbOpenDynaset)
       ' This line creates / declares rsTasks and opens it
       ' So at end, (1) close it and then (2) set it to nothing = destroy it
       
       ' Set dbsDoList = CurrentDb
       ' This line creates dbsDoList, so at end I set it to nothing = destroy it
       ' I did not open it, so I do not close it
       ' dbsDoList.Close
       Set dbsDoList = Nothing
       
       Exit Sub
    
    Err_btnresetPriorities_Click:
        
        MsgBox "Error #: " & Err.Number & vbCrLf & vbCrLf & Err.Description
        ' vbCrLf = visual basic carriage return line feed
        Resume Exit_btnresetPriorities_Click
        
    End Sub

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Which libraries for .Edit?
    By fogyreef in forum Programming
    Replies: 1
    Last Post: 05-26-2016, 06:58 AM
  2. Access VBA - Libraries and Plug-Ins HELP
    By adke001458 in forum Access
    Replies: 7
    Last Post: 04-07-2014, 11:53 AM
  3. Reference Libraries
    By dandoescode in forum Access
    Replies: 0
    Last Post: 06-07-2012, 07:07 AM
  4. Replies: 1
    Last Post: 11-14-2011, 07:03 PM
  5. TLB32 Type Libraries - DLL's, OLB's and EXE's Exposed
    By ajetrumpet in forum Code Repository
    Replies: 0
    Last Post: 12-30-2010, 02:44 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums