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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writting Data from A Form to A File: HELP

Status
Not open for further replies.

NikosXP

Instructor
Oct 24, 2001
13
0
0
GR
Hi to All
I hace a data file containing just 3 Numbers.
something like

1 2 3(1 space between Numbers(all positive))
The file has to stop at the end of the third Number(no spaces or newline vhars after that).
I read the file from a Form I have build in Access,
I change the values of these numbers I I put them back to the File(a text file.
I wrote the following piece of code in VBA which I attached to the click event on a DONE Button on the form to do this:
(I'm A Newbie...)

Dim FileNum As Integer
Dim K As String
Dim L As String
Dim M As String

FileNum = FreeFile()
Open "inputfile.txt" For Binary Access Write As FileNum

K = Format(Text0.Value) 'the value in textbox1
L = Str(Text2.Value) 'to leave a blank in front of Number
M = Str(Text4.Value) '....

Put #FileNum, , K
Put #FileNum, , L
Put #FileNum, , M
Close FileNum

The code seems to work , but If I write for example in the file the numbers 1 2 11, then read the file back in the form , change the numbers to 1 2 5, I get A file with numbers 1 2 51 , I know there is sth wrong in the way I write to to the file but I don't know what

Any ideas would be much Appreciated..
Nick
 
Hi,

Assumning that you use 'Open For Bianry Access Read' to retrieve the data:
The problem is that the type 'string' does not have a fixed lenght. When you open your file for binary and use 'get' to retrieve data you have to know how many bytes (usually which type of variable) that you want to read.

I would recommend you to use 'open for input', designed for reading text files.

Try something like:
---------------------------------------------------------
Dim FileNum As Integer, MyStr as String
Dim Data as Variant

FileNum = FreeFile()
Open "inputfile.txt" For Input As FileNum
line input #FileNum, MyStr
close #FileNum
'Here you need to parse the line that you have read
Data = Split(MyStr," ")
'The values are now in Data(0), Data(1) and Data(2)
---------------------------------------------------------

Sunaj

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top