Results 1 to 11 of 11

Thread: How to use RecordSet in separate class object

  1. #1

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    How to use RecordSet in separate class object

    I have a class where I keep all of my data collected from the form.

    I am having trouble to use RecordSet inside the class object.

    Here is a sample of my application state class:

    Code:
    public class MyAppDataClass
         public db2Conn as ADODB.Connection
         public RecordSet1 as ADODB.RecordSet
         public field as String
    
         public sub New()
                db2Conn = New ADODB.Connection()
                db2Conn.Open("access 2000 connection string")
                db2Conn.CursorLocation = ADODB.CursorLocationEnum.adUseClient
    
         end sub 
    
         public function checkData() as boolean
              dim result as boolean = true
    
              try
                   sql = "some select query"
                   RecordSet1.Open(sql, db2Conn, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, ADODB.CommandTypeEnum.adCmdText)
              
                   if not RecordSet1.EOF then
                        field  = cstr(RecordSet1.Fields("field1").Value) ' it breaks with RecordSet is not an instance of an object when called from event handles
                         ' or it breaks with Database Object is closed
                   else
                        result = false
                   end if
    
                catch ex As Exception
                   result = false
                end try
    
              return result
         end function
    end class
    
    Private Sub myField_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles myField.KeyUp
          If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Up Or e.KeyCode = Keys.Enter Then
                e.Handled = False
            ElseIf Len(myField.Text) = 6 Then
     
                ' it breaks with RecordSet is not an instance of an object when called from event handles
                ' or it breaks with Database Object is closed
                if instanceOfMyAppDataClass.checkData() then 
                      ' TODO
                else 
                      ' TODO
                end if
           end if
    end sub
    If I call all of those RecordSet commands inside the event handler it works:

    Code:
    Private Sub myField_KeyUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles myField.KeyUp
          If e.KeyCode = Keys.Left Or e.KeyCode = Keys.Up Or e.KeyCode = Keys.Enter Then
                e.Handled = False
            ElseIf Len(myField.Text) = 6 Then
     
                try
                   sql = "some select query"
                   RecordSet1.Open(sql, db2Conn, ADODB.CursorTypeEnum.adOpenForwardOnly, ADODB.LockTypeEnum.adLockReadOnly, ADODB.CommandTypeEnum.adCmdText)
              
                   if not RecordSet1.EOF then
                        field  = cstr(RecordSet1.Fields("field1").Value) 
                   end if
    
                catch ex As Exception
                  ' print exception to some output
                end try
           end if
    end sub
    What should I do or what do I do wrong when using RecordSet from inside other class?
    Last edited by kutlesh; Mar 25th, 2019 at 06:08 AM.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: How to use RecordSet in separate class object

    if instanceOfMyAppDataClass.checkData() then
    What is instanceOfMyAppDataClass?

    Try

    Code:
    If Me.checkData() Then
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to use RecordSet in separate class object

    Why are you using a Recordset at all? If you're going to use .NET then you really ought to use .NET, which means ADO.NET for data access.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Re: How to use RecordSet in separate class object

    Code:
    Dim instanceOfMyAppDataClass as MyAppDataClass
    
    Private Sub frmSV50_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
          instanceOfMyAppDataClass = new MyAppDataClass()
    end sub

  5. #5

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Re: How to use RecordSet in separate class object

    Because it all went in that direction... and nobody can guide me trough the learning process of ADO.NET, neither anybody has bothered to learn it in this company...

  6. #6

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Re: How to use RecordSet in separate class object

    From these: https://docs.microsoft.com/en-us/dot...-code-examples

    which one should I use to connect to IBM DB2 32bit database?

    ODBC, or again some Access 2000 combo with OleDb?

    But IBM DB2 version here is old, like 20 years, and it might not work if i don't use Access 2000 bridge.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to use RecordSet in separate class object

    If you are not allowed use ADO.NET then that's one thing but, if you are, do you really need to be guided? There's plenty of tutorials around. It might just be the most widely discussed area of .NET programming. There's even a Database FAQ link in my signature below that leads to a number of useful links on this very site. If you work your way through literature that covers the basics, then you can ask specific questions if and when you encounter specific issues.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: How to use RecordSet in separate class object

    One of the issues with using a RecordSet is that nobody uses them, so finding somebody who can give you any meaningful suggestions is relatively difficult. You may actually get better suggestions in the ClassicVB forum, though if you mention that you are using .NET, they'll immediately want the thread moved.
    My usual boring signature: Nothing

  9. #9

    Thread Starter
    Addicted Member kutlesh's Avatar
    Join Date
    Jun 2018
    Location
    Skopje, Macedonia
    Posts
    211

    Re: How to use RecordSet in separate class object

    Hmm, I will be forward thinking and eventually replace all RecordSet statements with ADO.NET replacement.
    The time does not allow to go that far.

    Is ADO.NET DataSet appropriate way to go?

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,102

    Re: How to use RecordSet in separate class object

    A dataset is just a collection of datatables.

    The datatable offers somewhat more than the recordset did, if I remember right. The idea behind a datatable was that you fill it, then detach from the database, and only interact with the database again when you are ready to push the data back. I believe the recordset maintained the connection while the recordset was in use, though I could easily be wrong about that.

    The other option is the DataReader, which may act more like a recordset, but it is forward-only and read-only, so you can't use it for updating data (though you CAN use it to fill a datatable), and you can't use it for going both backwards and forwards through the data.
    My usual boring signature: Nothing

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: How to use RecordSet in separate class object

    ADO.NET vs ADO follows a similar pattern to many areas when coming to VB.NET from VB6 in that VB6 used to use one type/object to do everything while VB.NET makes use of several smaller types/objects that each specialise in one part of the process. In ADO.NET, a DataSet is basically an in-memory representation of a database. It contains DataTables, which correspond to database tables, and DataRelations, which correspond to database relations. A DataTable contains DataColumns that describe the data and DataRows that contains the data, just as a database table contains columns and rows. You will generally use a data adapter to move data back and forth between a database and a DataTable. The data adapter contains up to four command object that correspond to the SQL SELECT, INSERT, UPDATE and DELETE statements. The command objects each refer to a connection object that represents the connection to the database. In VB6, all that is rolled up into the Recordset. The monolithic approach may seem easier and it is for those whose understanding of the details is relatively minimal. Once you're used to it, using the multiple types is pretty simple and it tends to be a bit more flexible as you only use the specific parts you need for any particular job.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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
  •  



Click Here to Expand Forum to Full Width