Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Write # to ascii file

Status
Not open for further replies.

lostintheglades

Programmer
Jul 28, 2003
4
US
I am exporting data out to an Ascii file using the Write # statement. If a field has a quote in it it is enclosed by quotes which is fine. The problem comes when reading the data back in.

Rather that re-write the entire code to use a print # and then parse the string coming back in. I was going to use the INStr in a function to replace and quotes with a non keyboard character..(chr(143) for now)

I created a function and thought I'd try to call the function while doing the write

ie: write #1, remove_quotes(.field("field 1"),remove_quotes(.field("field 2").......etc.

Instead all I get are blank fields. Has anyone ever tried anything like this or am I going to have to remove the quotes saving the new data to a variable.

Also...any idea as to why the input doesn't catch the quote when reading the data back in?

 

Perhaps a look at your remove_quotes function along with a example of data that you call it with may help clarify what you are trying to accomplish.

Good Luck

 
I'm trying to replace any " in the data with another ascii character prior to writing it out to a text file.

For some reason when the data is getting corrupted when I try to bring the text file back into the Access table. I know there are several ways to accomplish this but I'm trying to find the easy way out by simply calling the function that will replace the quotes while I am writing the fields to the text file using the "Write" command rather than the "Print" or "Put".

All fields are text fields. so...I have a statement that looks like this.

write #1 Remove_Quotes(.fields("field 1")), Remove_Quotes(.fields("field 2")) instead of using the normal

write .fields("field 1"),.fields("field 2")

The value is not being returned from the function Remove_Quotes. I may have to toss this idea...just thought I'd check to see if anyone has tried this in VB. The idea works in C and other languages.

 

If we could look at your remove_quotes function may help us help you in figuring out why it is not returning a value for you, or maybe you can debug it your self using this example...
[tt]
Public Function Remove_Quotes(StringToTest As String) As String
Remove_Quotes = Replace(StringToTest, """", Chr(143))
End Function
[/tt]

which can be tested as follows...
[tt]
Option Explicit

Private Sub Form_Load()

Dim S As String

S = "This is a " & """" & "test" & """"
MsgBox S
MsgBox Remove_Quotes(S)

End Sub

Public Function Remove_Quotes(StringToTest As String) As String
Remove_Quotes = Replace(StringToTest, """", Chr(143))
End Function
[/tt]

 
The problem you're running into is that Write # puts quotes around any string value you write to a file.

statement:
Write #1, mystring1,mystring2,integer1,integer2

output:
"mystring #1","mystring #2",123,456

So, when you put a quote in a string it causes VB to read it back as a closing of the string being read, thus the string is cut off. Your idea of substituting quotes for a different character would work. When writing to file simply do:

Write #1, replace(mystring,chr$(34),"replacementcharacter")

Chr$(34) is the quote character. Then when you read it back in you just do:

Input #1, mystring
Replace(mystring,"replacementcharacter",chr$(34))

OR skip having to replace the quote character altogether by using print # and line input # statements. This way you read in a whole line and then parse the data by specifying a delimiter to look for. i.e. VB won't look for quotes, it reads in the whole line, quotes and all.

(in this case comma is going to be the data delimiter)

statement:
print #1, mystring1 & "," & mystring2

output:
mystring1,mystring2

Now read it in and parse it:

'Variant array used to store data
Dim myarray as variant

'Input a whole line from text file
Line Input #1, wholelinestring

'Split string into words using comma as delimiter
'store words in an array starting at zero
myarray = Split(wholelinestring,",")

myfield1 = myarray(0)
myfield2 = myarray(1)
 
If you're working with a preset amount of fields that never change you might want to consider using random access files. Here's a small sample:

'Put this in a Module if you want it public
Type temptypename
Name As String * 20
PhoneNumber As String * 12
EmployeeNumber As Integer
End Type

'Store the record template for use
Dim EmployeeRecord as temptypename

Now that we have a recordset, it's simple to store and retrieve records from a random access file:

'Open a random access file and set record length as the length of EmployeeRecord

Open "myrandfile.ext" For Random Access Read Write As 1 Len=Len(EmployeeRecords)

'Set a max records available integer

Dim MaxRecords as Integer

'Amount of records in file equals length of file number divided by the record template length

MaxRecords = LOF(1)/Len(EmployeeRecords)

'Now we know how many records are in the file

'Pick a record # and retrieve it
Get 1, recordnumber, EmployeeRecords

'Since .Name has a set length of 20, you have to trim it before using it or you'll have spaces on the end.
txtName.Text = Trim(EmployeeRecords.Name)

'Store a new record just as easily...
EmployeeRecords.Name = txtName.Text
Put 1, , EmployeeRecords

'or replace an existing record
Put 1, recordnumber, EmployeeRecords
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top