MS Office Forum

Ask Question   UnAnswered
Home » Forum » MS Office       RSS Feeds

Unexplained "Bad Record Length" error / bug?

  Asked By: Everett    Date: Dec 11    Category: MS Office    Views: 1931

As an experienced VB programmer, I understand the normal causes of
the Bad Record Length error. I am stumped on this though and believe
it to be some kind of bug in VBA...

I have several user-defined data types which I use to Put/Get data to
and from random-access files. Occasionally I get the "Bad Record
Length" run-time error. There is no apparent reason for this error
occuring, it is intermittent, and can be resolved by the following

- comment-out an element within the data type
- attempt to run a procedure that includes a reference to this
element (this obviously causes a compile-time error "Method or data
member not found")
- end code execution
- reinstate declaration of the element

This process, in effect, forces VBA to recompile / rebuild the data
type, and everything runs fine again.

Anyone have an explanation?



7 Answers Found

Answer #1    Answered By: Fjodor Bonkob     Answered On: Dec 11

I suspect timing. There is as you know a lot going on under the
hood. My personal experience with "intermittent" errors is to shout

It may of course be a knee jerk... :-)

Try putting som DoEvent statements in your code  so that some processes
definately finish before others start.

In multi thread environments it's normal to check and see if a
particular thread has finished before continuing. Maybe you meed to
incorparate something like that.

Are you using a "fast" computer? Does the code run  ok on a slower
computer?.. ie an older one :-)

Windows typically uses write after caching.. Is the cache getting
written away before a different procedure  needs the data?

Answer #2    Answered By: Faizah Khan     Answered On: Dec 11

My program is not itself multi-threading,
so VBA / Windows should take care of any timing issues (I can't put a
DoEvents on every other line...) I'm going to put it down as a bug
in VBA.

Answer #3    Answered By: Kawkab Mansour     Answered On: Dec 11

Have you posted the code  that you are using to access your file? I don't recall
seeing it.

Some file I/O statements run  asynchronously and you have to specifically check
for completion.

Answer #4    Answered By: Christina Ramirez     Answered On: Dec 11

here is an example of the code  I am using. The 'Bad Record
Length' error  occurs here (on the Get... line) and elsewhere in my
program, for this and some other user-defined  data types. The
OpenFile function just opens the record  file for random access as
read-only and returns the file number. The line that includes  the
OpenFile function is equivalent to

intFileUsr = FreeFile
Open <recordfile> For Random Access Read As intFileUsr Len = Len

As I mentioned previously I can force a restructuring of the data
type by removing reference  to an element, attempting to run  some code
that includes this element, then reinstating the element. Everything
is fine  again then.

Public Type UsrType
LoginID As String * 5
Name As String * 20
SiteID As Byte
DeptID As Integer
Active As Boolean
Password As String * 10
PasswordSetDate As Long
HalfDayHolEnt As Byte
Perm(1 To 20) As Boolean
End Type

Public Sub USR_List(cntThis As MSForms.Control, Optional
blnIncInactive As Boolean)

'Display a list of users in a list or combobox

Dim intFileUsr As Integer
Dim usrThis As UsrType


intFileUsr = OpenFile(FileName(PATH_STATIC, FILETITLE_USR,
FILEEXT_USR), Len(usrThis), False, False)

Get intFileUsr, , usrThis
If EOF(intFileUsr) Then Exit Do
If usrThis.Active Or blnIncInactive Then cntThis.AddItem

Close intFileUsr

End Sub

Answer #5    Answered By: Charlie Smith     Answered On: Dec 11

No mention in the help of the get being asynchronous, and the code  looks
straightforward enough.

I do have a concern with your Perm(1 To 20) ...

The help for "get" talks about dynamic arrays and gives as example:

Dim MyArray(1 To 5,1 To 10) As Integer

I don't know if it treats such definitions as dynamic, even though they're not.
The gist of it is that you need to allow extra bytes for descriptors.

Are you in a position to try this with a more "traditional" array dimension to
see if it helps?

The only other thing I found in a quick Google trawl was a suggestion to use
LenB() instead of Len() to get the length  of UsrType.

Odd indeed that forcing a bad  compile is enough to fix the problem for a while.
Maybe there is a bug.

Answer #6    Answered By: Daniel Evans     Answered On: Dec 11

Quite sure it isn't a problem with arrays as I also get the error
(again intermittently) with this data  type:

Public Type TypeTRA
TypeID As Byte
Ref As String * 8
Date As Date
StockCode As String * 20
Qty As Integer
LocFrom As String * 9
LocTo As String * 9
End Type

The value returned by Len is not a problem, it is the same whether
the code  works or not.

I'm inclined to believe that there is some internal VBA bug  that
corrupts the data type structure in some way.

Answer #7    Answered By: Henry Davie     Answered On: 10:0 hrs ago

I'm afraid I don't have an answer - but I can confirm that it is a VBA bug which has haunted me from Office 200 to 2007. I get it with Microsoft Access. As the original respondent said, the VBA code is perfectly OK, and normally runs without a problem. But sometimes, when you re-compile it (due to some other change), up pops the bad record length message when you attempt to execute the fixed length record code. The solution, as described by the original respondent is to force a recompile - to do this, comment out part of the record structure so that a recompile fails with an error message - then remove the comment and recompile (the original code). It now usually works. Sometimes during the recompilation with the commented-out data, the compilation fails with an Internal Error.
I don't know if it persists with more modern Office Versions - since my clients have not yet updated from 2007.
I suspect the original respondent is the person I corresponded with many years ago, and I'm sure both of us would be delighted if Microsoft were to fix this bug

Didn't find what you were looking for? Find more on Unexplained "Bad Record Length" error / bug? Or get search suggestion and latest updates.