Re: Replace space in text file



"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



.



Relevant Pages

  • Re: Replace space in text file
    ... The reason I made vChar a variant (by simply not assigning any type to it to ... Dim sMarket As String ... case of vChars you have not specified the required variable type and VB ... standard variable length String which currently contained nine characters ...
    (microsoft.public.vb.general.discussion)
  • Re: String to Number ?
    ... I'm guessing the space-dash-space characters, taken together, is the ... then this will store the number parts in a variant array. ... a listing of 100 times each number in the record string... ... you would Dim Numbers As Variant when declaring it. ...
    (microsoft.public.vb.general.discussion)
  • Re: Comparing a variant string with char*
    ... A VARIANT type is not a string, and is not comparable to a string; ... extremely unlikely situation where you would have a pointer to an 8-bit character string ... There is no VARIANT type that can represent a char string, for example, although there is ... assuming 8-bit characters), strcmp. ...
    (microsoft.public.vc.mfc)
  • Re: Problem with large amount of characters in a VBA variant
    ... a SQL Server database via ODBC. ... The string is correctly stored in the Access ... variant VBA variable, but when looking at the variant with the ... I find that it has got only 255 characters (when the ...
    (comp.databases.ms-access)
  • Re: Code to delete/unlink Linked tables
    ... Public intLinkODBCTables As Variant, intLinkDB2Tables As Variant ... Public strLinkBackendDB As String, strLinkDSNname As String, strLinkLibName ... ' MsgBox "This database is in MDE format...I will delete/recreate ODBC ... Public Sub fncLinkDB2Table() ...
    (microsoft.public.access.modulesdaovba)