Home All Groups Group Topic Archive Search About

Writing to .TXT file question

Author
2 Feb 2006 2:01 AM
Aaron
I receive a File Already Open error in the line:
' Open fname For Output As #fno '  of the file Open & write, section 2

I'm Filling a form's text box with a value from a text.txt file
and works perfect using:

'1------ Populate Text1 when form opens--------

Private Sub Form_Load()
'Code for loading textfile into a textbox
   Dim fno As Integer
   Dim fname As String
'Obtain the next free  file handle from system
   fno = FreeFile
   fname = "file_1.txt"
'Load the file into the text box
   Open fname For Input As #fno
   Text1 = Input$(LOF(fno), fno)
   Close #fno
End Sub

'2-------Write to Text.txt file on Text1 change -------------

Private Sub Text1_Change()
'code for saving textfile into a textbox
  Dim fno As Integer
  Dim fname As String

'Obtain the next free file handle from the system
  fno = FreeFile
  fname = "file_1.txt"

'Open and save the textbox to a file
   Open fname For Output As #fno   <----File already open error
   Print #fno, (Text1)
   Close #fno
End Sub

Any suggestions?

Author
2 Feb 2006 3:48 AM
Dmitriy Antonov
Show quote Hide quote
"Aaron" <nom***@please.com> wrote in message
news:FJdEf.1511$Wj.1164@fe07.lga...
>I receive a File Already Open error in the line:
> ' Open fname For Output As #fno '  of the file Open & write, section 2
>
> I'm Filling a form's text box with a value from a text.txt file
> and works perfect using:
>
> '1------ Populate Text1 when form opens--------
>
> Private Sub Form_Load()
> 'Code for loading textfile into a textbox
>   Dim fno As Integer
>   Dim fname As String
> 'Obtain the next free  file handle from system
>   fno = FreeFile
>   fname = "file_1.txt"
> 'Load the file into the text box
>   Open fname For Input As #fno
>   Text1 = Input$(LOF(fno), fno)
>   Close #fno
> End Sub
>
> '2-------Write to Text.txt file on Text1 change -------------
>
> Private Sub Text1_Change()
> 'code for saving textfile into a textbox
>  Dim fno As Integer
>  Dim fname As String
>
> 'Obtain the next free file handle from the system
>  fno = FreeFile
>  fname = "file_1.txt"
>
> 'Open and save the textbox to a file
>   Open fname For Output As #fno   <----File already open error
>   Print #fno, (Text1)
>   Close #fno
> End Sub
>
> Any suggestions?
>
>
>
>

Have you tried to Debug or check the Call Stack, when error occurs?

And, IMO, overriding the file's content on each change in textbox can be
overkill.

Dmitriy.
Author
2 Feb 2006 4:50 AM
Aaron
Show quote Hide quote
"Dmitriy Antonov" <antonovdima@netzero.net_NOT_FOR_SPAM> wrote in message
news:uvK1Bu6JGHA.2392@TK2MSFTNGP09.phx.gbl...
>
> "Aaron" <nom***@please.com> wrote in message
> news:FJdEf.1511$Wj.1164@fe07.lga...
>>I receive a File Already Open error in the line:
>> ' Open fname For Output As #fno '  of the file Open & write, section 2
>>
>> I'm Filling a form's text box with a value from a text.txt file
>> and works perfect using:
>>
>> '1------ Populate Text1 when form opens--------
>>
>> Private Sub Form_Load()
>> 'Code for loading textfile into a textbox
>>   Dim fno As Integer
>>   Dim fname As String
>> 'Obtain the next free  file handle from system
>>   fno = FreeFile
>>   fname = "file_1.txt"
>> 'Load the file into the text box
>>   Open fname For Input As #fno
>>   Text1 = Input$(LOF(fno), fno)
>>   Close #fno
>> End Sub
>>
>> '2-------Write to Text.txt file on Text1 change -------------
>>
>> Private Sub Text1_Change()
>> 'code for saving textfile into a textbox
>>  Dim fno As Integer
>>  Dim fname As String
>>
>> 'Obtain the next free file handle from the system
>>  fno = FreeFile
>>  fname = "file_1.txt"
>>
>> 'Open and save the textbox to a file
>>   Open fname For Output As #fno   <----File already open error
>>   Print #fno, (Text1)
>>   Close #fno
>> End Sub
>>
>> Any suggestions?
>>
>>
>>
>>
>
> Have you tried to Debug or check the Call Stack, when error occurs?
>
> And, IMO, overriding the file's content on each change in textbox can be
> overkill.
>
> Dmitriy.

Ok

I have the error cleared.
Can you consider one more question?

When I store a value(with below code), and re-display it, its has two Dark
verticle bars at the end of my text:  || (darker & shorter)

When I look at the file in Notepad the bars are not visible.  What a wierd
thing.
I have created a new text.txt file, disaplayed it (without writing to it
with my code) and its perfect -- no bars.

