How to read a recordset file in VB6?

Posted on 2001-08-08
Last Modified: 2008-03-03
I am using VB6 and having trouble reading a recordset file created from an Access 97 database. The ExportData code below creates a file of what appears to be appropriate size. However, when I try to read the file in UpdateDB, the second line (rsInput.MoveFirst) generates error 3021, Either BOF or EOF is true.  The size of the file indicates that records are there. I have used similar code in other programs successfully. What is wrong here?

Dim msFileName as String            '-- name of quarterly export/import file
Dim mrsRun As New ADODB.Recordset   '-- recordset for used to create file
Dim mrsInput As New ADODB.Recordset  '-- recordset for to read data from file

Private Sub ExportData()
'-- export data to a recordset file
  '-- assign file name  
  msFileName = "QData" & ".rs" '-- let extension show file type  
  '-- dump the data to a recordset
  sSQL = "Select * from tblRun;"  
  Set mrsRun = New ADODB.Recordset
  mrsRun.CursorType = adOpenDynamic
  mrsRun.LockType = adLockOptimistic
  mrsRun.CursorLocation = adUseClient
  mrsRun.Open sSQL, gdbConnection  
  '-- save recordset as a file
  mrsRun.Save msFileName
End Sub

Sub UpdateDB()      
  '-- open input file
  rsInput.Open msFileName, , , , adCmdFile     'This is the file created from tblRun
  rsInput.MoveFirst    <==== CRASHES HERE
End Sub
Question by:daquin
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 70

Expert Comment

by:Éric Moreau
ID: 6365623
The recordset is empty.

You can do something like:
if rsimput.recordcount>0 then rsinput.movefirst

Also, to open the file, use this shorter syntax:
rsInput.Open msFileName

Author Comment

ID: 6365701
I tried dumping data from different tables and got file sizes ranging from 2K to 40K. This make me believe the data is there, so why is the recordset empty?  
LVL 70

Expert Comment

by:Éric Moreau
ID: 6365748
Maybe it is just the header that is saved! Open the file into notepad. You will see some scarbled data but other will be readable.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 6365826
About 6 characters are displayed when I view the file in Notepad.  I looked at some files generated in the same mannner by another program.  These file can be read by yet another program.  They look similar in notepad to the file generated in this program.  File size does not seem to affect the number of character displayed. I renamed one the files known to be readable and tried to read it with this program - got the same error message.  I am using the Microsoft ActiveX Data Objects Recordset 2.5 Library.  Am I missing something here?
LVL 75

Accepted Solution

Anthony Perkins earned 100 total points
ID: 6365961
I don't believe this is the issue, but one thing I notice is that the line

Dim mrsRun As New ADODB.Recordset  
should be
Dim mrsRun As ADODB.Recordset  

Since you are instantiating the recordset in code with:
Set mrsRun = New ADODB.Recordset

A suggestion that would help deterine the problem is to save the recordset with XML and then you will be able to determine for sure whether there is data or not.

Just change the line
mrsRun.Save msFileName
mrsRun.Save msFileName, adPersistXML

Expert Comment

ID: 6366653
my practice, you need change code
    rsInput.Open msFileName, , , , adCmdFile
    rsInput.Open msFileName

BTW: in ExportData()
    '-- save recordset as a file
    Kill msFileName
    mrsRun.Save msFileName
if the file exited, save method will raise some error.

Expert Comment

by:Valliappan AN
ID: 6366808
You could check for recordcount like : (adding to emoreau's comments)

If rsInput.RecordCount <> 0 Then rsInput.MoveFirst

Since, sometimes, the RecordCount will be -1.

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question