Results 1 to 11 of 11

Thread: [RESOLVED] Proper way to open recordset/connection to the database

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Resolved [RESOLVED] Proper way to open recordset/connection to the database

    Hi,
    Please help me with a proper way (better or more flexible) to open/close recordset and connection to the Access database.

    My requirements:
    I need to be able to replace the .mdb file any time without closing the program, so I'm opening and closing the connection each time I need to access the db (no opening in Form_Load() and closing in Form_Unload())

    1. Open db
    2. Read or Write data
    3. Close db

    Here's the way I do it now:
    Should I split the OpenStatsDB() to OpenConnection() and OpenRecordset() to gain flexibility. Is that the proper way to do this?

    Code in Module
    Code:
    Public con As New ADODB.Connection
    Public rec As New ADODB.Recordset
    
    Public Function OpenStatsDB(sEvent As String) As Boolean
      Call CloseStatsDB
      
      con.Open "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & App.Path & STATS
      
      If sEvent = "" Then
        rec.Open "SELECT * FROM myTable", con, adOpenStatic, adLockOptimistic
      Else
        rec.Open "SELECT * FROM myTable WHERE Event = '" & sEvent & "'", con, adOpenStatic, adLockOptimistic
      End If
    
      If rec.BOF And rec.EOF Then
        OpenStatsDB = False
      Else
        OpenStatsDB = True
      End If
    
    End Function
    
    Public Sub CloseStatsDB()
      If rec.State = adStateOpen Then rec.Close
      If con.State = adStateOpen Then con.Close
    End Sub

    Code in form
    Code:
    Private Sub Command1_Click()
      On Error GoTo Leave
       
      If OpenStatsDB("") Then
        ' do my updates here
        text1.text = rec.fields[0].value
      End If
      Call CloseStatsDB
        
      Exit Sub
      
    Leave:
      Call CloseStatsDB
    End Sub
    Thanks

    Tomexx.

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Proper way to open recordset/connection to the database

    You should always have a separate sub to open/close the connection.

    A recordset object should be declared up front, and then used, and closed on the fly, as needed.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: Proper way to open recordset/connection to the database

    Thanks for replaying Hack,
    Could you show a little sample?
    Thanks

    Tomexx.

  4. #4
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Proper way to open recordset/connection to the database

    A sample of what?

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: Proper way to open recordset/connection to the database

    The right way declaring, openning, pulling data, closing recordset/connections. Something that would be easy to reuse and flexible (used with different tables, being able to update different fields etc)
    Thanks

    Tomexx.

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Proper way to open recordset/connection to the database

    Something like this for the connection.
    vb Code:
    1. Option Explicit
    2. Public ADOCn As ADODB.Connection
    3. Public ConnString As String
    4. Public adoRS As ADODB.Recordset
    5.  
    6. Public Sub ConnectDB(strStatus As String)
    7. If strStatus = "Open" Then
    8.    ConnString = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
    9.         "Data Source=c:\myAccessDB.mdb;" & _
    10.         "Persist Security Info=False"
    11.  
    12.    Set ADOCn = New ADODB.Connection
    13.    ADOCn.ConnectionString = ConnString
    14.    ADOCn.Open ConnString
    15. Else 'assume close connection
    16.    ADOCn.Close
    17.    Set ADOCN = Nothing
    18. End If
    19. End Sub
    20.  
    21. Private Sub Form_Load()
    22. ConnectDB "Open"
    23. End Sub
    24.  
    25. 'somewhere else where you want to close your connection
    26. ConnectDB "Close"
    Then, when you want to run your queries, just do something like
    vb Code:
    1. Dim sSQL As String
    2. Set adoRS = New ADODB.Recordset
    3.  
    4. sSQL = "SELECT blah blah blah FROM table "
    5. adoRS.Open sSQL, ADOCn
    6. 'do something with the resulting recordset
    7. adoRS.Close
    8. Set adoRS = Nothing
    9. 'repeat as necessary throughout you your program for SELECTS
    10. 'for action queries like updates do
    11. Dim sSQL As String
    12. sSQL = "UPDATE tablename SET field = 'Blah'
    13. ADOCn.Execute sSQL
    Is this what you mean?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: Proper way to open recordset/connection to the database

    Ok, thanks,

    Where would I use "action query"?
    I see that you use that directly with the connection object as opposed to the recordset.
    Thanks

    Tomexx.

  8. #8
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Proper way to open recordset/connection to the database

    With access you need to be careful of processing the way you are...

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 1999
    Location
    Ont, Canada, Earth
    Posts
    458

    Re: Proper way to open recordset/connection to the database

    randem,
    what do you mean?
    Thanks

    Tomexx.

  10. #10
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Proper way to open recordset/connection to the database

    Action queries are Insert, Delete and Updates. When you return data from the database you use recordsets
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  11. #11
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: Proper way to open recordset/connection to the database

    Opening an closing an Access database repeatedly like you do you will need to constantly repair and compact it to avoid corruption depending on how frequently you are doing such a process. Access is not build for repeated processes such as that in a short period of time.

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