Solved

How to read a recordset file in VB6?

Posted on 2001-08-08
7
819 Views
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
  mrsRun.Close
End Sub

Sub UpdateDB()      
  '-- open input file
  rsInput.Open msFileName, , , , adCmdFile     'This is the file created from tblRun
  rsInput.MoveFirst    <==== CRASHES HERE
  ....
  ....
  ....
End Sub
0
Comment
Question by:daquin
7 Comments
 
LVL 69

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
0
 

Author Comment

by:daquin
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?  
0
 
LVL 69

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.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:daquin
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?
0
 
LVL 75

Accepted Solution

by:
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
to
mrsRun.Save msFileName, adPersistXML
0
 
LVL 4

Expert Comment

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

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

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.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Cannot enter table caption text in Word protected document 6 110
change vba from autofit to 13.5 width? 4 28
Validating VB6 Function 19 56
Microsoft Access combo box help 2 37
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
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…

919 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now