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:
If I call all of those RecordSet commands inside the event handler it works: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
What should I do or what do I do wrong when using RecordSet from inside other class?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




Reply With Quote