I'm saving the file with a Command button for a test purposes using the
following:

Private Sub Command8_Click()
  Dim intfilehandle As Integer 'File Handle
  Dim strvalue As String    'Value to store
  Dim strfilename As String
  strfilename = "C:\Program Files\Microsoft Visual Studio\VB98\file_1.txt"
strvalue = Form1.Text1
'Obtain the next free file handle from the system
  intfilehandle = FreeFile
  'Open and save the textbox to a file
  Open strfilename For Output As #intfilehandle
  Print #intfilehandle, strvalue
  Close #intfilehandle
End Sub

Any ideas appreciated.
Author
2 Feb 2006 5:39 AM
Mike Williams
"Aaron" <nom***@please.com> wrote in message
news:DcgEf.1152$yw5.287@fe05.lga...

> When I store a value(with below code), and re-display it, it
> has two Dark verticle bars at the end of my text:  ||

Presumably you're displaying the string in a Text Box at its default
settings (Multiline = False). Such text boxes cannot actually "do the
appropriate thing" when they come accross a "new line" code (vbCrLf) and so
they display it as the symbol you are seeing. Change the Text Box Multiline
property to True and the vbCrLf will be able to "do the job it is supposed
to do" (move to the start of a new line). By the way, the VB Print statement
(which you are using to write your text to the file) by default writes a
vbCrLf  at the end of your string (which is why your file ends up containing
one even though you didn't explicitly put it there). If you want to suppress
that behaviour then follow the Print statement with a semi colon. In other
words, instead of . . .

Print #intfilehandle, strvalue

.. . . you should use:

Print #intfilehandle, strvalue;

Mike
Author
2 Feb 2006 6:10 AM
Aaron
Your a genius!
What a life saver.
Thank you.

Show quoteHide quote
"Mike Williams" <M***@WhiskyAndCoke.com> wrote in message
news:%23fkxBs7JGHA.3408@TK2MSFTNGP12.phx.gbl...
> "Aaron" <nom***@please.com> wrote in message
> news:DcgEf.1152$yw5.287@fe05.lga...
>
>> When I store a value(with below code), and re-display it, it
>> has two Dark verticle bars at the end of my text:  ||
>
> Presumably you're displaying the string in a Text Box at its default
> settings (Multiline = False). Such text boxes cannot actually "do the
> appropriate thing" when they come accross a "new line" code (vbCrLf) and
> so they display it as the symbol you are seeing. Change the Text Box
> Multiline property to True and the vbCrLf will be able to "do the job it
> is supposed to do" (move to the start of a new line). By the way, the VB
> Print statement (which you are using to write your text to the file) by
> default writes a vbCrLf  at the end of your string (which is why your file
> ends up containing one even though you didn't explicitly put it there). If
> you want to suppress that behaviour then follow the Print statement with a
> semi colon. In other words, instead of . . .
>
> Print #intfilehandle, strvalue
>
> . . . you should use:
>
> Print #intfilehandle, strvalue;
>
> Mike
>
>
>
Author
2 Feb 2006 5:46 AM
LoVe ScoRpiOn
hi

yes  it is right that  File already open error
becuase when u load the form u make change to text
and this is done at the same time exactly
so it gives ( File already open error )
in visual basic u can't  read  & write to  a text file  at the same time


so

here is a trick to do it

by using  Timer 
u will be able to read from a text file and write to it
look at this code:

Private Sub Form_Load()

Dim filepath As String
Dim fr As Integer
fr = FreeFile
filepath = "file_1.txt"
Open filepath For Input As fr
Text1.Text = Input$(LOF(fr), fr)
Close #fr

End Sub



Private Sub Timer1_Timer()
Dim filepath As String
Dim fr As Integer



fr = FreeFile
filepath = "file_1.txt"
Open filepath For Output As fr
Print #fr, Text1.Text
Close #fr
End Sub

----

i hope  u get the idea -- LoVe ScoRpiOn ------------------------------------------------------------------------ Posted via http://www.codecomments.com ------------------------------------------------------------------------
Author
2 Feb 2006 6:09 AM
LoVe ScoRpiOn
i forgot to tell u

change timer interval  for the timer
from timer1 properties -- LoVe ScoRpiOn ------------------------------------------------------------------------ Posted via http://www.codecomments.com ------------------------------------------------------------------------
Author
2 Feb 2006 3:31 PM
Aaron
All is working.
Thanks to all.

Show quoteHide quote
"LoVe ScoRpiOn" <LoVe.ScoRpiOn.22l***@mail.codecomments.com> wrote in
message news:LoVe.ScoRpiOn.22lj9q@mail.codecomments.com...
>
> i forgot to tell u
>
> change timer interval  for the timer
> from timer1 properties
>
>
>
> --
> LoVe ScoRpiOn
> ------------------------------------------------------------------------
> Posted via http://www.codecomments.com
> ------------------------------------------------------------------------
>