Results 1 to 11 of 11

Thread: How to use RecordSet in separate class object

Threaded View

  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.

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