Results 1 to 3 of 3

Thread: Newbie(ish) Question about class modules

  1. #1

    Thread Starter
    Member
    Join Date
    Apr 2001
    Posts
    36

    Newbie(ish) Question about class modules

    I am writing a fairly Maths-intensive application for an Engineering firm and am trying to based it on a database. I am using class modules to tie in with each table in the database and use the following code to connect (copied from COM tutorial 2):

    Code:
    Private Sub Class_Initialize()
    Set rs = New Recordset
        
    rs.ActiveConnection = "Provider=Microsoft." & _
        "Jet.OLEDB.4.0;Data Source=" & App.Path & _
        "\CentralDB.mdb;" & "Persist Security Info=False"
    
    rs.Open "select * from JointTypes", , adOpenKeyset, adLockOptimistic
    
    End Sub
    
    Private Sub Class_Terminate()
    rs.Close
    
    Set rs = Nothing
    
    End Sub
    I am trying to keep the number of global variables as low as possible. If the class module is called Joint then for example I will put this code locally and pass parameters between subroutines:

    Code:
    Dim uJoint as Joint
    Set uJoint= New Joint
    The program works very well, but it is too slow as it is having to connect to the database all the time and I need a way of keeping the CurrentRecord the same when switching between different subs, modules, forms etc as the program is quite complicated.

    This may seem a really elementary question but remember I am totally new at this (since yesterday when I read the database and COM tutorials)

    Thanks

  2. #2
    Randalf the Red honeybee's Avatar
    Join Date
    Jun 2000
    Location
    off others' brains
    Posts
    4,345

    Well ...

    You can use a collection of Joint type objects, called Joints. Then in this collection, you can open the required recordset. When you are done with the collection, ideally at the end of the program, you can close the recordset.

    In the meantime you can create objects of type Joint, which will be appended to the collection and will share the recordset in the collection.

    .
    I am not a complete idiot. Some parts are still missing.
    Check out the rtf-help tutorial
    General VB Faq Thread
    Change is the only constant thing. I have not changed my signature in a long while and now it has started to stink!
    Get more power for your floppy disks. ; View honeybee's Elite Club:
    Use meaningfull thread titles. And add "[Resolved]" in the thread title when you have got a satisfactory response.
    And if that response was mine, please think about giving me a rep. I like to collect them!

  3. #3
    Hyperactive Member mvrp350's Avatar
    Join Date
    Feb 2001
    Location
    Best, the Netherlands
    Posts
    322
    You said you want to keep the number of global variables as low as possible, but have you thouht about making the recordsets global.

    If you do that you're able to reuse a recordset, I also made a separate function to connect so I don't need to reconnect over and over again:

    To make the connection:
    VB Code:
    1. Sub connectDB(ByVal UserName As String, ByVal Password As String, ByVal DatabaseName As String)
    2.  
    3. 'identify the new connection
    4. Set con = New ADODB.connection
    5.  
    6. 'if the connection is closed, open it
    7. If con.State = adStateClosed Then
    8.     con.Provider = YourDBProvide
    9.     con.ConnectionString = YourConnectionString
    10.     con.Open
    11. End If
    12.    
    13. End Sub

    To open the recordset:
    VB Code:
    1. Sub OpenRec(ByVal rs As ADODB.Recordset, ByVal con As ADODB.connection, ByVal strSQL As String)
    2.  
    3. 'open the recordset on remote
    4. With rs
    5.     .ActiveConnection = con
    6.     .CursorLocation = adUseClient
    7.     .CursorType = adOpenStatic
    8.     .LockType = adLockBatchOptimistic
    9.     .Source = strSQL
    10.     .Open
    11. End With
    12.    
    13. 'disconnect recordset from connection
    14. Set rs.ActiveConnection = Nothing
    15.  
    16. End Sub

    keep in mind that my app runs on server/client, so I remove the connection to my recordset.

    To update my recordset:
    VB Code:
    1. Sub UpdateRec(ByVal rs As ADODB.Recordset, ByVal con As ADODB.connection)
    2.  
    3. 'reconnect recordset to connection
    4. Set rs.ActiveConnection = con
    5.  
    6. 'update recordset
    7. rs.MarshalOptions = adMarshalModifiedOnly
    8. rs.UpdateBatch
    9.  
    10. 'disconnect recordset from connection
    11. Set rs.ActiveConnection = Nothing
    12.  
    13. End Sub

    Usage:

    VB Code:
    1. 'To open a recordset
    2. Set YourRecSet = New ADODB.Recordset
    3. strSQL = "SELECT Blah Blah Blah ........."
    4. Call OpenRec(YourRecSet, con, strSQL)
    5.  
    6. 'Place your code here
    7.  
    8. 'To update the recordset
    9. UpdateRec YourRecSet,con

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