How to do I/O write# and input# with ; as delimiter?

Premy

Registered User.
Local time
Today, 12:28
Joined
Apr 10, 2007
Messages
196
Hi folks,

I need to generate a write instruction that
writes strings to a file without appending string quotes to it and also allows me to put ; as a delimiter instead of just a comma. For example:

Dim hFile As Long
Dim v1 as string
Dim v2 as string
hFile = FreeFile
v1 = "text1"
v2 = "text2"
Open "TestFile.txt" For Append Access Write As hFile
Write #hFile, v1, v2
Close hFile

The output I've been getting is:
"text1","text2" but I really need as output:
text1;text2

Likewise I would like to be able to read the latter with input# just the way it is. I've only been able to read strings from a file, so far, by changing the format to:
"text1","text2"

I appreciate your help

regards,
Jaime
 
Thank u BrettStah. Wasn't even necessary to google around, I just looked up FileSystemObject in VBA help, and there it is, seemingly offering all the info I need in order to get along with my project.

Thanks again
 
So now I know how to write to a file in the format I need. Passing the values as variabes i quite easy too:

Dim v1 As String
Dim v2 As String
v1 = "1001;"
v2 = "2002;"
Set fs = CreateObject("Scripting.FileSystemObject")
Set a = fs.CreateTextFile("C:\My Documents\TestFile.txt", True)
a.WriteLine (v1 & v2)
a.Close

This outputs a file named TextFile.txt which contains:
1001;2002;

Just what I needed. But now, to retrieve values as variables from a file, is less obvious to me. If we'd take the Testfile.txt created above, I would want to have 1001 and 2002 assigned to 2 variables seperated by ;
How could I best achieve this? Anyone to get me going from here?

Thanks in advance
 
So the text file will always have one line, with two and only two items?
 
You just need the Split function:


To split a string, you use

Dim v As Variant

v = Split(YourTextLineHere,";")

Then you can use v(0) v(1) v(2) etc. to pull the values.
 
Hi BrettStah,
Well I just put 1 line and 2 values, just for ease of reading, in reality they will vary. But I think that once I get the workings, I can apply to any number of lines and values, don't u think? Or are there other issues when going beyond
1line/2values?

Thanks
 
The split function and a loop to loop through the text file will do it for you. I will attempt to write an example for you, if you can supply me with an idea of what exactly you are going to try to do with them.
 
Hi Bob, thanks for your reply

I did think about splitting, but with a large amount of values on multiple lines, would it still be the best way? And I'm also talking about regularly changing values here. Anyway I would need the "read" code also (the code to read from a file with values delimited by semicolon), since the example code in VBA help is not very clear to me as yet:

Sub OpenTextFileTest
Const ForReading = 1
Dim fs, f
Set fs = CreateObject("Scripting.FileSystemObject")
Set f = fs.OpenTextFile("c:\TestFile.txt", ForReading,TristateFalse)
f.Close
End Sub

How would I go about manipulating the value (string) stored in f? I tried to assign it to another variable but this generates an error of the kind "Method or Property not supported by this object. The only thing I seem able to do with f is f.write, but I don't want to write, I want to read.

Thank u for yr patience

regards,
Jaime Premy
 
Going back to the initial question, using Print in stead of Write, should achieve that objective - with a bit of concatenation

Print #hFile, v1 & ";" & v2

If you use early binding, at least when developing, you'd find f to be of the type TextStream, not string, and that it has several interesting methods - Read, ReadLine and ReadAll. Perhaps ReadLine is what you need?

MyString = f.ReadLine
MyArr = Split(MyString, ";")
For MyCounter = 0 to ubound(MyArr)
debug.print MyArr(MyCounter)
Next MyCounter

For early binding, try declaring as
Dim fs as scripting.filesystemobject
dim f as scripting.textstream
 
I say thanks a lot to all of you. I think that Roy's Readline suggestion will best work for my objective in easily putting the values from the text file into variables. Writing is not a problem anymore, thanks to BrettStah's suggestion (using FileSystemObject). All this info I needed was in order to enable me to communicate with a set of dll's from a special kind of invoice printer we use here in Brazil, which is bound by sets of rules from the Internal Revenue Service. Yes, in Brazil you can't just spit out an invoice, it must comply to rules that allow the IRS to directly controll a company's sales revenue. All this is fairly well documented for Delphi and VB apps, but not for Access Apps. I tried to figure it out thru the the VB documentation but only succeeded partially, hence this posting. There's also the problem that every printer manufacturer (all local companies) have different approaches and my app should be able to accomodate all of 'em. So I will be experementing for some time now and maybe will bother u guys again (once I get stuck -again:) ).

Thanks again,

Regards,
Jaime Premy
 

Users who are viewing this thread

Back
Top Bottom