Re: Put Variables giving problems

From: Russ Holsclaw (russ_at_holsclaw.nyet)
Date: 01/25/05


Date: Tue, 25 Jan 2005 11:01:20 -0700


"Khai" <middae@hotmail.com> wrote in message
news:EfqdnSxU_sI38GvcRVn-uw@comcast.com...
>I don't know how to "link" my posts to another newsgroup, but - this
>Syntax
> one seems more like where the original post is supposed to be. I'm sorry
> for crossposting, but noone's answered the others anyway. ;)
>
> Assume Word 2000 VBA.
>
> myDataType
> Name as string
> phone as string
> appdate as Date
> codes(15) of String
> end type
>
> dim myR as myDataType
> dim myF as integer
>
> myF = Freefile
>
> ----
> at this point, I've tried several methods and combinations of Open,
> Append,
> random, etc.
>
> The only success I've had is when myDataType is very small, then Opening
> for
> Random, with a Len = Len(myR). Finding out how to append to it was a bit
> more difficult, because each variant i tried of Put, recnum, myR would
> come
> up with "Bad record space" or somesuch.
>
> myDataType has about 16 strings in it, and 4 dates. Then there's one
> array
> of 16 slots, that are strings. the array will not always be full.
>
> Am I just missing something? Everytime I try the Open "C:\testfile.txt"
> for
> Append Access ... it tells me "bad file mode" when I try to put. Random
> was
> the only that got me close, and it doesn't work with the larger datatype.
>
> Is this the wrong way I should go about it? Should I use ADO instead?
> and
> if I do that, what's the quickest way to get an understanding of it?
>
> The object is to write these datatypes that are based off the data in the
> Word Document Merge Source, then - another program would open that
> database
> file (or file of myDataTypes), and do something else with it.

The problem you have with Random is that your UDT structure is of fixed
size, and Random files are assumed to have fixed-length records. That is,
each record is assumed to be the same size, or at least confined to some
known limit.

Since your "codes" array consists of a set of variable-length strings, they
will be written to a Random file as a set of variable-size elements, each
preceded by a "string descriptor" which gives the length.

Your other String variables have the same problem.

If you really want to use Random, all of your strings should be
fixed-length, such as:

Type myDataType
    Name as string * 30 '
    phone as string * 13 '...as in "(xxx)xxx-xxxx"
                            '(larger if international)
    appdate as Date 'date is always 8 bytes
    codes(15) as String * 2 '...or however long your codes are
End type

This type of structure will work with Random.

The alternative, if you don't want to use fixed strings, is to use a Binary
file. In this case, however, each time you do a Put of one of your data
types, the size of the output will be variable, and you will
have to read them sequentially, starting with the first record, and
you will somehow have to know how many records there are. This
can be done by writing a record-count at the beginning of the file.
The record-count value can be written at the start of the file as
the last thing you write, before closing the file. Then, the code that
reads the file will know when to stop reading. There is no EOF()
function for Random or Binary files. You simply must record information
in the file that helps the program that's reading it "know" what
to expect. This requires a certain amount of planning, and
knowing what you're doing.

A file with fixed-length records (a Random file, that is) has the
advantage that individual records can be updated in place without
disturbing the surrounding data. A binary file with variable-length
records, on the other hand, should usually be written and read in
its entirety, in some carefully-planned sequence, so that it can
properly be interpreted by the program reading it.

Another little problem, in the case of VB, is that the core VB language
provides no way to reduce the size of the file, if updated contents
require less space than was consumed earlier. Ordinarily, and Random or
Binary file can only get larger, never smaller. There are API functions
you can use to "shrink" the file, but this must be considered
"advanced" programming.

If all this is more complicated than you bargained for, you should
consider using a database engine, such as Jet, that handles
the ugly details for you. Even Jet files must be compressed from
time to time, and your program should include the function of
running the compression functions of Jet, if you go that route.

Learning to write and read binary files can be very educational for
you, especially if you are new to computer programming. But don't
expect very much to be done automatically for you. It isn't. The
overall organization and layout of the file is entirely your
own responsibility. There is no magic. If you don't know what you're
doing, you can easily create a file that can not be successfully
read back in using any deterministic algorithm. If this happens, you
have only yourself to blame, not VB.

If this frightens you a little, then I have achieved my purpose.



Relevant Pages

  • Re: The annotated annotated annotated C standard part 4
    ... Unless string literals ... doesn't require variable-length instructions. ... The values are defined; each increment must happen ... the address of an object declared register. ...
    (comp.programming)
  • Re: Put Variables giving problems
    ... And - I would actually prefer to get it into a database that Access2000 ... >> phone as string ... > will be written to a Random file as a set of variable-size elements, ... > function for Random or Binary files. ...
    (microsoft.public.vb.syntax)
  • Re: Put Variables giving problems
    ... arrStrAs String ... dim myR as myDataType ... dim RecNum as Integer ... >> dim myR as myDataType ...
    (microsoft.public.vb.syntax)
  • Put Variables giving problems
    ... one seems more like where the original post is supposed to be. ... Name as string ... dim myR as myDataType ... Finding out how to append to it was a bit ...
    (microsoft.public.vb.syntax)
  • Re: Reading Binary File
    ... all files are binary files, but since the FSO is designed to ... object doesn't have any trouble reading a string with Chrin it ... Public Function GetMenuData(ByVal strTableName As String, ... Dim strTableData As String ...
    (microsoft.public.scripting.vbscript)

Quantcast