RecordSets In ADO for VB6
-
I am having a problem in vb 6 in trying to access the content of recordsets which store the results of a query. When I try to pull the value out of the recordset as in dim value = rs!key, I get an error saying that the recordset is empty (I test the BOF and EOF and I get both to be true for the rs), - (the code crashes and gives this error at rs.MoveFirst), however when i click on debug and go into the code, the rs fills and the code continues to the end successfully. I am also getting this problem in a different context where in trying to insert records into a table, recordsets are updating and reading too slowly so old values are being inserted into tables generating errors. I only get these errors when I run the code, but If i step through I get NO errors! Please help!:((
-
I am having a problem in vb 6 in trying to access the content of recordsets which store the results of a query. When I try to pull the value out of the recordset as in dim value = rs!key, I get an error saying that the recordset is empty (I test the BOF and EOF and I get both to be true for the rs), - (the code crashes and gives this error at rs.MoveFirst), however when i click on debug and go into the code, the rs fills and the code continues to the end successfully. I am also getting this problem in a different context where in trying to insert records into a table, recordsets are updating and reading too slowly so old values are being inserted into tables generating errors. I only get these errors when I run the code, but If i step through I get NO errors! Please help!:((
If rs.BOF AND rs.EOF are both true then the rs is either nothing or empty. In which case don't let your code use the rs. Sounds like the underlying db has just been populated and you are attempting to get records from it before the db has finished updating itself i.e. after data has been sent to it. Are you using Access by any chance? Try putting a wait in your code after the db has been populated.
-
If rs.BOF AND rs.EOF are both true then the rs is either nothing or empty. In which case don't let your code use the rs. Sounds like the underlying db has just been populated and you are attempting to get records from it before the db has finished updating itself i.e. after data has been sent to it. Are you using Access by any chance? Try putting a wait in your code after the db has been populated.
Take a look at this URL for your solution: http://www.blackbeltvb.com/index.htm?free.htm
-
If rs.BOF AND rs.EOF are both true then the rs is either nothing or empty. In which case don't let your code use the rs. Sounds like the underlying db has just been populated and you are attempting to get records from it before the db has finished updating itself i.e. after data has been sent to it. Are you using Access by any chance? Try putting a wait in your code after the db has been populated.
-
Yes I am using an Access Db. Could you give me an example of using the wait code for recordsets or after the db has been populated? I was looking on the net and could not find anything! Thanks so much for your help! Looking forward to your response!
The correct solution to your problem would be to open an asyncronous connection and then when you have written your data to the db wait until the connection has finished executing the SQL command. Use the ADO const
dbRunAsync
when opening the connection to get an asyncronous connection. Then write the data to the db, and wait by using a loop such as:Do While Con.StillExecuting ... Loop
When the loop exits the db has been updated and you can then retrieve the required data. HTH -
I am having a problem in vb 6 in trying to access the content of recordsets which store the results of a query. When I try to pull the value out of the recordset as in dim value = rs!key, I get an error saying that the recordset is empty (I test the BOF and EOF and I get both to be true for the rs), - (the code crashes and gives this error at rs.MoveFirst), however when i click on debug and go into the code, the rs fills and the code continues to the end successfully. I am also getting this problem in a different context where in trying to insert records into a table, recordsets are updating and reading too slowly so old values are being inserted into tables generating errors. I only get these errors when I run the code, but If i step through I get NO errors! Please help!:((
I am attempting to connect to an Access Database with not much data and am using MDAC version 2.70.7713.4...I have worked around the problem. I noticed that the record I was querying for and then using movefirst to retrieve, was added immediately prior to performing the query for the value associated with the record added. Before: 'If record did not exist then add record Dim rs As New ADODB.Recordset SQL = "TableName" rs.Open SQL, dbMasterConn, adOpenKeyset, adLockOptimistic, adCmdTable rs.Addnew rs!code = varCode rs.Update 'Following this a rs was filled for generic instances where record did or did not exist for the record just added. Record was only added if it did not exist but the code is needed regardless of whether record did or did not exist Dim rs As New ADODB.RecordSet rs.Source = "SELECT code FROM TableName where condition" rs.CursorType = adOpenForwardOnly rs.ActiveConnection = dbMaster rs.Open rs.MoveFirst <--it stuck here CodeNeded = rs!code After changes: 'If record did not exist then Dim rs As New ADODB.Recordset rs.ActiveConnection = MasterDb SQL = "TableName" rs.Open SQL, , adOpenKeyset, adLockOptimistic, adCmdTable rs.Addnew rs!code = varCode rs.Update CodeNeeded = varCode else(if record already exists) Dim rs As New ADODB.RecordSet rs.Source = "SELECT code FROM TableName where condition" rs.CursorType = adOpenForwardOnly rs.ActiveConnection = dbMaster rs.Open rs.MoveFirst <--no longer stuck here and ran. CodeNeded = rs!code A very good detailed link that helped with making connections, creating recordsets, reading and adding values to recordsets etc for both DAO and ADO was: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnacc2k/html/acchap2.asp