Re: Replace space in text file
- From: "Webbiz" <noreply@xxxxxxx>
- Date: Sun, 13 Jan 2008 19:51:14 -0600
GREAT READ!!!
Thanks Mike. That made a ton of sense.
The reason I made vChar a variant (by simply not assigning any type to it to
begin with) is because when reading about the GET function, I was left with
the impression it required a Variant. Thus the 'v' in vChar.
Sometimes the definitions of some functions are just not as clear to Novice
programmers. ;-b
Thanks again.
Webbiz
"Mike Williams" <mikea@xxxxxxxxxxxxxxxxx> wrote in message
news:OZAyuOcVIHA.5164@xxxxxxxxxxxxxxxxxxxxxxx
"Webbiz" <noreply@xxxxxxx> wrote in message
news:OMhUf5ZVIHA.2368@xxxxxxxxxxxxxxxxxxxxxxx
Also, why did I get the error I got?
Rick has already given you a full solution which will perform your
required task and I'm sure you'll be very happy with it, so I'll restrict
myself to answering just your one question (above) on the grounds that
Rick's code works in a quite different way to the code you were attempting
to write and on the assumption that for future reference you would still
like to know why you got the Automation error in your own code.
Incidentally, your code actually contains some logic errors which would
cause it to fail to perform your desired task anyway, even if the error
did not occur, but to keep it simple I won't deal with them here.
In your code you have declared the following variables:
Dim iFileNum1 As Integer
Dim iFileNum2 As Integer
Dim lByteNum As Long
Dim vChars
Dim sMarket As String
In four of the above cases you have specified which type of variable you
want VB to create (Integer, Long, String or whatever). However, in the
case of vChars you have not specified the required variable type and VB
will therefore create it as a Variant (the default variable type). A
Variant is a special kind of variable which is capable of holding any
other kind of variable (Integer, Long, String, etc), and its data area is
therefore slightly more complex than normal variables because it needs to
contain information about the type of variable it currently contains as
well as the actual variable data. The "type of variable" information is
usually held in the first two bytes of the Variant's data area in memory.
Variants should generally be avoided, although they do have some uses in
special cases.
In the part of your code in which you are attempting to load data from the
file you have the following line (the line on which your error will
occur):
Get #iFileNum1, lByteNum, vChars
When the VB Get statement loads some data it first checks to see what kind
of variable it is loading that data into and it reads exactly the number
of bytes from the file that are required to fill that variable. For
example, if vChars was an Integer then VB would read two bytes from the
file, and if it was a Long it would read four bytes, and if it was a
standard variable length String which currently contained nine characters
then it would read nine characters from the file and if it was a String
that currently contained just one character then it would read one
character from the file (which I assume was your intention).
However, in your specific case vChars is a Variant. In the case of a
Variant VB first reads two bytes from the file and it expects those two
bytes to contain information telling VB what kind of variable the Variant
is currently required to contain. It then immediately reads sufficient
further bytes from the file to fill that specific variable type (which
depend of course on what type it is currently required to be). That's
where the code is going wrong, and that's why you are getting the
Automation error. When the above line of code is executed VB reads the two
bytes from the file starting at position lByteNum and, because vChars is a
Variant, VB expects those two bytes to contain a "two byte number" (a
standard Integer) the value of which specifies exactly which type of
variable the Variant is currently required to contain (for example the
value 3 specifies a Long, the value 8 specifies a String, etc, etc). But
in your case those two bytes actually contain the Ascii codes of two
characters, which together form a numeric value that is nothing like any
of the values VB expects it to be, effectively an "illegal" type of
variable that cannot exist, and so VB reports that error with your
"Run-time error '458': Variable uses an Automation type not supported in
Visual Basic" error message. (By the way, it is of course possible to use
the Get statement to load a Variant from a file, but it must have been
written out to that file originally as a Variant using Put, or written out
in some way that mimics Put's behaviour in that respect).
Anyway, to cut this thing short, you can fix your problem (at least that
specific problem, because there are others in your code) by declaring
vChars as a String and by assigning it a string that contains exactly the
number of characters you wish to be read from the file when you use Get.
For example:
Dim vChars As String
vChars = "X" ' the string contains one character
Or of course you could in this case use a fixed length string. Anyway,
hopefully that explains the problem you are having and will enable you to
pick up where you left off in your "get one character at a time code",
which might still be worth pursuing simply as a learning experience even
though you have since been given some code that does the job for you in a
different way.
Mike
.
- References:
- Replace space in text file
- From: Webbiz
- Re: Replace space in text file
- From: MikeB
- Re: Replace space in text file
- From: Webbiz
- Re: Replace space in text file
- From: Mike Williams
- Replace space in text file
- Prev by Date: Re: Replace space in text file
- Next by Date: Re: Replace space in text file
- Previous by thread: Re: Replace space in text file
- Next by thread: Re: Replace space in text file
- Index(es):
Relevant Pages
|
Loading